C编程-如何将文本文件内容放入Array?

时间:2018-11-23 14:19:45

标签: c arrays struct file-io

我正在尝试编写一个程序,该程序可以删除文件io中的记录。文件内容是默认设置的。现在,我正在尝试将文本文件内容放入Array,但是发生了一些麻烦。...

首先,这是我的默认数据文件内容:

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

这是我的代码:(我正在使用fscanf将我的文本文件数据放入数组,您可以在打开的文件中间看到它供读取)

#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;
}

抱歉,我的长代码......

结果:

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

及以下结果相同……有趣。...

这不是我的预期结果...我知道这个单词出现了3次,因为我的for循环。但是我希望我的for循环运行3次,第一次包括9record及以下。.

1 个答案:

答案 0 :(得分:2)

您正在使用getc从文件中读取字符,然后将其丢弃。因此,您可能会失去有意义的角色。您也没有检查fscanf的返回值是否成功,从而留下了在未意识到的情况下出错的可能性。您可以通过摆脱getc调用并使用fscanf的返回值来控制循环来解决这两个问题:

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;
}

然后,要写入文件,您要删除请求的记录,而不仅仅是一行。因此,最好的选择是使用先前读入Arr的数据从头开始(重新)写入文件,并保留要删除的记录。