0

I'm trying to write a program which can delete a record in file io. The file content is set by default. Now I am trying put my text file content into Array, but there is some trouble happening....

Firstly,it is my default data file content:

1001
eric
1
human
10
70.00
eric
home
arrive


1002 
She
1 
human 
10 
50.00 
she
home 
arrive


1003 
She_eric
2 
human 
10 
120.00 
eric 
home 
arrive

Here is my code:(i am using fscanf putting my text file data into array,you can see it in the middle about open file for read)

#include <stdio.h>
#include <stdlib.h>
struct record{
char recordnum [40];
char itemrecord [40];
char quantity [40];
char weight [40];
char itemname [40];
char catagory [40];
char recipient [40];
char final_destination [40];
char status [40];
};



int main()
{
FILE *fileptr1, *fileptr2, fileptr3;
char filename[40]="record.txt";
char save;
int delete_num, temp ;
char reply;

#define MAX 9
struct record Arr[MAX];

printf("Enter file name: ");
scanf("%s", filename);


do{
temp =1; //reset temp and loop can work below time

//open file in read mode
fileptr1 = fopen(filename, "r");
if (fileptr1== NULL){
printf("open unsuccessful,file not exist"); 
exit(1);
}

save = getc(fileptr1);
while (save != EOF)
{
    printf("%c", save);
    save = getc(fileptr1);
    int i =0;
    for (i=0; i<3 ; i++){

    fscanf(fileptr1,"%s %s %s %s %s %s %s %s %s", Arr[i].recordnum, 
 Arr[i].itemname, Arr[i].itemrecord, Arr[i].catagory, Arr[i].quantity, 
 Arr[i].weight, Arr[i].recipient, Arr[i].final_destination, Arr[i].status 
 ); 

}
}
//rewind
rewind(fileptr1);


printf(" \n\n Enter record number to be deleted <type 0 = not delete 
anything>:");
fflush(stdin);
scanf("%d", &delete_num);


//open new file in write mode
fileptr2 = fopen("copy.c", "w");
save = getc(fileptr1);
while (save != EOF)
{
    save = getc(fileptr1);
    //except the line to be deleted
    int i ;

    for (i=0;i<3;i++){

    if (temp != delete_num)
    {
        //copy all lines in file replica.c
        putc(save, fileptr2);
}}
}




fclose(fileptr1);
fclose(fileptr2);
remove(filename);
//rename the file replica.c to original name
rename("copy.c", filename);
fflush(stdout);
printf("\nThe contents of file after being changed are as follows:\n");

fileptr1 = fopen(filename, "r");
save = getc(fileptr1);
while (save != EOF)
{
    printf("%c", save);
    save = getc(fileptr1);
}
fclose(fileptr1); 

fflush(stdin);

printf("\n\n Delete anther item?<y/n>: ");
scanf("%c",&reply);  



}while(reply=='y' || reply=='Y');
return 0;
}

Sorry for my long code......

Result:

Enter file name:record.txt
10 
Enter record number to be deleted <type 0 = not delete anything>:1001
The contents of file after being changed are as follows:

0000000111
eeerrriiiccc
111
hhhuuummmaaannn
111000
777000...000
eeerrriiiccc
hhhooommmeee
aaarrrrrriiivvveee

and below result same......interesting....

It is not my expected result... i know the word appear three time because of my for loop. However I want my for loop run 3 time , first time include 9record and below..

3
  • fflush(stdin); is UB, dont do it. Commented Nov 23, 2018 at 14:21
  • @SouravGhosh I dont use it now apart from the final one , and any suggestion for me? :)) Commented Nov 23, 2018 at 14:37
  • file not exist is potentially a very confusing error message if the file exists but was unable to be opened for some other reason. Don't try to guess the reason; let the system tell you why fopen failed. if( (fileptrt1 = fopen(filename, "r")) == NULL) { perror(filename); exit(EXIT_FAILURE);} Commented Nov 24, 2018 at 4:25

1 Answer 1

1

You're using getc to read characters from the file, but then discarding them. So you're likely to lose meaningful characters. You're also not checking the return value of fscanf to see if it succeeded, leaving the possibility of getting errors without realizing it. You can fix both by getting rid of the getc calls and using the return value of fscanf to control your loop:

while (fscanf(fileptr1,"%39s%39s%39s%39s%39s%39s%39s%39s%39s",
              Arr[i].recordnum, Arr[i].itemname, Arr[i].itemrecord,
              Arr[i].catagory, Arr[i].quantity, Arr[i].weight,
              Arr[i].recipient, Arr[i].final_destination, Arr[i].status) == 9) {
    if (++i >= MAX)
        break;
}

Then, to write the file, you want to delete the record requested, not just a single line. So your best bet is to (re)write the file from scratch using the data you previously read into Arr and leaving out the record you want to delete.

Sign up to request clarification or add additional context in comments.

4 Comments

I mean using the data I previously read into Array and leaving out the record I want to delete. @Chris Dodd
for(i=0;i<total;i++) then..if (Arr[i].recordnum != delete_num){ fprintf(fileptr2,"%s %s %s %s %s %s %s %s %s",Arr[i].recordnum, Arr[i].itemname, Arr[i].itemrecord, Arr[i].catagory, Arr[i].quantity, Arr[i].weight, Arr[i].recipient, Arr[i].final_destination, Arr[i].status )`
@HangWui: exactly, though since your recordnum is a string rather than an integer, you'll need to use strcmp rather than !=
you mean if(strcmp(Arr[i].recordnum,delete_num)==0) ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.