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

C Basic Language (Finding a line in a file, deleting the contents of the line an

ID: 3825188 • Letter: C

Question

C Basic Language (Finding a line in a file, deleting the contents of the line and replacing it with a new string)

I have a list of things to do that is printed out 1. 2. 3. ..., and i need the user to enter in a character 1,2,3... for which task on the to do list they are wanting to edit. Then the program goes to that line in the file (i have it so each entry is on a separate line in the file) and takes it out and then asks the user for the new string, then the program prints out the contents of the file with the new description. this is what its supposed to do:

1. Walk Dog

2. Clean house

3. Do homework

which entry do you want to edit? : 2

new description?: Mow Lawn

1. Walk dog

2. Mow Lawn

3. Do homework

Here is my function editEntry(), i thought this was supposed to work, but it doesnt even get to where i can enter in a new description and just skips most everything and prints out my main menu again with no changes,

void editEntry() {
   char filename[40];
   char c;
   int delete_line;
   int   tem = 1;

   flist = fopen("mylist.txt", "r");
   c = getc(flist);
   while (c != EOF) {
       printf("%c", c);
       c = getc(flist);
   }
   printf("Enter entry number to edit (1..%d; 0 to cancel):", t);
   scanf("%d", &delete_line);
   rewind(flist);
   ftemp = fopen("copy.c", "w");
   c = getc(flist);
   while (c != EOF) {
       if (c == 'n')
       {
           tem++;
       }
       //till the line to be deleted comes,copy the content to other
       if (tem != delete_line)
       {
           putc(c, ftemp);
       }
       else
       {
           while ((c = getc(flist)) != 'n')
           {
           }
           //read and skip the line ask for new text
           printf("Enter a new description (empty to cancel):");
           //flush the input stream
           fflush(stdin);
           putc('n', ftemp);
           //put 'n' in new file
           while ((c = getchar()) != 'n')
               putc(c, ftemp);
           //take the data from user and place it in new file
           fputs("n", ftemp);
           tem++;
       }
       //continue this till EOF is encountered
       c = getc(flist);
   }
   fclose(flist);
   fclose(ftemp);
   remove("mylist.txt");
   rename("copy.c", "mylist.txt");
   flist = fopen("mylist.txt", "r");
   //reads the character from file
   c = getc(flist);
   //until last character of file is encountered
   while (c != EOF)
   {
       printf("%c", c);
       //all characters are printed
       c = getc(flist);
   }
   fclose(flist);
   return 0;

};

Explanation / Answer

#include <stdio.h>
#include <string.h>

int main(void)
{
FILE *fileptr1, *fileptr2;
char filechar[40];
char c;
int delete_line, temp = 1;
   
printf("Enter file name: ");
scanf("%s", &filechar);

fileptr1 = fopen("E:/Chegg/input.txt", "r");
c = getc(fileptr1);
//print the contents of file .
while (c != EOF)
{
printf("%c", c);
c = getc(fileptr1);
}
printf(" Enter line number to be deleted and replaced: ");
scanf("%d", &delete_line);
//take fileptr1 to start point.
rewind(fileptr1);
//open replica.c in write mode
fileptr2 = fopen("E:/Chegg/replica.txt", "w");
c = getc(fileptr1);
while (c != EOF)
{
if (c == ' ')
{
temp++;
}
//till the line to be deleted comes,copy the content to other
if (temp != delete_line)
{
putc(c, fileptr2);
}
else
{
while ((c = getc(fileptr1)) != ' ')
{
}
//read and skip the line ask for new text
printf("Enter new text");
//flush the input stream
fflush(stdin);
putc(' ', fileptr2);
//put 'n' in new file
while ((c = getchar()) != 'n')
putc(c, fileptr2);
//take the data from user and place it in new file
fputs("n", fileptr2);
temp++;
}
//continue this till EOF is encountered
c = getc(fileptr1);
}
fclose(fileptr1);
fclose(fileptr2);
remove(filechar);
rename("replica.c", filechar);
fileptr1 = fopen(filechar, "r");
//reads the character from file
c = getc(fileptr1);
//until last character of file is encountered
while (c != EOF)
{
printf("%c", c);
//all characters are printed
c = getc(fileptr1);
}
fclose(fileptr1);
return 0;
}