Strings are written on same line

This is a discussion on Strings are written on same line within the Windows Programming forums, part of the Platform Specific Boards category; Developed this function to dump the string to file. But when ever I call this, string is written into the ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    Strings are written on same line

    Developed this function to dump the string to file.

    But when ever I call this, string is written into the same line. I want this into the next line. Can anybody suggest, what is going wrong in this..

    Thanks,
    Bye

    Code:
    bool writetoLogFile(CHAR *buf)
    {
    	bool bResult;
    	ULONG nbytes;
    	SYSTEMTIME st;
    	char logData[280];
    	
    	GetLocalTime(&st);
    
    	GetDateFormat( 0, 0, &st, "ddd',' MMM dd,yy",logData, sizeof(logData) );
    	GetTimeFormat( 0, 0, &st, "  hh':'mm':'ss tt", logData+14, sizeof(logData) );
    	
    	strcat(logData,buf);
    	logData[strlen(logData)] = '\0';
    	logData[strlen(logData)+1] = '\n';
    	
    	bResult = WriteFile( hLogFile,
    						 logData,
    						 strlen(logData),
    						 &nbytes,
    						 0);
    	if ( 0 == bResult )
    	{
    		printf("Can not write to log file\n");
    		return 0;
    	}
    	return 1;
    }

  2. #2
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    You have your null terminator before your newline character, so it never gets read.

  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    logData[strlen(logData)] = '\0';
    logData[strlen(logData)+1] = '\n';
    I'm sorry, but I have to laugh... Hahahaha!

    That is funny, yo. Just as good old minesweeper said, you appended the null character then you appended the newline : )
    hahahaha.. funny stuff.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Thanks LuckY.

    Even if I put the new line first it isn't working.

    Let me know, what is wrong.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    	GetDateFormat( 0, 0, &st, "ddd',' MMM dd,yy",logData, sizeof(logData) );
    	GetTimeFormat( 0, 0, &st, "  hh':'mm':'ss tt", logData+14, sizeof(logData) );
    	
    	logData[strlen(logData)] = '\n';
    	logData[strlen(logData)+1] = '\0';
    	strcat(logData,buf);
    Or:
    Code:
    	GetDateFormat( 0, 0, &st, "ddd',' MMM dd,yy",logData, sizeof(logData) );
    	GetTimeFormat( 0, 0, &st, "  hh':'mm':'ss tt", logData+14, sizeof(logData) );
    	
    	strcat(logData,"\n");
    	strcat(logData,buf);

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    At last I had to go back to fopen and fprintf.

    CreateFile and WriteFile did not work.

    Thanks for help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 05-30-2007, 05:47 PM
  2. Move the Caret to a line
    By TheDan in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2005, 12:59 PM
  3. Read only one line using seekg
    By RedZippo in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2004, 10:10 PM
  4. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 05:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21