473,385 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to use fread?

i hav used like this
Expand|Select|Wrap|Line Numbers
  1. char mstr[50];
  2. file *fp;
  3. char *fname;
  4. printf("enter d file name\n");
  5. scanf("%s",fname);
  6. fp=fopen(fp,"r");
  7. fread(mstr,1,50,fp);
  8. lex-ana(mstr);//passing to constructor
  9. .
  10. .
  11. .
in output its displaying ilke this
enter filename
test.cpp//filename
input string is a+b ------(- indicates garbage value here)
i mean iam getting contents of file as wel as garbage value
pls help me
May 21 '12 #1
2 2787
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1. char *fname;
  2.   printf("enter d file name\n");
  3.   scanf("%s",fname);
  4.  
shows fname to be a pointer that you use without ever initializing it. You need to create an array and put the address of the array in the pointer before using the pointer in your scanf.
May 21 '12 #2
johny10151981
1,059 1GB
adding with weaknessforcats, its does not read garbage value.
if you request for
Expand|Select|Wrap|Line Numbers
  1. fread(mstr,1,50,fp);
then fread function will read 50 or less than 50 characters as available. fread will return the count of read number.

to solve your problem follow either of these:
1. use memset function to reset all the bytes in mstr variable, example: memset(mstr,0,sizeof mstr); remeber sizeof function wont work accordingly for pointer type character array.
or
2. use the return value of fread function to set total read character. example:
Expand|Select|Wrap|Line Numbers
  1. int n=fread(mstr, 1, 50, fp);
  2. mstr[n]=0;
  3.  
the problem with both statements are, if it read all 50 characters then then the program will create an error. it will try to access unallocated memory space. so, safe way is define
char mstr[50]; as

char mstr[51];

figure out rest :)
May 23 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Luc Holland | last post by:
Hey, I'm working on a program that reads a binary file. It's opened with ==== if ((f1=fopen(argv,"rb"))==NULL) { fprintf(stderr,"Error opening %s for reading . . .\n",argv); exit(2); } ====...
10
by: Alain Lafon | last post by:
Helas, I got something that should be a minor problem, but anyhow it isn't to me right now. A little code fragment: fread(&file_qn, x, 1, fp_q); The corresponding text file looks like...
6
by: Patrice Kadionik | last post by:
Hi all, I want to make a brief comparison between read() and fread() (under a Linux OS). 1. Family read and Co: open, close, read, write, ioctl... 2. Family fread and Co: fopen, fclose,...
2
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
8
by: M. Åhman | last post by:
I'm reading "C: A Reference Manual" but still can't understand a very basic thing: is there any functional difference between fgetc/fputc and fread/fwrite (when reading/writing one unsigned char)?...
13
by: 010 010 | last post by:
I found this very odd and maybe someone can explain it to me. I was using fread to scan through a binary file and pull bytes out. In the middle of a while loop, for no reason that i could...
5
by: David Mathog | last post by:
When reading a binary input stream with fread() one can read N bytes in two ways : count=fread(buffer,1,N,fin); /* N bytes at a time */ or count=fread(buffer,N,1,fin); /* 1 buffer at a...
20
by: ericunfuk | last post by:
If fseek() always clears EOF, is there a way for me to fread() from an offset of a file and still be able to detect EOF?i.e. withouting using fseek(). I also need to seek to an offset in the file...
3
by: juleigha27 | last post by:
Hi, First off, I want to apologize if this already got posted it seemed like something happened when I tried to post it previously and it didn't work. I am new to file manipulation with c. I...
30
by: empriser | last post by:
How to use fread/fwrite copy a file. When reach file's end, fread return 0, I don't konw how many bytes in buf.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.