I am running the following code:
#include<stdio.h>
#include<string.h>
#include<io.h>
int main(){
FILE *fp;
if((fp=fopen("test.txt","r"))==NULL){
printf("File can't be read\n");
exit(1);
}
char str[50];
fgets(str,50,fp);
printf("%s",str);
return 0;
}
text.txt contains: I am a boy\r\n
Since I am on Windows, it takes \r\n as a new line character and so if I read this from a file it should store "I am a boy\n\0"
in str
, but I get "I am a boy\r\n"
. I am using mingw compiler.
str
? Your program never seems to investigate that. – Titerfgets
will include the new line characters plus a null terminator, so the output you get is perfectly fine. – Lesslie