读入数组的fgets和问题 [英] fgets and problems reading into array
问题描述
我已经用这段代码了一段时间了,我似乎无法确定问题所在。
找出问题所在。最初的问题是我为某些原因或其他原因而无法分配少量80,000,000个元素阵列。通常
这不应该是一个问题,但确实如此。所以我试着做了一些calloc调用
,一切都从那里向南走了。
基本上我是想读一个大文件,坚持每一个排成一个数组,
然后从那时开始做东西。 do stuff部分应该没有
故障,如果我可以让部分读取工作。
这是代码。
#include< stdio.h>
#include< string.h>
int main()
{
int i = 0;
int j = 0;
int eof_marker = 0;
char * array [1000000];
char * out_array [1000000];
char input_line [80] ;
FILE * fp;
FILE * fp_out;
/ ************ *****************************************/
fp = fopen(" /var/tmp/cpu.out"," r");
fp_out = fopen(" /var/tmp/cpu.output2"," w" );
for(i = 0; i< 1000000; i ++)
array [i] = calloc(80,sizeof(char)); < (b)(f_(input_line,sizeof(input_line),fp)!= NULL)
{
strcat(array [i ],input_line); / *在这一行代码核心转储* /
i ++;
}
eof_marker = i;
}
请原谅M $的恶意文本解析,我的整齐缩进似乎没有完好无损地存活下来。
我不认为我理解malloc和calloc背后的逻辑。我已经阅读了常见问题解答以及来自该组的一些过去的线程 - 这是我的
语法主要来自哪里。它似乎没有点击。
Rob
I''ve been dinking around with this code for a while now and I can''t seem to
figure out where the problem lies. The original problem was that I for some
reason or another couldn''t allocate few 80,000,000 element arrays. Normally
that shouldn''t be a problem but it was. So I tried to do some calloc calls
and everything went south from there.
Essentially I''m trying to read in a big file, stick each line into an array,
then from then on do stuff to it. The do stuff part should work without a
hitch, if I can just get that read in part to work.
Here''s the code.
#include <stdio.h>
#include <string.h>
int main()
{
int i=0;
int j=0;
int eof_marker=0;
char *array[1000000];
char *out_array[1000000];
char input_line[80];
FILE *fp;
FILE *fp_out;
/*************************************************/
fp=fopen("/var/tmp/cpu.out", "r");
fp_out=fopen("/var/tmp/cpu.output2", "w");
for (i=0; i<1000000; i++)
array[i]=calloc(80, sizeof(char));
while(fgets(input_line, sizeof(input_line), fp) != NULL)
{
strcat(array[i], input_line); /* At this line the code core dumps */
i++;
}
eof_marker=i;
}
Please forgive M$''s abominable text parsing, my neat indenting doesn''t seem
to have survived intact.
I don''t think I understand the logic behind malloc and calloc. I''ve read
the FAQ and a few past threads from this group already - which is where my
syntax comes from mostly. It just doesn''t seem to be clicking.
Rob
推荐答案
'令人讨厌的文字解析,我整齐的缩进似乎并没有完好无损地存活下来。
我不认为我理解malloc和calloc背后的逻辑。我已经阅读了常见问题解答以及来自该组的一些过去的线程 - 这是我的
语法主要来自哪里。它似乎没有点击。
Rob
''s abominable text parsing, my neat indenting doesn''t seem
to have survived intact.
I don''t think I understand the logic behind malloc and calloc. I''ve read
the FAQ and a few past threads from this group already - which is where my
syntax comes from mostly. It just doesn''t seem to be clicking.
Rob
Eigenvector写道:
Eigenvector wrote:
我现在已经习惯了这段代码了一段时间,我似乎无法找出问题所在。最初的问题是我出于某种原因或其他原因无法分配少量80,000,000个元素阵列。通常
这不应该是一个问题,但确实如此。所以我试着做了一些calloc调用
,一切都从那里向南走了。
基本上我是想读一个大文件,把每一行都插入一个数组,>然后从那时开始做东西。 do stuff部分应该没有问题,如果我可以让部分读取工作。
这里是代码。
#include< stdio .h>
#include< string.h>
int main()
{int / i} i = 0;
int j = 0;
int eof_marker = 0;
char * array [1000000];
char * out_array [1000000];
char input_line [ 80];
FILE * fp;
FILE * fp_out;
/ ********************** ******************************* /
fp = fopen(" /var/tmp/cpu.out" ;," r");
fp_out = fopen(" /var/tmp/cpu.output2" ;," w");
for(i = 0; i< 1000000; i ++)
array [i] = calloc(80,sizeof(char));
^^^^^^^^^^^^
sizeof(char)的定义是1。
....但是回到你的问题。这里我的价值是多少?
while(fgets(input_line,sizeof(input_line),fp)!= NULL)
{
strcat(array [i ],input_line); / *在这一行代码核心转储* /
BOOM!
i ++;
}
eof_marker = i;
}
请原谅M
I''ve been dinking around with this code for a while now and I can''t seem to
figure out where the problem lies. The original problem was that I for some
reason or another couldn''t allocate few 80,000,000 element arrays. Normally
that shouldn''t be a problem but it was. So I tried to do some calloc calls
and everything went south from there.
Essentially I''m trying to read in a big file, stick each line into an array,
then from then on do stuff to it. The do stuff part should work without a
hitch, if I can just get that read in part to work.
Here''s the code.
#include <stdio.h>
#include <string.h>
int main()
{
int i=0;
int j=0;
int eof_marker=0;
char *array[1000000];
char *out_array[1000000];
char input_line[80];
FILE *fp;
FILE *fp_out;
/*************************************************/
fp=fopen("/var/tmp/cpu.out", "r");
fp_out=fopen("/var/tmp/cpu.output2", "w");
for (i=0; i<1000000; i++)
array[i]=calloc(80, sizeof(char)); ^^^^^^^^^^^^
sizeof(char) is 1 by definition.
....but back to your problem. What is the value of `i'' here?
while(fgets(input_line, sizeof(input_line), fp) != NULL)
{
strcat(array[i], input_line); /* At this line the code core dumps */
BOOM!
i++;
}
eof_marker=i;
}
Please forgive M
的恶意文本解析,我整齐的缩进似乎没有完好无损。
我不认为我理解malloc和calloc背后的逻辑。我已经阅读了常见问题解答和这个小组的一些过去的主题 - 这是我的
语法主要来自哪里。它似乎没有点击。
''s abominable text parsing, my neat indenting doesn''t seem
to have survived intact.
I don''t think I understand the logic behind malloc and calloc. I''ve read
the FAQ and a few past threads from this group already - which is where my
syntax comes from mostly. It just doesn''t seem to be clicking.
HTH,
--ag
-
Artie Gold - 德克萨斯州奥斯汀
HTH,
--ag
--
Artie Gold -- Austin, Texas
这篇关于读入数组的fgets和问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!