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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
03-09-2005, 12:05 AM
|
#1
|
LQ Newbie
Registered: Jul 2004
Posts: 6
Rep:
|
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.
|
|
|
03-09-2005, 03:03 AM
|
#2
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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.
|
|
|
09-19-2008, 09:42 AM
|
#3
|
LQ Newbie
Registered: Sep 2008
Posts: 2
Rep:
|
Quote:
Originally Posted by Blue_muppet
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.
|
|
|
All times are GMT -5. The time now is 02:50 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|