逐个字符读取文件的行到char **数组中

时间:2019-04-12 20:38:01

标签: c arrays string

我编写了下一个函数,该函数试图读取文本文件中的每一行并将其输入到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;

}

我有2个问题:

  • 1)每当代码到达单元格0的打印位置时,我都会得到
    分段故障(核心转储)错误。怎么了 ?
  • 2)如果我 要查看主行中的lines数组中的更改,我应该通过 &lines到func并获取char ***作为参数行?在
    另外,我需要将每个'line'关键字替换为'* line'吗? *我知道我可以使用fopen,fget等。出于某种原因,我决定以这种方式实现它。

1 个答案:

答案 0 :(得分:1)

有很多问题导致代码核心转储。 这里的版本与您的代码非常相似。我希望它能帮助您理解这一点。

#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; 
}