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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question using fgetc();

Status
Not open for further replies.

Netherbeast

Programmer
Jun 4, 2002
89
US
I'm trying to get the Decimal value of an Ascii char being read by fgetc() like this...

int theChar = 0;//the character being read
FILE *fp = fopen("poly.txt", "r");//file being read

while(theChar=fgetc(fp) != EOF) {
printf("theChar=%d\n", theChar);
}

but when it prints, it only prints the value 1.
I'm just a begining programmer and don't understand everything yet. Pointers are hard for me too.

Netherbeast
 
Netherbeast:

This works under Solaris 7 Unix:

#include <stdio.h>

int main()
{
int theChar;
FILE *fp;

if((fp = fopen(&quot;poly.txt&quot;, &quot;r&quot;)) == NULL)
{
printf(&quot;can't open file\n&quot;);
exit(1);
}

while((theChar=fgetc(fp)) != EOF) {
printf(&quot;theChar=%c\n&quot;, theChar);
}

fclose(fp);
}

You have major problems:

1) You need an extra set of () around:
((theChar=fgetc(fp))

2) From the man page:
The fgetc() function obtains the next byte (if present) as
an unsigned char converted to an int, from the input stream
pointed to by stream,

This means since theChar is an int, you have to use the character &quot;c&quot; agrument of printf.

Regards,


Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top