将文本文件内容复制到C中的2D数组中

时间:2022-05-12 06:34:40

I want to copy the content of a file that has a 10 by 10 array of characters to a 2D array. I verified but the compiler gives me an empty screen. The file is opening since the program dosen't print: "error in opening file", i would appreciate it if you help me. This is my code:

我想将具有10 x 10字符数组的文件的内容复制到2D数组。我验证了,但编译器给了我一个空屏幕。由于程序不打印,文件正在打开:“打开文件时出错”,如果你帮助我,我将不胜感激。这是我的代码:

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

int main()
{
   char puzzle[10][10], line[10];
   int i,j, rows=0;
   FILE *fp;
   fp=fopen("arr.txt", "r");
   if (fp== NULL) { 
      printf("Error in opening file\n");
      return 1;
   }

   while(!EOF)
   {
      if(fgets(line, 10, fp) != NULL){
         for(i=0; i<(strlen(line)-1); i++)
            puzzle[rows][i] = line[i];
      }
      rows++;   
   }
   for(i=0; i<10; i++)
      for(j=0; j<10; j++)
         printf("%c", puzzle[i][j]);
}

Thank you

谢谢

2 个解决方案

#1


1  

You reading-loop condition will always be false, so you never read anything. This will led to undefined behavior when you print out the uninitialized puzzle matrix.

你的阅读循环条件总是错误的,所以你永远不会读任何东西。当您打印出未初始化的拼图矩阵时,这将导致未定义的行为。

The macro EOF is defined as -1, and in C anything non-zero is true, and since you negate this "true" value through the ! operator your condition will be false.

宏EOF定义为-1,而在C中任何非零都是真的,因为你通过!否定这个“真”值。操作员您的情况将是错误的。

The usual newbie way to do it is using

通常的新手方式是使用

while (!feof(fp)) { ... }

but that's the wrong solution (in most cases).

但这是错误的解决方案(在大多数情况下)。

Instead do

相反

while (fgets(line, sizeof line, fp) != NULL) { ... }

By the way, you might still have undefined behavior if the line you read is less than 9 characters, as not all of the puzzle matrix will be initialized. You might want to initialize it first, which can be done when you define it:

顺便说一下,如果您读取的行少于9个字符,则可能仍然存在未定义的行为,因为并非所有拼图矩阵都将被初始化。您可能希望首先对其进行初始化,这可以在您定义时完成:

char puzzle[10][10] = { 0 };

Also note that if the lines you read are exactly 9 characters or longer, then the fgets call will not include the newline in the string.

另请注意,如果您阅读的行正好是9个字符或更长,那么fgets调用将不包括字符串中的换行符。

#2


1  

You are using the wrong size for the arrays.

您使用的数组大小错误。

When a line has 10 characters, you need to have

当一行有10个字符时,您需要拥有

// 10 characters, the newline and the terminating null character.
char line[12];
char puzzle[10][12];

#1


1  

You reading-loop condition will always be false, so you never read anything. This will led to undefined behavior when you print out the uninitialized puzzle matrix.

你的阅读循环条件总是错误的,所以你永远不会读任何东西。当您打印出未初始化的拼图矩阵时,这将导致未定义的行为。

The macro EOF is defined as -1, and in C anything non-zero is true, and since you negate this "true" value through the ! operator your condition will be false.

宏EOF定义为-1,而在C中任何非零都是真的,因为你通过!否定这个“真”值。操作员您的情况将是错误的。

The usual newbie way to do it is using

通常的新手方式是使用

while (!feof(fp)) { ... }

but that's the wrong solution (in most cases).

但这是错误的解决方案(在大多数情况下)。

Instead do

相反

while (fgets(line, sizeof line, fp) != NULL) { ... }

By the way, you might still have undefined behavior if the line you read is less than 9 characters, as not all of the puzzle matrix will be initialized. You might want to initialize it first, which can be done when you define it:

顺便说一下,如果您读取的行少于9个字符,则可能仍然存在未定义的行为,因为并非所有拼图矩阵都将被初始化。您可能希望首先对其进行初始化,这可以在您定义时完成:

char puzzle[10][10] = { 0 };

Also note that if the lines you read are exactly 9 characters or longer, then the fgets call will not include the newline in the string.

另请注意,如果您阅读的行正好是9个字符或更长,那么fgets调用将不包括字符串中的换行符。

#2


1  

You are using the wrong size for the arrays.

您使用的数组大小错误。

When a line has 10 characters, you need to have

当一行有10个字符时,您需要拥有

// 10 characters, the newline and the terminating null character.
char line[12];
char puzzle[10][12];