LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-09-2005, 12:05 AM   #1
Blue_muppet
LQ Newbie
 
Registered: Jul 2004
Posts: 6

Rep: Reputation: 0
C: fread to read a file line by line until the end


Hey there,
Im having trouble getting fread to work the way i want it to. I have a long text file 500,000+ lines of text, that i want to read in line by line, until the end of the file. At the moment, my code looks kinda like this

char *buffer[somebignumber];
fread(buffer, sizeof(buffer), 1, dataFile);

dataFile is correctly opened.
Basically the problem i get is that it will read in the first 35,000 lines or so, and then stop, like it has run out of memory space or something.
there are 122 characters on each line of the text file.
Can anybody help me with this?

also, how would i go about telling fread to only read in say, the first 5,000 lines? would it just be a matter of keeping track of an incrementing int in a loop?

thanks.
 
Old 03-09-2005, 03:03 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You can't use fread() to read in a line of text. fread() works on number of bytes. I think fgets() will do what you're looking for.
 
Old 09-19-2008, 09:42 AM   #3
yodasoda
LQ Newbie
 
Registered: Sep 2008
Posts: 2

Rep: Reputation: 0
Quote:
Originally Posted by Blue_muppet View Post
Hey there,
Im having trouble getting fread to work the way i want it to. I have a long text file 500,000+ lines of text, that i want to read in line by line, until the end of the file. At the moment, my code looks kinda like this

char *buffer[somebignumber];
fread(buffer, sizeof(buffer), 1, dataFile);

dataFile is correctly opened.
Basically the problem i get is that it will read in the first 35,000 lines or so, and then stop, like it has run out of memory space or something.
there are 122 characters on each line of the text file.
Can anybody help me with this?

also, how would i go about telling fread to only read in say, the first 5,000 lines? would it just be a matter of keeping track of an incrementing int in a loop?

thanks.
That is because it has run out of space. I had a similar problem on an embedded system (TI DSP) and I think this may be the same case for you. I had a really big array to compute with and I had declared it in my main but what that did was to saturate the stack of my main. So what I then did was to declare the big array outside of my main as a global variable and the problem disapeared. remember also that each time you call a function the computer has to save the current process in the stack so your array would bust the stack.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
No new line at end of file? What does this mean? BajaNick Programming 12 05-19-2014 08:39 PM
read the input file from the specified line no till end suchi_s Programming 5 09-09-2004 04:36 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM
adding new line at end of file airikah Programming 3 11-08-2003 04:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration