473,594 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fgets and problems reading into array

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(inp ut_line, sizeof(input_li ne), 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
Nov 13 '05 #1
12 7133
Eigenvector wrote:
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(inp ut_line, sizeof(input_li ne), fp) != NULL)
{
strcat(array[i], input_line); /* At this line the code core dumps */
BOOM!
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.


HTH,
--ag

--
Artie Gold -- Austin, Texas

Nov 13 '05 #2
[snips]

On Fri, 25 Jul 2003 18:25:30 -0700, Eigenvector wrote:
for (i=0; i<1000000; i++)
array[i]=calloc(80, sizeof(char));
Three items on one line...

1) why not allocate the length of the line actually read in? That way
short lines don't waste space, long lines don't overflow.

2) No check for calloc failure.

3) sizeof(char)? Just use 1.
while(fgets(inp ut_line, sizeof(input_li ne), fp) != NULL)
{
strcat(array[i], input_line); /* At this line the code core dumps

*/

Assuming calloc properly zero-inits the buffer, that should be okay...
except you don't even know if the allocation worked or not.

'Course, the fact that i currently points past the _last_ element of the
array might be an issue...
Nov 13 '05 #3

"Kelsey Bjarnason" <ke*****@xxnosp amyy.lightspeed .bc.ca> wrote in message
news:pa******** *************** *****@xxnospamy y.lightspeed.bc .ca...
[snips]

On Fri, 25 Jul 2003 18:25:30 -0700, Eigenvector wrote:
for (i=0; i<1000000; i++)
array[i]=calloc(80, sizeof(char));


Three items on one line...

1) why not allocate the length of the line actually read in? That way
short lines don't waste space, long lines don't overflow.

2) No check for calloc failure.

3) sizeof(char)? Just use 1.
while(fgets(inp ut_line, sizeof(input_li ne), fp) != NULL)
{
strcat(array[i], input_line); /* At this line the code core dumps

*/

Assuming calloc properly zero-inits the buffer, that should be okay...
except you don't even know if the allocation worked or not.

'Course, the fact that i currently points past the _last_ element of the
array might be an issue...


Oh, crap!!!!!!! I didn't even think of that! I forgot to reset the value
for i

Please tell me that the value for 'i' was in fact the reason why my code
bombed.

You'll have to forgive some of the generic formatting and other strange
anomolies in my code - some of it was straight plagarism just to figure out
how the syntax for calloc worked. It'll get cleaned out when I actually get
the code working.

Nov 13 '05 #4

"Eigenvecto r" <co*******@hotm ail.com> wrote in message news:EA******** ********@news.u swest.net...
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(inp ut_line, sizeof(input_li ne), fp) != NULL)
{
strcat(array[i], input_line); /* At this line the code core dumps */
i++;
}
I think "i" is the main problem, you didn't reset i = 0 before the while loop. Also, why do you use
strcat instead of strcpy ? ( I know that it is not a problem, because calloc fills the array block
with all zero. )

BTW, you should add code to trap the failure of fopen and calloc.

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


--
BC
Nov 13 '05 #5
In article <EA************ ****@news.uswes t.net>, co*******@hotma il.com
says...
char *array[1000000];
char *out_array[1000000];


You've already received several good responses to the program operation,
but you should be aware that there is no guarantee that you can declare
an array this large and have it work (portably). A fair number of
compilers will bomb out on arrays that large. I believe the limit for
the size of an array declared this way is 64KB in C99, and half that for
C89, but I can't recall the "chapter and verse" on that as I can't
remember the last time I even attempted to declare an array this large in
this way.

clc Faq entries 16.3 and 19.23 cover this, perhaps a bit obliquely.
(http://www.eskimo.com/~scs/C-faq/faq.html)

Nov 13 '05 #6

"Randy Howard" <ra**********@F OOmegapathdslBA R.net> wrote in message
news:MP******** *************** *@news.megapath dsl.net...
In article <EA************ ****@news.uswes t.net>, co*******@hotma il.com
says...
char *array[1000000];
char *out_array[1000000];
You've already received several good responses to the program operation,
but you should be aware that there is no guarantee that you can declare
an array this large and have it work (portably). A fair number of
compilers will bomb out on arrays that large. I believe the limit for
the size of an array declared this way is 64KB in C99, and half that for
C89, but I can't recall the "chapter and verse" on that as I can't
remember the last time I even attempted to declare an array this large in
this way.

Well that's kind of lame. I understand that not every computer has loads of
memory, but putting an actual limit or even recognizing a limit seems
artificial and unnecessary. I guess programmers need to make their code as
efficient as possible at all times, but I intensely dislike those types of
standards.

64Kb is pretty damn small, maybe not for a PC, but certainly my 16 GB
internal memory UNIX server shouldn't have this limit imposed on it. But
like you said, its not guarenteed, and there are other more efficient ways
of doing the job.
clc Faq entries 16.3 and 19.23 cover this, perhaps a bit obliquely.
(http://www.eskimo.com/~scs/C-faq/faq.html)

Nov 13 '05 #7
Eigenvector wrote:

<snip>

64Kb is pretty damn small, maybe not for a PC, but certainly my 16 GB
internal memory UNIX server shouldn't have this limit imposed on it.


The limit is not imposed on your implementation. It is imposed on programs
which the programmer requires to be portable to any conforming C compiler.

<snip>

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #8
Eigenvector <co*******@hotm ail.com> wrote:

"Randy Howard" <ra**********@F OOmegapathdslBA R.net> wrote in message
news:MP******** *************** *@news.megapath dsl.net...
In article <EA************ ****@news.uswes t.net>, co*******@hotma il.com
says...
> char *array[1000000];
> char *out_array[1000000];


You've already received several good responses to the program operation,
but you should be aware that there is no guarantee that you can declare
an array this large and have it work (portably). A fair number of
compilers will bomb out on arrays that large. I believe the limit for
the size of an array declared this way is 64KB in C99, and half that for
C89, but I can't recall the "chapter and verse" on that as I can't
remember the last time I even attempted to declare an array this large in
this way.

Well that's kind of lame. I understand that not every computer has loads of
memory, but putting an actual limit or even recognizing a limit seems
artificial and unnecessary. I guess programmers need to make their code as
efficient as possible at all times, but I intensely dislike those types of
standards.


Don't think of it as telling you your array won't work if it's bigger
than 64kB - instead think of it as promising that you that your array
*will* work if it's smaller than 64kB.

- Kevin.

Nov 13 '05 #9
[snips]

On Sun, 27 Jul 2003 05:50:49 -0700, Eigenvector wrote:
Well that's kind of lame. I understand that not every computer has loads of
memory, but putting an actual limit or even recognizing a limit seems
artificial and unnecessary. I guess programmers need to make their code as
efficient as possible at all times, but I intensely dislike those types of
standards.

64Kb is pretty damn small, maybe not for a PC, but certainly my 16 GB
internal memory UNIX server shouldn't have this limit imposed on it. But
like you said, its not guarenteed, and there are other more efficient ways
of doing the job.


It's not a limit; you can (perhaps) create objects terabytes in size.
What it is is a statement that you cannot create objects larger than X and
expect them to actually work across all conforming implementations .
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7230
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files, two aspects which I want to incorporate into my program eventually. That aside, my most pressing problem right now is how to get rid of the newline in the input when I use fgets(). Now I have looked around on the net, not so much in this group...
20
7887
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The standard says about fgets: synopsis #include <stdio.h> char *fgets(char *s, int n, FILE *stream);
35
9942
by: David Mathog | last post by:
Every so often one of my fgets() based programs encounters an input file containing embedded nulls. fgets is happy to read these but the embedded nulls subsequently cause problems elsewhere in the program. Since fgets() doesn't return the number of characters read it is pretty tough to handle the embedded nulls once they are in the buffer. So two questions: 1. Why did the folks who wrote fgets() have a successful
42
6769
by: mellyshum123 | last post by:
I need to read in a comma separated file, and for this I was going to use fgets. I was reading about it at http://www.cplusplus.com/ref/ and I noticed that the document said: "Reads characters from stream and stores them in string until (num -1) characters have been read or a newline or EOF character is reached, whichever comes first." My question is that if it stops at a new line character (LF?) then how does one read a file with...
9
3414
by: uidzer0 | last post by:
Hey everyone, Taken the following code; is there a "proper" or dynamic way to allocate the length of line? #include <stdio.h> #include <errno.h> int main(int argc, char **argv) { FILE *fp;
24
2915
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be modified and the program code. In the attached file below i just want to change the value of data(only float value) after the line 1 P V T 1 15 till 2 G TT, from positive to negative and vice versa, and wire the date in other file. can someone help...
285
8757
by: Sheth Raxit | last post by:
Machine 1 : bash-3.00$ uname -a SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R bash-3.00$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/ specs gcc version 2.95.3 20010315 (release)
27
5082
by: Jeff | last post by:
Im trying to figure out why I cant read back a binary file correctly. I have the following union: #define BITE_RECORD_LEN 12 typedef union { unsigned char byte; struct { unsigned char type; /* 0 Type */ unsigned char sn; /* 1-3 Serial Number */
14
2429
by: subramanian100in | last post by:
Suppose fgets is used to read a line of input. char str; fgets(str, sizeof(str), stdin); After reading some characters on the same line, if end-of-file is encountered, will fgets return the 'str' parameter and set EOF indicator for the stream ? Or will it return the string argument and set EOF indicator only on subsequent call ? Kindly clarify
0
7946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7876
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
6654
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5739
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5408
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3859
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3897
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2385
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1478
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.