console - C - fgets skips CR character -


i have c code, reads line txt file. file has 1 line below:

content of txt file

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:

output of program

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&regards.

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

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -