简体   繁体   English

C将文件中的输入读取到2d数组中-每行的行数可变

[英]C reading input from a file into 2d array - variable number of lines per row

I am reading a list of grades from a txt file into an array. 我正在将成绩列表从txt文件读入数组。 It worked fine when reading user input, but I'm having trouble reading each line when scanning from file. 读取用户输入时,它工作正常,但是从文件扫描时,读取每行时遇到问题。 The number of students is variable. 学生人数是可变的。 The number of grades per student is variable. 每个学生的成绩数是可变的。 I have no trouble reading the number of students and number of assignments, but when reading from file I'm having trouble pulling the int (grade) from each line for each student. 我可以很轻松地读取学生人数和作业数量,但是从文件中读取时,我很难为每个学生从每一行中提取int(成绩)。 The input may be like a or b (or any larger number of students/assignments): 输入内容可能类似于a或b(或更多的学生/作业):

txt-example1 (the comments including and after // are my own and not in txt file) txt-example1(包括//之后的注释是我自己的,不在txt文件中)

2  //number of students
3  //the number of grades per student (will match the number of grade rows below)
theo  alvin //the number of names will match the number of students
75 60
89 90
79 95

txt-example2 txt-example2

3
4
theo alvin simon 
78 85 90
85 96 76 
77 99 100
88 55 92

I can put the names into 1 dimension of a 2d array (I'll use the second dimension later to print - no problems with that part). 我可以将名称放入2d数组的1维中(稍后将使用第二维进行打印-该部分没有问题)。 I want to get the grades into a 2d array. 我想将成绩分为2D数组。 Here is what I have 这是我所拥有的

#include<stdio.h>
#include<string.h>

int numStus;
int numGrades;

int main()
{
   FILE* inputFile;
   char stuNames[numStus][10];
   int grades[numGrades][numStus];

   inputFile = fopen("testData.txt","r"); //assume inputFile has data from Ex 1 or 2 above

   fscanf(inputFile,"%d",&numStus);

   fscanf(inputFile,"%d",&numGrades);

   int i;
   int j;

   for (i=0; i<numStus; i++)
   {
      fscanf(inputFile,"%s",&stuNames[i]);
   }

   //here is where I'm having trouble

      for(i=0;i<numGrades;i++)
   {
      for(j=0;j<numStus; j++)
      {
        //I want to use fscanf, but don't know how to account for carriage returns to iterate into next part of array
      }   
   }

}  

What worked when getting from user input: 从用户输入获取的结果是:

int i;
int j;
int k;

for (i=0; i<numGrades; i++)
{
   for (j=0; j<numStus; j++)
   {
      printf("Enter grade for Assignemnt %d for ",i)

      for(k=0;k<10;k++)
      {
         printf("%c",stuNames[j][k]);
      }
      scanf("%d",&grades[i][j]);
   }
}

The part immediately above worked well for user input grades. 紧接在上的部分对于用户输入等级非常有效。 When getting the same input from a file I'm not sure how to get the grades into the proper dimensions. 当从文件中获得相同的输入时,我不确定如何将等级转换为正确的尺寸。 Any advice on how to account for the newline/CR to increment the array would be very much appreciated. 非常感谢有关如何考虑换行符/ CR来增加数组的任何建议。 Thanks. 谢谢。

The scanf can be used in the nested loops as usually for reading a single value, since the carrige return is skipped as a space, for example: fscanf(inputFile,"%d",&grades[i][j]); 可以在嵌套循环中使用scanf ,就像通常读取单个值一样,因为carrige返回被跳过为空格,例如: fscanf(inputFile,"%d",&grades[i][j]);

However, the arrays stuNames and grades must be initialized only after reading numStus and numGrades , for example: 但是,只有在读取numStusnumGrades ,才必须初始化数组stuNamesgrades ,例如:

...
fscanf(inputFile,"%d",&numStus);
char stuNames[numStus][10];

fscanf(inputFile,"%d",&numGrades);
int grades[numGrades][numStus];
...

That trick is not allowed in ANSI C. In that case dynamic memory allocation should be used. ANSI C中不允许使用该技巧。在这种情况下,应使用动态内存分配。

You have trouble much earlier than you think. 您比您想的要早得多。 You can't use uninitialized variables when you are declaring your arrays. 在声明数组时,不能使用未初始化的变量。 In your working example you don't show how you allocated your arrays. 在您的工作示例中,您没有显示如何分配数组。 Were they fixed size? 它们是固定大小的吗? Whatever you did there would probably work in your new program. 无论您做什么,都可能会在新程序中工作。

You first need to read the amount of students and amount of grades. 您首先需要阅读学生数量和成绩数量。

A uninitialized variable like int foo; 未初始化的变量,例如int foo; has a "random" value. 具有“随机”值。 You need to initialize the variable like int foo = 0; 您需要初始化变量,例如int foo = 0; .

Knowing this lets analyze your code step by step. 知道这一点后,就可以逐步分析代码。

int numStus; //numStus gets random value
int numGrades; //numGrades gets random value

int main()
{
   FILE* inputFile;
   char stuNames[numStus][10]; //random amount gets generated
   int grades[numGrades][numStus]; //variable gets random value
   fscanf(inputFile,"%d",&numStus); //numStus gets actual value
   fscanf(inputFile,"%d",&numGrades); //numGrades gets actual value

C is a language which handles things in order. C是一种按顺序处理事物的语言。 This means that the array keeps the size is has been given. 这意味着数组的大小已给出。

The code can be fixed by switching around the statements like this: 可以通过切换以下语句来固定代码:

int numStus; //numStus gets random value
int numGrades; //numGrades gets random value

int main()
{
   FILE* inputFile;
   fscanf(inputFile,"%d",&numStus); //numStus gets actual value
   fscanf(inputFile,"%d",&numGrades); //numGrades gets actual value
   char stuNames[numStus][10]; //array of wanted size gets created
   int grades[numGrades][numStus]; //random amount gets

I hope this helps, may you have any questions ask them 希望对您有所帮助,如果您有任何问题要问他们

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

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