Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hi guys I am reading a file and pri

Status
Not open for further replies.

italy

Programmer
Feb 13, 2001
162
0
0
US
hi guys I am reading a file and printing it out but the printing or reading the file skips letters like goes to the first letter and skip the second and goes to the third and skip the fourth and so on any help


char ch;
FILE *fl;
do (ch = fgetc(fl)){

printf("the words ar %c ",ch);

while (fgetc(fl) != EOF)
 
Your code is incomplete, however I can see 2 errors in what you posted.

* ch should be type int, not char.

* You need an extra set of parentheses around your loop condition.

int ch;

while ((ch = fgetc(fl)) != EOF)
/* Do something with ch */;
 
there... this will work
//------------
char szfilename[255] = "myfile.txt";
char ch;
FILE *fl;

if (fl = fopen(szfilename,"r")) //"r" for read
{
printf("the words ar ");
do
{
ch = fgetc(fl);
printf("%c",ch);

}while (ch != EOF);
}
//------------


jb
 
ch is still the wrong type. If char is unsigned, ch will never equal EOF and the loop will continue forever. If char is signed and there are values larger than SCHAR_MAX that are returned by fgetc, the result overflows.

The test is also performed at the wrong time and the value of EOF gets printed to standard output.
 
hi,
as per my knowledge goes i m answering........

here in pgm, in do (c = ..) u hav used fgetc once which will get the character pointed by f1 & thats printed using printf.but again in while(fgetc ...) u r getting char but not printing it & when control goes back to do(..), its getting char again. so, char which was got in while(..) is missed out. so, i feel thats the reason u r skipping characters while printing....
try using a while loop instead of a do-while...
 
your calling fgetc twice in each iteration. one gets printed to stdout and the other goes to the bit bucket.

disregarding other errors you might change

while (fgetc(fl) != EOF)

to this

while (ch != EOF)

the other sugestions as above notwithstanding.

tomcruz.net
 
its off the topic ... but i'd like to ask u a question
Is this question that u've posted just meant for guys?
why females cant answer it /do u think females are incapable of answering ur questions???

-vs
 
thank you manvid for your question but this is term used in north america describing a group of people wormen and man so I did not mean disrespect and thank you again for your reply.
 
If you want to be careful not to offend anyone, you might try "Hey you all" or "Hey y'all". :)
 
well Italy,
i hail from a country in Asia where "guys" are used only for men... dat too informally...
like dlkdldk.. had said .. y'all / people ... these words iam sure will not bother anyone..
-vs
 
well Ms. hail hail I am an asian my self and I did not to mean to be a sexest and I explained it to you so if got offended u can drink from the sea becuase honesly I dont give a dam about your insecurity ???
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top