c - 如何将从文件中获取的 2-3 位数字放入数组中?

标签 c arrays

<分区>

main ()
{
        FILE *pRead;
        FILE *pWrite;
        char lastname [20];
        char firstname [20];
        char grade [4];
        char gradefi [4];
        char gradeltr [3];
        int i=0;

        pRead=fopen ("testtext.dat", "r");

        if (pRead == NULL || pWrite == NULL)
                printf ("\nFile cannot be opened\n");
        else
                printf ("\nContents of testtext.dat\n\n");
                fscanf (pRead, "%[^,]%[^\n]%s", lastname, firstname, &grade);
                printf ("\n");
                pWrite=fopen ("report.txt", "w");
                fprintf (pWrite, "%s %s %s\n", firstname, lastname, &grade);
        while (!feof(pRead))
                {
                        ++i;
                        printf ("%s %s %s\n", firstname, lastname, &grade);
                        fscanf(pRead, "%[^,]%[^\n]%s", lastname, firstname, &grade);
                        fprintf (pWrite, "%s %s %s\n", firstname, lastname, &grade);
                }

fclose(pRead);
fclose(pWrite);

printf ("\nThere are %d student(s) in this list\n", i);

最佳答案

好吧,不要让自己看起来像个陈腐的偏执狂放屁或其他任何东西,但我建议您在 else 之后的后续语句中加上一些大括号,看看会发生什么。

关于c - 如何将从文件中获取的 2-3 位数字放入数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5690808/

相关文章:

java - 将字符串转换为字符数组

objective-c - 如何将浮点值数组写入 Core Audio 中的音频文件?

arrays - VBA 中的数组公式错误

c - 停留在 while 循环中

c - 带空参数的函数

javascript - 使用lodash的extend方法扩展JS对象实例会导致奇怪的结果

java - 尝试在 Java 中搜索 JSON

c - C 的 pow() 函数如何在不损失任何精度的情况下输出 2^1023 的精确值?

c - 独立于语言环境的 strtod 实现

c - javacc C 语法和 C "Bit fields";解析异常