i hav used like this - char mstr[50];
-
file *fp;
-
char *fname;
-
printf("enter d file name\n");
-
scanf("%s",fname);
-
fp=fopen(fp,"r");
-
fread(mstr,1,50,fp);
-
lex-ana(mstr);//passing to constructor
-
.
-
.
-
.
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
2 2787
This code: - char *fname;
-
printf("enter d file name\n");
-
scanf("%s",fname);
-
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.
adding with weaknessforcats, its does not read garbage value.
if you request for
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: - int n=fread(mstr, 1, 50, fp);
-
mstr[n]=0;
-
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 :)
Sign in to post your reply or Sign up for a free account.
Similar topics
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);
}
====...
|
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...
|
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,...
|
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;
|
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)?...
|
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...
|
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...
|
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...
|
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...
|
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.
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
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,...
|
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...
| |