简体   繁体   English

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

[英]Using fscanf to scan contents of a text document into an array in C

I need to scan the contents of a text document, namely; 我需要扫描文本文档的内容,即;

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

into an array, with each number in its own block, and then read each block to identify what the contents are. 放入一个数组,每个数字都在其自己的块中,然后读取每个块以标识内容是什么。 I feel like I have the processing part complete, but I can't work out how to allocate a number to a block in the array. 我觉得我已经完成了处理部分,但是我不知道如何为数组中的块分配数字。 Here is what I have so far; 这是我到目前为止所拥有的;

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

First, are you really sure that you can't do this in C++ ( ifstream )( getline ) ? 首先,您真的确定不能在C ++( ifstream )( getline )中做到这一点吗?

Secondly, if you must do it in C, then you should look into the file and count the number of lines and the number of comma on each line. 其次,如果必须在C中执行此操作,则应查看文件并计算行数和每行的逗号数。 To get the total number of line in the file 获取文件中的总行

You can then iterate again the file, line by line and count the number of comma ( if there is not always 4 numbers on each line like in your question ). 然后,您可以逐行再次迭代文件并计算逗号的数量(如果您的问题中每行并不总是有4个数字)。

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

Chokes on the first comma. 扼住第一个逗号。 You'll want something more like: 您将需要更多类似的东西:

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

The comma and white space (which also eats new lines) are important. 逗号和空格(也占用换行符)很重要。 Except that your last character doesn't end with a comma, so that won't quite work. 除了您的最后一个字符没有以逗号结尾之外,否则将无法正常工作。

Yeah, and this: 是的,这:

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

If it worked, it would walk through the array in this pattern: 如果可行,它将按照以下模式遍历数组:

X000
0X00
00X0
000X

What you want is something more like: 您想要的更像是:

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

And don't listen to that guy C is awesome and the right language for every problem. 而且不要听那个家伙C真棒,并且每个问题都适合使用这种语言。 Ever. 曾经

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM