LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 04-02-2007, 07:19 AM   #1
nesta
Member
 
Registered: Aug 2006
Posts: 100

Rep: Reputation: 15
problem with fread function


hi all,
i have a txt file named by hello.txt
i wanna read the file untill the end of it.

i write the following simple code :
Code:
#include <stdio.h>

char outbuff[7*1024];

int main ()
{
	int read_bytes,fd,write_bytes;

	printf("Start reading data\n");
	fd  = fopen("hello.txt","r");
	if (fd==NULL)
		printf("error cant open the file\n");
	read_bytes = fread(outbuff, sizeof(outbuff), 1,fd);
	printf("the read bytes ar %d\n",read_bytes);
	fclose(fd);
         return 0;
}
when i run the previous program, the read_bytes variable was zero.
and also i dont know how can i read the file till the end. alll i just can imagine is that i should make a while loop?
can anyone help me please, thanks in advance
 
Old 04-02-2007, 08:50 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
first of all fd should be a FILE* not an int, that should get rid of a whole bunch of compile warnings

Quote:
Originally Posted by man fread
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
The function fread() reads nmemb elements of data, each size bytes
long, from the stream pointed to by stream, storing them at the loca-
tion given by ptr.
It looks to me like to you mixed up the size and nmemb parameters, when I switch them your program works. I think what was happening was that fread didn't manage to get a whole "element" of size 7*1024, so it just returned without reading anything.
 
Old 04-02-2007, 08:51 AM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
I do not see how it compiled in the first place.
Remove fd from the list of int's you declared.
Add this line as a declaration:
FILE *fd=NULL;

Also whenever you open a file, check the returns - ie., if(fd==NULL) { report the error }
 
Old 04-02-2007, 12:23 PM   #4
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
to be more specific

i go for
Code:
if( fd == (FILE*) NULL ) {
  exit(1);
}
 
Old 04-02-2007, 01:10 PM   #5
haxpor
Member
 
Registered: Dec 2006
Distribution: Ubuntu 20.04
Posts: 87

Rep: Reputation: 15
You should print out outbuff to test the result.

Anyway the fread() function will return the number of objects read, in this case your each object is 1 byte long (because char is used).

The result you see that it print 0 for read_bytes, it is correct because it read successfully in 1 time so you dont have to put it in while loop (but i suggest everytime dealing with read/write IO function you should put it in while loop to check if the byte or object it read is still more than 0) .

That is, you have read to the end of file already. Just check outbuff, if I correct then.
 
  


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
C: fread to read a file line by line until the end Blue_muppet Programming 2 09-19-2008 09:42 AM
using fread with fscanf andystanfordjason Programming 4 12-20-2006 09:49 AM
C programming fread and fwrite using structures exvor Programming 4 09-26-2006 08:56 AM
UT: "fread failed: BufferCount = -248172 Error=1" occurs after gfx card change SBing Linux - Games 2 07-04-2004 04:40 PM
fread magic: Input/Output error LoungeLizard Linux - Hardware 1 02-24-2004 06:21 PM

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

All times are GMT -5. The time now is 03:11 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