Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need some help with a C-programming project I have. I need to read from a file

ID: 3827317 • Letter: I

Question

I need some help with a C-programming project I have. I need to read from a file (A todo list which my program is supposed to create). I need the material from the file to be read into a structure array using dynamic memory but I dont completely understand the concept or how to write it. Once it is in the structure array I should be able to handle the data but then I will need to close it again and I don't know If I have that correct either.

This is in my Header.h file:

typedef enum Status { Active, Completed } type;

typedef struct Entries
{
   int Number;
   char EntryName[200];
   type Status;
}Entries;

Entries Ptr[20];

int cntr;

This is in my function.c file:

void ReadFile()
{
   Entries * P;
   P = malloc(sizeof(Entries));

   FILE *fp;
   if ((fp = fopen("todo.txt", "r")) == NULL)
   {
       printf("Can't open file.");
       return;
   }

   cntr = 1;

   while (fscanf(fp, "%d. [%d] %s", Ptr[cntr].Number, Ptr[cntr].Status, Ptr[cntr].EntryName) != EOF)
   {
       cntr++;
   }
   fclose(fp);
}

_____________________________________________________________________________________

void RewriteFile()
{
   FILE *fp;
   if ((fp = fopen("todo.txt", "w")) == NULL)
   {
       printf("Can't open file.");
       return;
   }
   for (int o = 1; o < cntr; o++)
   {
       fprintf("%d. [%d] %s", Ptr[o].Number, Ptr[o].Status, Ptr[o].EntryName);
   }

}

_______________________________________________________________________

cntr is a counter for how many entries are in the todo list

I was wanting the output (in the file) to look something like

1. [1] Sample entry

2. [0] Sample entry

3. [0] etc......

I can figure out how to manipulate the rest of the program to do what I want, but I don't entirely know how to handle a dynamic memory structure array... when I go to rewrite an entry name for example. do I use Ptr[cntr]->EntryName?

(side note, given that my program will have multiple options for manipulating the list I was going to set it up so that at the beginning of each function that will change the list I would re-read the file into the struct, go on to manipulate it within the struct and then rewrite the list at the end of the function. So I want "ReadFile()" and "RewriteFile()" to be functions that I can call at the beginning and end of the functions to save me time)

Explanation / Answer

//define MAX as 100 after include statement in .h file. then I chenged two functions to read and write to file, if my //solution helpful please rate.

#define MAX 100

void ReadFile()
{
   Entries * P;
   //Cheg EA , here you require how many numbber of structure elements created ,so you need , sizeof(Entries) * no of array of struct elements,define MAX to hold that
   P = (Entries*)malloc(sizeof(Entries)*MAX);
   FILE *fp;
   if ((fp = fopen("todo.txt", "r")) == NULL)
   {
       printf("Can't open file.");
       return;
   }
   cntr = 0;
   while (fscanf(fp, "%d%d%s", &Ptr[cntr].Number, &Ptr[cntr].Status, &Ptr[cntr].EntryName) != EOF)
   {
       cntr++;
   }
   fclose(fp);
}

void RewriteFile()
{
   FILE *fp;
   if ((fp = fopen("todo.txt", "w")) == NULL)
   {
       printf("Can't open file.");
       return;
   }
   for (int o = 0; o < cntr; o++)
   {
       fprintf(fp,"%d %d %s ", Ptr[o].Number, Ptr[o].Status, Ptr[o].EntryName);
   }
   fclose(fp);
}

---------------------------------------------------------------