【问题标题】:How to read text from file into Array如何将文件中的文本读入数组
【发布时间】:2015-07-23 08:27:08
【问题描述】:

我试图了解如何从文本中读取某些文件,然后将其输入到数组中以保存以供以后操作。我只是似乎无法理解。我已经尝试过结构和字符串数组,但我仍然无法掌握如何在 c 中实现它。 我已经成功地将至少一行记录到数组中,但它多次复制同一行。这是我的文本和代码的布局。

文本文件:

# A demonstration of some simple MIPS instructions

Loop: sll $t1, $s3, 2
      add $t1, $t1, $s6
      lw $t0, 0($t1)
      bne $t0, $s5, Exit
      addi $s3, $s3, 1
      j Loop
Exit: 

.c 文件:

int assem::readFile(FILE *file) //read file function for file to be read
{
//char String[200];
char lines[20];
char *ptr = lines;
const int MAX = 101;
char myString[MAX];
int i = 0;

while(fgets(&lines[i], 100, file) != NULL)
{
    i++;
}

printf("%s\n", lines);

【问题讨论】:

  • 关于这一行:'int assem::readFile(FILE *file)' class::method() 是 C++,而不是 C

标签: c string parsing


【解决方案1】:
char * fgets ( char * str, int num, FILE * stream );

fgets() 从 stream 读取字符并将它们作为 C 字符串存储到 str 直到 (num-1) 字符被读取或 换行符 或到达文件结尾,以先发生者为准。

我已经成功录制了至少一行

因为 换行 字符会使 fget 停止读取。请参阅参考资料here。

你可以使用

size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );

从文件中读取。

fread() 从stream 中读取count 元素的数组,每个元素的大小为size 字节,并将它们存储在ptr 指定的内存块中。

我编写了以下函数来读取文件并将每个字符写入缓冲区,这可能会对您有所帮助:

typedef  unsigned char BYTE;
void readFile(char *filename, BYTE* buffer) {
    FILE * pFile;
    long lSize;
    size_t result;

    pFile = fopen(filename, "rb");
    if (pFile == NULL ) {
        fputs("File error", stderr);
        exit(1);
    }

    // obtain file size:
    fseek(pFile, 0, SEEK_END);
    lSize = ftell(pFile);
    rewind(pFile);

    // copy the file into the buffer:
    result = fread(buffer, 1, lSize, pFile);
    if (result != lSize) {
        fputs("Reading error 2", stderr);
        exit(3);
    }

    /* the whole file is now loaded in the memory buffer. */
    fclose(pFile);
}

【讨论】:

    【解决方案2】:

    这是一个简单的代码,用于静态存储包含 99 个字符的 10 行(不要忘记 \0 来终止字符串)

    const size_t maxLines = 10;
    size_t currentLine = 0;
    char lines[maxLines][100]; // Storing 10 lines
    
    // Reading and storing a line from file
    while(fgets(lines[currentLine], sizeof(lines[currentLine]), file) && currentLine < maxLines)
    {
        ++currentLine;
    }
    
    // Printing stored lines
    size_t i = 0;
    while (i < currentLine)
    {
        printf("%s", lines[i]);
        ++i;
    }
    

    【讨论】:

    • 为了稳健,这一行:'while(fgets(lines[currentLine], sizeof(lines[currentLine]), file))' 应该是:'while(( fgets(lines[currentLine] , sizeof(lines[currentLine]), file)) && currentLine
    【解决方案3】:

    您需要逐个字符地读取所有文件。这会让事情变得更容易。

    #include <stdio.h>
    void main ()
    {
        int ch, size, i = 0;
        FILE *file;
        file = fopen("Text.txt", "r"); // open the file for reading
        fseek(file, 0L, SEEK_END);  // goto the end of the file
        size = ftell(file);
        rewind(file);               // goto the start of the file
        char *text = (char*)malloc(size - 20);  // the -20 is to avoid the gibberish at the end of the text file
    
        for (i = 0; i<size; i++)
        {
           ch = fgetc(file);
           text[i] = ch;
        }
        printf("%s", text);
        getchar();
    }
    

    【讨论】:

      【解决方案4】:

      感谢您的帮助> 我设法解决了如何早一点解决。这是我在终于理解了更多的 c 文字之后的实现。再次感谢!!! “计数器”跟踪文本文件中有多少行

      int assem::readFile(FILE *file) 
      {
      int i =0;
      counter = 0;                                            //this is sort of the constructor, if you will. 
                                                              //breaks down text file into line by line 
      while(fgets(myString, 101, file) != NULL)               //and stores them into "myLines array"
          {
              myLines[i] = strdup(myString);
              i++;
              counter++;
          }   
      
      return 0;
      }
      

      【讨论】:

      • 这个答案无法定义 myLines 和 myString 和 myLines 现在应该定义为 'char ** myLines = malloc(...);'
      • 感谢您的反馈。 myLines 和 myString 都在头类文件中定义
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      • 2017-07-16
      • 2013-04-11
      • 2020-05-18
      • 2021-12-19
      相关资源
      最近更新 更多