C 编程——如何将文本文件内容放入我的数组中?

标签 c arrays struct file-io

我正在尝试编写一个程序来删除文件 io 中的记录。文件内容默认设置。现在我正在尝试将我的文本文件内容放入数组中,但是出现了一些问题....

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

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

和下面的结果相同......有趣......

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

最佳答案

您正在使用 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 的数据从头开始(重新)写入文件,并忽略要删除的记录。

关于C 编程——如何将文本文件内容放入我的数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53448384/

相关文章:

c - 如何使用 getpwuid_r() 正确设置缓冲区和 bufsize?

c - C语言中如何处理小数点后10位以上的INT?

c - 特定情况下的段错误

arrays - 如何使用for循环遍历2D数组

javascript - 不带参数的 Javascript 数组函数

c++ - 如何将数组传递给构造函数并保存在类变量中

类对象初始化内部的c++指针(指向数组)

c - 从函数返回一个字符串到 main

c++ - Excel 64 位中的 C 函数不能用作工作表函数,但在 VBA 中运行良好

c++ - 数组在*物理*内存中是连续的吗?