Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > im facing problem with fread()??

Reply
Thread Tools

im facing problem with fread()??

 
 
Rajshekhar
Guest
Posts: n/a
 
      08-24-2005
Hi ,
i am writing a simple prgm to read a .txt file then store the contents
into the array...
program as follows:
--------------------------
#include<stdio.h>

int main()
{
FILE *fp1;
int buf[5];
int num,i;

fp1=fopen("triangle.txt","r");
if(fp1 == NULL)
{
printf("file cant be opend");
exit(0);
}

num=fread(buf,sizeof(int),5,fp1);
printf("num of elements read =%d\n",num);

for(i=0;i<5;i++)
printf("%d\n",buf[i]);
return 0;
}

------------------
i am getting no.of elements read as 0,but file is openable...
the elements as junk numbers...

can anybody tell me wat is the problem in doing this ..????
is it that i m using fread wrongly or it behaves abnoramlly???

TIA
Regards,
Rajshekhar

 
Reply With Quote
 
 
 
 
Kuku
Guest
Posts: n/a
 
      08-24-2005
The file triangle.txt must be empty that's why it is returning 0 number
of elements and junk value there on
Rajshekhar wrote:
> Hi ,
> i am writing a simple prgm to read a .txt file then store the contents
> into the array...
> program as follows:
> --------------------------
> #include<stdio.h>
>
> int main()
> {
> FILE *fp1;
> int buf[5];
> int num,i;
>
> fp1=fopen("triangle.txt","r");
> if(fp1 == NULL)
> {
> printf("file cant be opend");
> exit(0);
> }
>
> num=fread(buf,sizeof(int),5,fp1);
> printf("num of elements read =%d\n",num);
>
> for(i=0;i<5;i++)
> printf("%d\n",buf[i]);
> return 0;
> }
>
> ------------------
> i am getting no.of elements read as 0,but file is openable...
> the elements as junk numbers...
>
> can anybody tell me wat is the problem in doing this ..????
> is it that i m using fread wrongly or it behaves abnoramlly???
>
> TIA
> Regards,
> Rajshekhar


 
Reply With Quote
 
 
 
 
Rajshekhar
Guest
Posts: n/a
 
      08-24-2005
no its not empty ....!!
i read the contents of it using,,,,,getc & printed them using putc

~Rajshekhar

 
Reply With Quote
 
manoj1978@gmail.com
Guest
Posts: n/a
 
      08-24-2005

Rajshekhar wrote:
> Hi ,
> i am writing a simple prgm to read a .txt file then store the contents
> into the array...

Please post the contents of triangle.txt.then someone can help you.
If triangle.txt is a binary file then try rb instead of r in fopen.

> program as follows:
> --------------------------
> #include<stdio.h>
>
> int main()
> {
> FILE *fp1;
> int buf[5];
> int num,i;
>
> fp1=fopen("triangle.txt","r");
> if(fp1 == NULL)
> {
> printf("file cant be opend");
> exit(0);
> }
>
> num=fread(buf,sizeof(int),5,fp1);
> printf("num of elements read =%d\n",num);
>
> for(i=0;i<5;i++)
> printf("%d\n",buf[i]);
> return 0;
> }
>
> ------------------
> i am getting no.of elements read as 0,but file is openable...
> the elements as junk numbers...
>
> can anybody tell me wat is the problem in doing this ..????
> is it that i m using fread wrongly or it behaves abnoramlly???
>
> TIA
> Regards,
> Rajshekhar


 
Reply With Quote
 
Rajshekhar
Guest
Posts: n/a
 
      08-24-2005
hi
the file triangle.txt contents are...
6 6 4

thats it .....
i dont think this file is binary file ..!!!

 
Reply With Quote
 
Villy Kruse
Guest
Posts: n/a
 
      08-24-2005
On 24 Aug 2005 03:30:51 -0700,
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:


>
> Rajshekhar wrote:
>> Hi ,
>> i am writing a simple prgm to read a .txt file then store the contents
>> into the array...

> Please post the contents of triangle.txt.then someone can help you.
> If triangle.txt is a binary file then try rb instead of r in fopen.
>


The way fread() in program is used strongly suggests that the file has
to be binary. A text file is better read using fscanf(), or a combination
of fgets() and sscanf(), or som variant of getc()/getchar().

Villy
 
Reply With Quote
 
Villy Kruse
Guest
Posts: n/a
 
      08-24-2005
On 24 Aug 2005 03:34:16 -0700,
Rajshekhar <(E-Mail Removed)> wrote:


> hi
> the file triangle.txt contents are...
> 6 6 4
>
> thats it .....
> i dont think this file is binary file ..!!!



If sizeof(int) on your system is greater than the total number of bytes
in the input file there are not enough data for even the first element,
and thus you read zero elements.

Villy
 
Reply With Quote
 
Cong Wang
Guest
Posts: n/a
 
      08-24-2005

Rajshekhar wrote:
> hi
> the file triangle.txt contents are...
> 6 6 4
>
> thats it .....
> i dont think this file is binary file ..!!!

No,you are wrong!That depends on how you treat it!If you want to open
is as a binary file,it is a binary file!Text files are similar.

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      08-24-2005
Rajshekhar wrote:

> i am writing a simple prgm to read a .txt file


if it's a text file use fgets() rather than fread()


> then store the contents
> into the array...
> program as follows:
> --------------------------
> #include<stdio.h>


#include <stdio.h>


> int main()


int main (void)

> {
> FILE *fp1;
> int buf[5];
> int num,i;
>
> fp1=fopen("triangle.txt","r");
> if(fp1 == NULL)
> {
> printf("file cant be opend");
> exit(0);
> }
>
> num=fread(buf,sizeof(int),5,fp1);
> printf("num of elements read =%d\n",num);
>
> for(i=0;i<5;i++)
> printf("%d\n",buf[i]);


here we have a fundamental misconception. Lets suppose your file looks
like this:
3 4 5

I'm guessing your program wants to read three numbers (because it's
called triangle.txt). If you read raw binary and your file is in ASCII
you'll actually read the following bytes:
51 32 52 32 53

So you need to convert these bytes into numbers (or use %c istead of %d
in the above printf()). Try this:-

if (scanf (buf "%d %d %d", &j, &k, &m) != 3)
{
printf ("can't parse line\n");
exit (1);
}
else
printf ("read %d, %d %d\n", j, k, m);

declare j, k and m as int


> return 0;
> }
>
> ------------------
> i am getting no.of elements read as 0,but file is openable...
> the elements as junk numbers...


hmm. I only just noticed this. I don't know what your file looks like.
I don't know what "junk number" are. You've told it read five lots of
4 bytes (assuming 32-bit ints). Probably not what you meant. That won't

fit in a 5 bytes array. Really do change over to fgets(). fread() is
for binary data. fread() may not handle end of line and end of file
correctly.

> can anybody tell me wat is the problem in doing this ..????
> is it that i m using fread wrongly or it behaves abnoramlly???


I suggest you get a good book. Eg. K&R.


--
Nick Keighley

Quantum Boggum Sort:
Q1. use a source of quantum noise (eg. radioactive decay) to
randomly permutate an array.
Q2. if the array is not ordered, destroy the universe (*)
Q3. if you reached this step your universe has sorted the array
in O(n) time.

(*) [100] this is left as a exercise

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      08-24-2005
Rajshekhar <(E-Mail Removed)> wrote:

> #include<stdio.h>


The prototype for exit() is in stdlib.h, which you forgot to include.

> int main()
> {
> FILE *fp1;
> int buf[5];
> int num,i;


> fp1=fopen("triangle.txt","r");


> num=fread(buf,sizeof(int),5,fp1);


Consider what you are doing here. You are asking for the first sizeof
int * 5 bytes of the file (probably 20); if the file contains 6 4 4,
buf[0] will contain four bytes that correspond to the internal
representations of '6', ' ', and '4'. As already noted, you'd be much
better off reading the contents of this file using fgets() and using
strtol() and friends to get your integers.

> printf("num of elements read =%d\n",num);


> for(i=0;i<5;i++)


As a side note, if you know you read num bytes from the file, why not
iterate num times through buf?

> printf("%d\n",buf[i]);
> return 0;
> }


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Facing problem while using Wizards with Project Templates !!!!! Anubhav Jain ASP .Net 0 03-20-2006 09:16 AM
Facing Session Timeout problem Hema S via DotNetMonster.com ASP .Net 1 04-21-2005 08:58 AM
Facing problem in loading data from CArchive in VCDotNet. Konark Khera ASP .Net 0 03-22-2005 11:24 AM
Urgent Pls: Facing problem in reading Log information from Log file, created by IIS Amratash ASP .Net 0 04-13-2004 09:08 AM
facing a problem hussein_hariri@hotmail.com ASP .Net 2 12-27-2003 02:06 PM



Advertisments