简体   繁体   English

读取文本文件,复制到C中的数组

[英]reading text file, copying into array in C

The code is supposed to read a user-inputted text file name, copy every character into a multidimensional array, then display it with standard output. 该代码应该读取用户输入的文本文件名,将每个字符复制到多维数组中,然后以标准输出显示它。 It compiles, but produces unintelligible text. 它可以编译,但是会产生难以理解的文本。 Am I missing something? 我想念什么吗?

    for (i = 0; i < BIGGEST; i++) {
        for (j = 0; j < BIGGESTL; j++) {
            if (fgetc(array, fp) ) != EOF)
                array[i][j] = c;
            else array[i][j] = '\0'


        }

    fclose(fp);    
    return 0;
}

You stop filling the array when you encounter EOF, but you print the full array out no matter what. 当遇到EOF时,您将停止填充数组,但是无论如何您都将打印完整的数组。

If the data read from the file is smaller than the input array, you will read that data in and then print that data out, plus whatever random characters were in the memory locations that you do not overwrite with data from the file. 如果从文件中读取的数据小于输入数组,则将读取该数据,然后将其打印输出,再加上不被文件数据覆盖的内存位置中的任何随机字符。

Since the requirement seems to be to print text data, you could insert a special marker in the array (eg '\\0') to indicate the position where you encountered EOF, and stop displaying data when you reach that marker. 由于要求似乎是打印文本数据,因此您可以在数组中插入一个特殊标记(例如'\\ 0')以指示遇到EOF的位置,并在到达该标记时停止显示数据。

You had better read each line from file 您最好从文件中读取每一行

For example: 例如:

int i = 0;
while(fgets(text[i],1000,fp))
{
    i++;
}

Though the question is edited and only part of the code is left in question. 尽管问题已被编辑,但仅剩下部分代码有问题。 I am posting more than what is required for the question at the moment. 我目前发布的内容超出了问题的要求。 Reason being, there can be numberous improvements to originally posted full code. 原因是,最初发布的完整代码可能会有很多改进。

In main() function: main()函数中:

You need to check for the argc value to be equal to 2 for your purpose and only then read in value of argv[1] . 您需要检查argc值是否等于2 ,然后才读入argv[1]值。 Else if program executed without the command-line-argument which is file_name in this case, invalid memory read occurs, resulting in segmentation fault if you read in argv[1] . 否则,如果在没有命令行参数(file_name)的情况下执行程序,则会发生无效的内存读取,如果您读入argv[1]则会导致分段错误。

In read_file_and_show_the contents() function: read_file_and_show_the contents()函数中:

Stop reading file if end of file is reached or maximum characters is read and store in the character array. 如果到达文件末尾或已读取最大字符并将其存储在字符数组中,则停止读取文件。

Below Program will help you visualize: 以下程序将帮助您可视化:

#include <stdio.h>

/*Max number of characters to be read/write from file*/
#define MAX_CHAR_FOR_FILE_OPERATION 1000000 

int read_and_show_the_file(char *filename)
{  
   FILE *fp;
   char text[MAX_CHAR_FOR_FILE_OPERATION];
   int i;

   fp = fopen(filename, "r");

   if(fp == NULL)
   {
      printf("File Pointer is invalid\n");
      return -1;
   }
   //Ensure array write starts from beginning
   i = 0;

   //Read over file contents until either EOF is reached or maximum characters is read and store in character array
   while( (fgets(&text[i++],sizeof(char)+1,fp) != NULL) && (i<MAX_CHAR_FOR_FILE_OPERATION) ) ;

   //Ensure array read starts from beginning
   i = 0;

   while((text[i] != '\0') && (i<MAX_CHAR_FOR_FILE_OPERATION) )
   {
      printf("%c",text[i++]);
   }

   fclose(fp);

   return 0;
}

int main(int argc, char *argv[])
{
   if(argc != 2)
   {
      printf("Execute the program along with file name to be read and printed. \n\
              \rFormat : \"%s <file-name>\"\n",argv[0]);
      return -1;
   }

   char *filename = argv[1];

   if( (read_and_show_the_file(filename)) == 0)
   {
      printf("File Read and Print to stdout is successful\n");
   }
   return 0;
}  

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

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