C language - record data (add,delete) in text file using file IO, structure, function, array, pointers

C language - record data (add,delete) in text file using file IO, structure, function, array, pointers

To record data (add, delete) in a text file using file IO, structures, functions, arrays, and pointers in C, you can follow these steps:

  1. Define a structure to represent the data you want to store.
  2. Implement functions to add and delete records.
  3. Use file IO functions to read from and write to a text file.
  4. Use arrays to store multiple records.
  5. Use pointers to manipulate the array elements.

Here's an example implementation:

#include <stdio.h> #include <stdlib.h> #include <string.h> // Define a structure to represent a record struct Record { int id; char name[50]; float score; }; // Function to add a record to the array void addRecord(struct Record *records, int *count, int id, const char *name, float score) { records[*count].id = id; strcpy(records[*count].name, name); records[*count].score = score; (*count)++; } // Function to delete a record from the array void deleteRecord(struct Record *records, int *count, int id) { int i, j; for (i = 0; i < *count; i++) { if (records[i].id == id) { for (j = i; j < (*count) - 1; j++) { records[j] = records[j + 1]; } (*count)--; break; } } } // Function to save records to a text file void saveToFile(struct Record *records, int count, const char *filename) { FILE *file = fopen(filename, "w"); if (file == NULL) { printf("Error opening file.\n"); return; } for (int i = 0; i < count; i++) { fprintf(file, "%d %s %.2f\n", records[i].id, records[i].name, records[i].score); } fclose(file); } int main() { struct Record records[100]; int count = 0; // Add records addRecord(records, &count, 1, "John", 85.5); addRecord(records, &count, 2, "Alice", 90.0); // Delete a record deleteRecord(records, &count, 1); // Save records to file saveToFile(records, count, "records.txt"); return 0; } 

In this example:

  • We define a Record structure to store the data.
  • We implement functions addRecord and deleteRecord to add and delete records, respectively.
  • We implement a saveToFile function to save records to a text file.
  • In the main function, we add records, delete a record, and then save the records to a file.

This is a basic example. You can expand upon it to include error handling, additional functionalities, and improvements as needed.

Examples

  1. How to add records to a text file in C using file I/O, structures, arrays, and pointers?

    • Description: This query focuses on adding new records to a text file in C, leveraging file I/O operations, structures to define record format, arrays to store multiple records, and pointers for efficient memory management.
    // Code to add a record to a text file using file I/O, structures, arrays, and pointers #include <stdio.h> #include <stdlib.h> struct Record { int id; char name[50]; float score; }; void addRecord(struct Record *records, int size) { FILE *file = fopen("records.txt", "a"); if (file != NULL) { fprintf(file, "%d %s %f\n", records[size].id, records[size].name, records[size].score); fclose(file); } else { printf("Error opening file!"); } } 
  2. C program to delete a record from a text file using file I/O, structures, arrays, and pointers

    • Description: This query aims to learn how to delete a specific record from a text file in C, employing file I/O operations, structures to represent records, arrays to manage records, and pointers for efficient data manipulation.
    // Code to delete a record from a text file using file I/O, structures, arrays, and pointers #include <stdio.h> #include <stdlib.h> struct Record { int id; char name[50]; float score; }; void deleteRecord(struct Record *records, int size, int idToDelete) { FILE *file = fopen("records.txt", "w"); if (file != NULL) { for (int i = 0; i < size; i++) { if (records[i].id != idToDelete) { fprintf(file, "%d %s %f\n", records[i].id, records[i].name, records[i].score); } } fclose(file); } else { printf("Error opening file!"); } } 
  3. C program to read records from a text file using file I/O, structures, arrays, and pointers

    • Description: This query seeks guidance on reading records stored in a text file in C, employing file I/O operations, structures to represent record format, arrays to store records, and pointers for efficient data retrieval.
    // Code to read records from a text file using file I/O, structures, arrays, and pointers #include <stdio.h> #include <stdlib.h> struct Record { int id; char name[50]; float score; }; void readRecords(struct Record *records) { FILE *file = fopen("records.txt", "r"); if (file != NULL) { int i = 0; while (fscanf(file, "%d %s %f", &records[i].id, records[i].name, &records[i].score) != EOF) { i++; } fclose(file); } else { printf("Error opening file!"); } } 
  4. C program to update a record in a text file using file I/O, structures, arrays, and pointers

    • Description: This query focuses on updating an existing record in a text file in C, utilizing file I/O operations, structures for record representation, arrays for record storage, and pointers for efficient data manipulation.
    // Code to update a record in a text file using file I/O, structures, arrays, and pointers #include <stdio.h> #include <stdlib.h> struct Record { int id; char name[50]; float score; }; void updateRecord(struct Record *records, int size, int idToUpdate, float newScore) { FILE *file = fopen("records.txt", "w"); if (file != NULL) { for (int i = 0; i < size; i++) { if (records[i].id == idToUpdate) { records[i].score = newScore; } fprintf(file, "%d %s %f\n", records[i].id, records[i].name, records[i].score); } fclose(file); } else { printf("Error opening file!"); } } 
  5. C program to search for a record in a text file using file I/O, structures, arrays, and pointers

    • Description: This query involves searching for a specific record in a text file in C, leveraging file I/O operations, structures for record representation, arrays for storing records, and pointers for efficient data access.
    // Code to search for a record in a text file using file I/O, structures, arrays, and pointers #include <stdio.h> #include <stdlib.h> struct Record { int id; char name[50]; float score; }; int searchRecord(struct Record *records, int size, int idToSearch) { for (int i = 0; i < size; i++) { if (records[i].id == idToSearch) { return i; // Return index of the found record } } return -1; // Return -1 if record not found } 
  6. C program to add a record to a text file using file I/O, structures, arrays, and pointers

    • Description: This query focuses on adding a new record to a text file in C, utilizing file I/O operations, structures to define record format, arrays to manage records, and pointers for efficient memory management.
    // Code to add a record to a text file using file I/O, structures, arrays, and pointers #include <stdio.h> #include <stdlib.h> struct Record { int id; char name[50]; float score; }; void addRecord(struct Record *records, int size) { FILE *file = fopen("records.txt", "a"); if (file != NULL) { fprintf(file, "%d %s %f\n", records[size].id, records[size].name, records[size].score); fclose(file); } else { printf("Error opening file!"); } } 
  7. C program to delete a record from a text file using file I/O, structures, arrays, and pointers

    • Description: This query aims to learn how to delete a specific record from a text file in C, employing file I/O operations, structures to represent records, arrays to manage records, and pointers for efficient data manipulation.

More Tags

visual-studio-code nsstring pdflatex libavformat filestructure oracle-xe steganography bitarray real-time-clock docker-copy

More Programming Questions

More Investment Calculators

More Everyday Utility Calculators

More Tax and Salary Calculators

More Livestock Calculators