here is the code:
i was writing a character whose ASCII value is 144 to a file transfer.txt, then i close the file and open it to read the character which i wrote and when i checked the ascii value of it it was showing -112…..why????
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
void main()
{
char ch=144,str;
int fd;
fd=open(“transfer.txt”,O_WRONLY|O_CREAT|O_TRUNC);
if(fd<0)
{
perror(“fault\n”);
exit(1);
}
write(fd,&ch,1);
close(fd);
fd=open(“transfer.txt”,O_RDONLY);
read(fd,&str,1);
printf(“%d\n”,str);
close(fd);
}
look your code carefully.
you have done big mistakes.
ch=144
in this line you are trying to enter the multiple character in a single character.
char is 1 byte data type.
to print desired result you need to take 144 as a string.
one another method that if you take ch as int type instead of char then your problem may resolve.
i am giving you 1 e.g..
void main()
{
int ch=144;
int str;
int fd;
fd=open(“transfer.txt”,O_WRONLY|O_CREAT|O_TRUNC);
if(fd<0)
{
perror("fault\n");
exit(1);
}
write(fd,&ch,sizeof(int));
close(fd);
fd=open("transfer.txt",O_RDONLY);
read(fd,&str,sizeof(int));
printf("%d\n",str);
close(fd);
}
char ch = 144
giving negative integer value because overflow had started