c++ - reading and writing of new line charecter -


i confuse \n , \r\n charecters.i reading , writing files in c++.

basically want maintain log file of 1 exe following conditions consider.

1)if file not present create file.

2)if file present append data on it.

but reason not working following block of code using.

to open file using following code.

 bool applicationfilenotopen;    char* applicationfilelogname; applicationfilelogname = ".\\applicationlog\\sample.log"; handle apllicationlogwritehandle; apllicationlogwritehandle = createfile(applicationfilelogname, // name of write                     generic_write,          // open writing                     0,                      // not share                     null,                   // default security                     create_new,             // create new file                     file_attribute_normal,  // normal file                     null);                  // no attr. template   

to write data in file using following code..

char messagestring[200]; messagestring = "application start \n"; time_t applicationnow = time(null); applicationnow = time(null); struct tm * timeinfo = localtime(&applicationnow); char applicationtimstring[100]; strftime (applicationtimstring,32,"%d/%m/%y %i:%m:%s %p",timeinfo);  char temp_char_array[default_array_size]; strcpy(temp_char_array,applicationtimstring); strcat(temp_char_array,  messagestring); dword dwbytestowrite = (dword)strlen(temp_char_array); dword dwbyteswritten = 0; bool berrorflag = false; berrorflag=writefile(applicationfilehandle, // open file handle            temp_char_array,      // start of data write            dwbytestowrite,  // number of bytes write            &dwbyteswritten, // number of bytes written            null);            // no overlapped structure   

and reading file using following code.

char* applicationfilelogname; applicationfilelogname = ".\\applicationlog\\sample.log"; file *applicationfileopenhandle;  applicationfileopenhandle =fopen(applicationfilelogname,"r");  //read file.. while(fgets(currentstring , default_array_size , applicationfileopenhandle) != null)//reading 1 one line file unicode.. {         printf("%s",currentstring); } 

now when open file "sample.log" in notpad show me following content in same line.

10/05/2013 02:32:28 pm application start 10/05/2013 02:32:36 pm application start 10/05/2013 02:47:31 pm application start

all in same line when open in wordpad or textpad show me content proper like,

10/05/2013 02:32:28 pm application start

10/05/2013 02:32:36 pm application start

10/05/2013 02:47:31 pm application start

please tell me going wrong.

note :- use \r\n not working. .

as see on windows. windows line separator crlf (\r\n). notepad tolerates this. word or wordpad can handle \n separators. on unices line separator \n unix gvim can handle also.

so either add \r\n @ end of each lines, or use text viewer, can handle \n line endings.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -