-6

I am really confuse now what to do with \n and \r\n charecters.I am reading and writing some files in c++.

Basically i want to maintain log file of one exe where following conditions are consider.

1)If file is not present then create file.

2)If file is already present then append data on it.

But some reason it not working following are some block of code i am using.

to open a file i am using following code.

 bool ApplicationfileNotOpen;   
char* ApplicationFileLogName;
ApplicationFileLogName = ".\\ApplicationLog\\Sample.log";
HANDLE APllicationLogWriteHandle;
APllicationLogWriteHandle = CreateFile(ApplicationFileLogName, // name of the write
                    GENERIC_WRITE,          // open for writing
                    0,                      // do not share
                    NULL,                   // default security
                    CREATE_NEW,             // create new file only
                    FILE_ATTRIBUTE_NORMAL,  // normal file
                    NULL);                  // no attr. template  

To write a data in file i am 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 to write
           dwBytesToWrite,  // number of bytes to write
           &dwBytesWritten, // number of bytes that were written
           NULL);            // no overlapped structure  

and for reading from file i am 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 one by one line from file it UNICODE..
{
        printf("%s",CurrentString);
}

Now when i open file "sample.log" in notpad it will show me following content all 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 but when i open it in wordpad or textpad it will show me all 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 where i am going wrong.

Note :- i already use \r\n but its not working. .

xaxxon
  • 19,189
  • 5
  • 50
  • 80
Santosh Dhanawade
  • 1,756
  • 14
  • 29
  • 1
    Just use `std::ofstream`, opened with `std::ios_base::app`. And `std::endl` instead of `"\r\n"`. Much simpler. – James Kanze May 10 '13 at 09:40
  • 1
    @JamesKanze Why flush all the time? – BoBTFish May 10 '13 at 09:41
  • sorry to say i cant use std interface as some told me to do without std:: i can use c++ for object oriented fetures only. – Santosh Dhanawade May 10 '13 at 09:42
  • @xaxxon i known new line charecter has different representation in unicode and ascii. – Santosh Dhanawade May 10 '13 at 09:45
  • @SantoshDhanawade \r\n is the correct solution, you need to say how it's not working for you. No-one can help unless you *describe* the problem you are having, 'its not working' is not good enough. – john May 10 '13 at 09:52
  • might also help to open the output file in a hex editor and look at the values that you are actually writing to the file – camelccc May 10 '13 at 09:54
  • 1
    @BoBTFish In this case, because it's a log file. In general, because it makes debugging easier. – James Kanze May 10 '13 at 10:03
  • Re the note: the code in your posting doesn't use `"\r\n"`. – James Kanze May 10 '13 at 10:08
  • 1
    All those people dissing Notepad may want to adjust their answers now that it handles LF properly. Of course, that won't stop me from using something decent like Vim :-) – paxdiablo Jul 17 '18 at 01:33

2 Answers2

0

As I see this is on windows. Windows line separator is CRLF (\r\n). Notepad only tolerates this. Word or wordpad can handle the \n separators. On unices the line separator is \n so unix gvim can handle this also.

So either add \r\n at the end of each lines, or use a text viewer, which can handle the \n only line endings.

TrueY
  • 7,360
  • 1
  • 41
  • 46
  • dear ,1)i already use \r\n but its not working even.2)File will go to client side it depend on user which software he/she use to open file.if he use workpad then best but need solution when he/she use notepad. – Santosh Dhanawade May 10 '13 at 09:47
  • 3
    No, the line ending is not `\n\r` anywhere except someone's nightmare. – R. Martinho Fernandes May 10 '13 at 09:49
  • 1
    The Mac OS line ending was `\r`, in the distant past. Today, MacOS is a variant of Unix, and uses the Unix line endings. – James Kanze May 10 '13 at 10:06
  • @SantoshDhanawade: I see in the definition of `MessageString` the following: `"Application Start \n"`. Where does the `\r` appear? – TrueY May 10 '13 at 10:26
  • @R.MartinhoFernandes: Thanks! Line removed! – TrueY May 10 '13 at 10:27
  • @SantoshDhanawade: Yes I did, but the code does not reflects to it! I would suggest to install some hexa editor on windows (in `cygwin` I may offer `od -c` command) and check the end of the lines. I assume there is only `\n` and not `\r\n`. – TrueY May 10 '13 at 11:17
-2

Different systems expect different line endings.

Windows traditionally wants \r\n, while unix prefers just \n.

What you are seeing is some apps are better at dealing with different representations. Notepad is braindead and expects \r\n. Wordpad is a bit smarter and can deal with UNIX-style \n.

http://en.wikipedia.org/wiki/Newline

xaxxon
  • 19,189
  • 5
  • 50
  • 80