簡體   English   中英

逐個字符讀取文件的行到char **數組中

[英]reading file`s lines char by char into char** array

我編寫了下一個函數,嘗試讀取文本文件中的每一行並將其輸入到c中的字符串數組中:

int main(int argc,char* argv[])
{

        char ** lines;
        readFile(argv[1],lines);

}
int readFile(char* filePath,char** lines)
    {  
    char file_char;
    int letter_in_line=0;
    int line=1;
    char* line_string=malloc(1024);
    int j=1;
    int fd=open(filePath,O_RDONLY);
    if (fd < 0)
    {
            return 0;
    }

     while (read(fd,&file_char,1) >0)
     {
             if(file_char != '\n' && file_char != '0x0')
             {
                line_string[letter_in_line] = file_char;
                letter_in_line++;                   
             }                    

             else
             {
                if(lines != NULL)
                {
                    lines=(char**)realloc(lines,sizeof(char*)*line);
                }
                else
                {
                    lines=(char**)malloc(sizeof(char*));
                }
                char* line_s_copy=strdup(line_string);
                lines[line-1]=line_s_copy;
                line++;
                letter_in_line=0;
                memset(line_string,0,strlen(line_string));

             }
             j++;
     }
     printf("cell 0 : %s",lines[0]);

     return 1;

}

我有兩個問題:

  • 1)每當代碼到達單元格0的打印位置時,我都會
    分段故障(核心轉儲)錯誤。 怎么了 ?
  • 2)如果我想查看主行中的lines數組中的更改,我應該將&lines傳遞給func並獲取char ***行作為參數?
    另外,我需要將每個'line'關鍵字替換為'* line'嗎? *我知道我可以使用fopen,fget等。出於某種原因,我決定以這種方式實現它。

有很多問題使您的代碼核心轉儲。 這里的版本與您的代碼非常相似。 我希望它能幫助您理解這一點。

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

int read_file(const char *filename, char ***result)
{

     /* open the file */
     const int fd = open(filename, O_RDONLY);
     if (fd < 0) {
      *result = NULL;
      return -1;
     }
     /* read the file characters by characters */
     char *buffer = (char *)malloc(sizeof(char) * 1024); 
     char c; 
     int column = 0; 
     int line = 0;
     *result = NULL; 

     /* for each characters in the file */
     while (read(fd, &c, 1) > 0) {
      /* check for end of line */
      if (c != '\n' && c != 0 && column < 1024 - 1) 
           buffer[column++] = c;
      else {
           /* string are null terminated in C */
           buffer[column] = 0;
           column = 0; 

           /* alloc memory for this line in result */
           *result = (char **)realloc(*result, sizeof(char *) *
                      (line + 1));

           /* duplicate buffer and store it in result */
           (*result)[line++] = strdup(buffer);
      }
     }
     free(buffer); 
     return line; 
}

int main(int argc, char *argv[])
{
     if (argc != 2) {
      fprintf(stderr, "usage: %s [filename]", argv[0]);
      return 1; 
     }

     char **lines; 
     int line_count = read_file(argv[1], &lines);
     if (line_count < 0) {
      fprintf(stderr, "cannot open file %s\n", argv[1]);
      return 1; 
     }

     for(int i=0; i < line_count; i++)
      printf("%s\n", lines[i]);

     return 0; 
}

這里是另一個版本:

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

int read_file(const char *filename, char ***result)
{
     /* init result */
     *result = NULL;

     /* open the file */
     FILE *file = fopen(filename, "r");
     if (file == NULL)
      return -1;

     /* read the file line by line */
     char *buffer = (char *)malloc(sizeof(char) * 1024);
     int line = 0; 
     while (fgets(buffer, 1024, file)) {
      *result = (char **)realloc(*result, sizeof(char *) *
                     (line + 1));
      (*result)[line++] = strdup(buffer); 
     }
     free(buffer);
     return line; 
}

int main(int argc, char *argv[])
{
     if (argc != 2) {
      fprintf(stderr, "usage: %s [filename]", argv[0]);
      return 1; 
     }

     char **lines; 
     int line_count = read_file(argv[1], &lines);
     if (line_count < 0) {
      fprintf(stderr, "cannot open file %s\n", argv[1]);
      return 1; 
     }
     for(int i=0; i < line_count; i++)
      printf("%s\n", lines[i]);

     return 0; 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM