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

时间:2014-11-18 20:54:37

标签: 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

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

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

2 个答案:

答案 0 :(得分:0)

首先,您是否确定无法在C ++(ifstream)(getline)中执行此操作?

其次,如果必须在C中执行,那么您应该查看该文件并计算每行的行数和逗号数。获得总number of line in the file

然后,您可以逐行重复该文件并计算逗号的数量(如果您的问题中每行上不总是有4个数字)。

答案 1 :(得分:0)

(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很棒,而且每个问题都是正确的语言。永远。