Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   im facing problem with fread()?? (http://www.velocityreviews.com/forums/t439184-im-facing-problem-with-fread.html)

Rajshekhar 08-24-2005 09:29 AM

im facing problem with fread()??
 
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


Kuku 08-24-2005 10:24 AM

Re: im facing problem with fread()??
 
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



Rajshekhar 08-24-2005 10:28 AM

Re: im facing problem with fread()??
 
no its not empty ....!!
i read the contents of it using,,,,,getc & printed them using putc

~Rajshekhar


manoj1978@gmail.com 08-24-2005 10:30 AM

Re: im facing problem with fread()??
 

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



Rajshekhar 08-24-2005 10:34 AM

Re: im facing problem with fread()??
 
hi
the file triangle.txt contents are...
6 6 4

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


Villy Kruse 08-24-2005 10:54 AM

Re: im facing problem with fread()??
 
On 24 Aug 2005 03:30:51 -0700,
manoj1978@gmail.com <manoj1978@gmail.com> 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

Villy Kruse 08-24-2005 10:56 AM

Re: im facing problem with fread()??
 
On 24 Aug 2005 03:34:16 -0700,
Rajshekhar <rajshekhar3@gmail.com> 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

Cong Wang 08-24-2005 10:58 AM

Re: im facing problem with fread()??
 

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.


Nick Keighley 08-24-2005 11:01 AM

Re: im facing problem with fread()??
 
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


Christopher Benson-Manica 08-24-2005 02:32 PM

Re: im facing problem with fread()??
 
Rajshekhar <rajshekhar3@gmail.com> 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.


All times are GMT. The time now is 03:57 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.