c - 在 C 中使用 fscanf 将文本文档的内容扫描到数组中

标签 c arrays scanf

我需要扫描文本文档的内容,即;

77, 66, 80, 81
40,  5, 35, -1
51, 58, 62, 34
 0, -1, 21, 18
61, 69, 58, 49
81, 82, 90, 76
44, 51, 60, -1
64, 63, 60, 66
-1, 38, 41, 50
69, 80, 72, 75

放入一个数组中,每个数字都在自己的 block 中,然后读取每个 block 以识别内容是什么。我觉得我已经完成了处理部分,但我无法弄清楚如何将数字分配给数组中的 block 。这是我到目前为止所拥有的;

int main()
{
    FILE * marks;
    marks = fopen("marks.txt", "r");
    int marksArray[4][10], x, y, greaterThan70 = 0, between60and69 = 0, between50and59 = 0, between40and49 = 0, lessThan39 = 0, notSubmitted = 0;
    //Scanning in the contents
    while ((fscanf(marks, "%d", &marksArray[x][y])) != EOF)
    {
        x++;
        y++;
    }
    //Processing the array
    for(x = 0; x < 4; x++)
    {
        for(y = 0; y < 10; y++)
        {
            if(marksArray[x][y] == -1)
            {
                notSubmitted++;
            }
            else if(marksArray[x][y] >= 0 && marksArray[x][y] <= 39)
            {
                lessThan39++;
            }
            else if(marksArray[x][y] >= 40 && marksArray[x][y] <= 49)
            {
                between40and49++;
            }
            else if(marksArray[x][y] >= 50 && marksArray[x][y] <= 59)
            {
                between50and59++;
            }
            else if(marksArray[x][y] >= 60 && marksArray[x][y] <= 69)
            {
                between60and69++;
            }
            else
            {
                greaterThan70++;
            }
            break;
        }
    }
    printf("The number of marks greater than 70 was %d", greaterThan70);
    printf("The number of marks between than 60 and 69 was %d", between60and69);
    printf("The number of marks between than 50 and 59 was %d", between50and59);
    printf("The number of marks between than 40 and 49 was %d", between40and49);
    printf("The number of marks less than 39 was %d", lessThan39);
    printf("The number of coursework submissions not handed in was %d", notSubmitted);
}

最佳答案

(fscanf(marks, "%d", &marksArray[x][y])

第一个逗号被哽住了。你会想要更多类似的东西:

(fscanf(marks, "%d, ", &marksArray[x][y])

逗号和空格(也会占用新行)很重要。只是你的最后一个字符不以逗号结尾,所以这不太有效。

是的,还有这个:

while ((fscanf(marks, "%d", &marksArray[x][y])) != EOF)
{
    x++;
    y++;
}

如果它有效,它将按照以下模式遍历数组:

X000
0X00
00X0
000X

你想要的更像是:

#include <stdio.h>

int main()
{
FILE * marks;
marks = fopen("in.txt", "r");
int marksArray[4][10], x, y, greaterThan70 = 0, between60and69 = 0, between50and59 = 0, between40and49 = 0, lessThan39 = 0, notSubmitted = 0;
//Scanning in the contents
//No need for EOF if you know the size ahead of time
for(y=0; y<10; y++)
{
  for(x=0; x<3; x++)
  {
    fscanf(marks, "%d, ", &marksArray[x][y]);
  }      
  //To deal with the annoying lack of comma at the end of the line
  //Be wary of EoL variance, the difference between \n and \r\n, bloody windows...
  fscanf(marks, "%d\n", &marksArray[x][y]);
}

//Processing the array
for(x = 0; x < 4; x++)
{
    for(y = 0; y < 10; y++)
    {
        if(marksArray[x][y] == -1)
        {
            notSubmitted++;
        }
        else if(marksArray[x][y] >= 0 && marksArray[x][y] <= 39)
        {
            lessThan39++;
        }
        else if(marksArray[x][y] >= 40 && marksArray[x][y] <= 49)
        {
            between40and49++;
        }
        else if(marksArray[x][y] >= 50 && marksArray[x][y] <= 59)
        {
            between50and59++;
        }
        else if(marksArray[x][y] >= 60 && marksArray[x][y] <= 69)
        {
            between60and69++;
        }
        else
        {
            greaterThan70++;
        }
        break;
    }
}
printf("The number of marks greater than 70 was %d\n", greaterThan70);
printf("The number of marks between than 60 and 69 was %d\n", between60and69);
printf("The number of marks between than 50 and 59 was %d\n", between50and59);
printf("The number of marks between than 40 and 49 was %d\n", between40and49);
printf("The number of marks less than 39 was %d\n", lessThan39);
printf("The number of coursework submissions not handed in was %d\n", notSubmitted);
}

别听那个人的胡言乱语,C 非常棒,而且是解决所有问题的正确语言。曾经。

关于c - 在 C 中使用 fscanf 将文本文档的内容扫描到数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27003832/

相关文章:

c - 如何从.txt文件中读取整数并将它们存储在C中的整数类型数组中

c - 关于签名 : int WINAPI WinMain (HINSTANCE p1, HINSTANCE p2, LPSTR p3, int p4)

c - 如何在 c 中生成返回原始输出的 SHA1?

c - 如何使用 scanf\fscanf 读取一行并解析成变量?

arrays - 如何在 Swift 中将正确位置的元素插入到排序数组中?

java - 将数组与子类链接

c - 使用 scanf 读取无符号字符

将元素从数组复制到另一个数组并计算C中字符的数量

c++ - 如何仅在C/C++项目中处理宏而不进行编译?

arrays - PostgreSQL 数组数据类型到 JSON 对象