console - C - fgets skips CR character -
i have c code, reads line txt file. file has 1 line below:
the code snippet read line is:
int** readfile(char* filename){ int col=0, row =0; int i=0; int* numlist[2048]; for(int = 0; i<2048; i++) numlist[i] = (int*) malloc(6*sizeof(int)); if(null == numlist){ printf("memory error!"); } char * token = null; char currentline[25] = {'\0'}; file* file = fopen(filename, "r"); if(null != file){ printf("file opened successfully\n"); if( null != fgets (currentline, 60, file) ) { int = 0; while (null != currentline[i]){ printf("%d ", currentline[i]); i++; } } } else { printf("file i/o error"); return null; } fclose(file); return numlist; }
when code runs, following output:
i observed suspicious, is, can see in first screenshot (content of txt file), notepad++ shows cr lf @ end of line. in output, see 10 last character lf.
probably missing primitive point but, couldn't understand why cr character not there.
needless say, platform windows , console program.
thanks®ards.
you're opening file in text mode. mode ensures can handle text files same on platform.
c specifies '\n'
end of line. in windows, end of line sequence "\r\n"
. c (in case, standard library implementing stdio
) automatically translate you. reading file on windows in text mode give \n
\r\n
.
if want see byte contents of file, have open in binary mode instead:
file* file = fopen(filename, "rb");
Comments
Post a Comment