Вопрос

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

Это было полезно?

Решение

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.

Другие советы

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top