Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图在C语言中从文件中写入和获取数据,但只能无限地获取最后一个数据(编码新手)_C_File - Fatal编程技术网

试图在C语言中从文件中写入和获取数据,但只能无限地获取最后一个数据(编码新手)

试图在C语言中从文件中写入和获取数据,但只能无限地获取最后一个数据(编码新手),c,file,C,File,我有一项任务,要求我在取回数据(账号、名称和余额)并打印后,首先将数据写入文件。 我得到了写作部分,但我不能得到所有的数据,我只得到最后一个无限次。我怎样才能一个接一个地得到这些 (为糟糕的ofc英语道歉) 我的代码 #include <stdio.h> int main() { int account; char name[50]; double balance; FILE *cfPtr; char str[100];

我有一项任务,要求我在取回数据(账号、名称和余额)并打印后,首先将数据写入文件。 我得到了写作部分,但我不能得到所有的数据,我只得到最后一个无限次。我怎样才能一个接一个地得到这些

(为糟糕的ofc英语道歉)

我的代码

#include <stdio.h>

int main()
{
    int account;
    char name[50];
    double balance;

        FILE *cfPtr;
        char str[100];

        if((cfPtr=fopen("clients.dat","w"))==NULL)
        {
            printf("File could not be opened\n");
        }
        else
        {
        printf("Enter the account, name and balance.\n");
        printf("Enter EOF to end input.\n");
        printf("?");
        scanf("%d %s %lf", &account, name, &balance );
        while (!feof(stdin))
            {
            fprintf(cfPtr, "%d %s %.2f \n", account, name, balance);
            printf("?");
            scanf("%d %s %lf", &account, name, &balance);
            }
            fclose(cfPtr);
        }     
        rewind(cfPtr);
        while(!feof(cfPtr))
        {
        fscanf(cfPtr,"%d %s %lf",&account, name, &balance);
        printf("%d %s %.2f",account,name,balance);      
        }
        return 0;
}




#包括
int main()
{
国际账户;
字符名[50];
双平衡;
文件*cfPtr;
char-str[100];
if((cfPtr=fopen(“clients.dat”,“w”))==NULL)
{
printf(“无法打开文件\n”);
}
其他的
{
printf(“输入帐户、名称和余额。\n”);
printf(“输入EOF以结束输入。\n”);
printf(“?”);
scanf(“%d%s%lf”、&帐户、名称和余额);
而(!feof(stdin))
{
fprintf(cfPtr,“%d%s%.2f\n”,科目、名称、余额);
printf(“?”);
scanf(“%d%s%lf”、&帐户、名称和余额);
}
fclose(cfPtr);
}     
倒带(cfPtr);
而(!feof(cfPtr))
{
fscanf(cfPtr,“%d%s%lf”&帐户、名称和余额);
printf(“%d%s%.2f”,科目、名称、余额);
}
返回0;
}

和编译

如果你想读写文件,你应该打开它进行读写,例如:

fopen("clients.dat","w+"));  // notice "w+"
如果计划在关闭后执行任何操作,则不应关闭文件,在这种情况下是错误的:

    while(...)
    {
        ... 
        printf("?");
       scanf("%d %s %lf", &account, name, &balance);
    }
    fclose(cfPtr);       
   // you can't do any read-write operation with the file after fclose();

我通过为每个数据输入添加计数器解决了这个问题,并循环了最后一个fscanf

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int account;
    char name[50];
    double balance;
    int acc;
    char nam[50];
    double bal;
    int counter=0;
    int i;

        FILE *cfPtr;

cfPtr=fopen("clients.dat","w+");
                                            printf("Enter the account, name and balance.\n");
                                            printf("Enter EOF to end input.\n");
                                             printf("?");
        scanf("%d %s %lf", &account, name, &balance );
        while (!feof(stdin))
            {
            fprintf(cfPtr, "%d %s %.2f \n", account, name, balance);
            printf("?");
            scanf("%d %s %lf", &account, name, &balance);
            counter++;
            }

            rewind(cfPtr);
                    printf("AccountNum       Name    Balance\n");
        for(i=0; i<counter; i++) {
            fscanf(cfPtr, "%d %s %lf", &acc, nam, &bal);
            printf("%10d %10s %10.2lf\n", acc, nam, bal);
        }
        fclose(cfPtr);
        return 0;
}

#包括
#包括
int main()
{
国际账户;
字符名[50];
双平衡;
国际会计准则;
查南[50];
双bal;
int计数器=0;
int i;
文件*cfPtr;
cfPtr=fopen(“clients.dat”,“w+”);
printf(“输入帐户、名称和余额。\n”);
printf(“输入EOF以结束输入。\n”);
printf(“?”);
scanf(“%d%s%lf”、&帐户、名称和余额);
而(!feof(stdin))
{
fprintf(cfPtr,“%d%s%.2f\n”,科目、名称、余额);
printf(“?”);
scanf(“%d%s%lf”、&帐户、名称和余额);
计数器++;
}
倒带(cfPtr);
printf(“AccountNum名称余额”);
对于(i=0;i