简体   繁体   English

c-将文件放入字符数组

[英]c - get file into array of chars

hi i have the following code below, where i try to get all the lines of a file into an array... for example if in file data.txt i have the following: 嗨,我在下面有以下代码,在这里我尝试将文件的所有行放入数组...例如,如果在文件data.txt中,我具有以下内容:

first line 第一行

second line 第二行

then in below code i want to get in data array the following: data[0] = "first line"; 然后在下面的代码中,我想在数据数组中获得以下数据:data [0] =“ first line”; data[1] = "second line" data [1] =“第二行”

My first question: Currently I am getting "Segmentation fault"... Why? 我的第一个问题:目前,我遇到了“细分错误” ...为什么?

Exactly i get the following output: 确实我得到以下输出:

Number of lines is 7475613 行数是7475613

Segmentation fault 分段故障

My second question: Is there any better way to do what i am trying do? 我的第二个问题:是否有更好的方法来做我想做的事情?

Thanks!!! 谢谢!!!

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

int main(int argc, char* argv[])
{
 FILE *f = fopen("data.txt", "rb");
 fseek(f, 0, SEEK_END);
 long pos = ftell(f);
 fseek(f, 0, SEEK_SET);

 char *bytes = malloc(pos);
 fread(bytes, pos, 1, f);

 int i =0;
 int counter = 0;
 for(; i<pos; i++)
 {
  if(*(bytes+i)=='\n') counter++;
 }
 printf("\nNumber of lines is %d\n", counter);

 char* data[counter];
 int start=0, end=0;
 counter = 0;
 int length;

 for(i=0; i<pos; i++)
 {
  if(*(bytes+i)=='\n')
  {
   end = i;
   length =end-start;
   data[counter]=(char*)malloc(sizeof(char)*(length));
   strncpy(data[counter],
           bytes+start,
           length);
   counter = counter+1;
   start = end+1;
  }
 }

 free(bytes);
 return 0;
}

First line of the data.txt in this case is not '\\n' it is: "23454555 6346346 3463463". 在这种情况下,data.txt的第一行不是'\\ n',而是:“ 23454555 6346346 3463463”。

Thanks! 谢谢!

  • You need to malloc 1 more char for data[counter] for the terminating NUL. 您需要为终结点NUL的data[counter]多分配1个char。
  • after strncpy , you need to terminate the destination string. strncpy之后,您需要终止目标字符串。

Edit after edit of original question 编辑原始问题后进行编辑

Number of lines is 7475613 行数是7475613

Whooooooaaaaaa, that's a bit too much for your computer! Whooooooaaaaaa,这对您的计算机来说太过分了! If the size of a char * is 4, you want to reserve 29902452 bytes (30M) of automatic memory in the allocation of data . 如果char *的大小为4,则要在分配data保留29902452字节(30M)的自动内存。

You can allocate that memory dynamically instead: 您可以改为动态分配该内存:

/* char *data[counter]; */
char **data = malloc(counter * sizeof *data);

/* don't forget to free the memory when you no longer need it */

Edit: second question 编辑:第二个问题

My second question: Is there any better way to do what i am trying do? 我的第二个问题:是否有更好的方法来做我想做的事情?

Not really; 并不是的; you're doing it right. 你做对了。 But maybe you can code without the need to have all that data in memory at the same time. 但是也许您可以进行编码而无需将所有数据同时存储在内存中。
Read and deal with a single line at a time. 一次读取并处理一行。

You also need to free(data[counter]); 您还需要 free(data[counter]); in a loop ... and free(data); 循环...并free(data); before the "you're doing it right" above is correct :) 在上述“您做对了”之前是正确的:)

And you need to check if each of the several malloc() calls succeeded LOL 并且您需要检查几个malloc()调用中的每一个是否成功大声笑

First of all you need to check if the file got opened correctly or not: 首先,您需要检查文件是否正确打开:

FILE *f = fopen("data.txt", "rb");
if(!f)
{
    fprintf(stderr,"Error opening file");
    exit (1);
}

If there is error opening the file and you don't check it, you'll get a seg fault when you try to fseek on an invalid file pointer. 如果打开文件时出现错误而您没有检查它,则当您尝试查找无效的文件指针时,将会出现段错误。

Apart from that I see no errors. 除此之外,我没有看到任何错误。 Tried running the program, by printing the value of the data array at the end, it ran as expected. 尝试运行该程序,方法是在最后打印数据数组的值,使其按预期运行。

要注意的一件事是,您以二进制格式打开文件-行终止规则可能无法在您的平台上正常运行(UNIX为lf,Windows为cr-lf,某些MacOS版本为cr)。

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

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