Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my program I need to store the character corresponding to some ASCII value and then retrieved the character from the file and print its ASCII value.

The code for this program is given below
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
int ch;
FILE *fptr;
clrscr();
fptr=fopen("newtest1.txt","w++");
for(ch=0;ch<256;ch++)
fprintf(fptr,"%c",ch);
fclose(fptr);
fptr=fopen("newtest1.txt","r++");
for(;;)
{
ch=fgetc(fptr);
if(ch==EOF)
break;
printf("%d\t",ch);
}
getch();
}




but the problem is that it only gives output 1 to 25. after that it gets out of the loop and does not print the rest.I 'm using turbo C.
this program run goo in Linux environment.But I have to do the program in Turbo C.how can I read the value 26? please help.
Posted

Why don't you open the file in binary mode?
:)
 
Share this answer
 
In text mode, the value 26 (0x1A or ASCII CTRL-Z) is interpreted as end of file and fgetc() returns the EOF code.
In binary mode fgetc() doesn't interpret the readed value and you can read to the real end of file.

You better use if( 0 != feof(fptr)) as break condition to end the reading loop.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900