Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to make a stream file in pc with VC++.

The layout of records are as follows:
C++
struct Log_record {

char sequence[8];

int   index;

int   cord;

char  crlf[2];

}

Log_record my_rec;

char LogBuffer[sizeof(my_rec)*1000];

for (int i = 0;i<1000;i++)

{

  CString s = _T("");

  s.Format(_T("%07d"), i+1);

  Widechartomultibyte(s,...., my_rec.sequence, 8, NULL,NULL);

 my_rec.index = 0;;

 my_rec.cord = 0;

 my_rec.crlf = 0x0d0a;

 memcpy(&LogBuffer[i*sizeof(my_rec), &my_rec, sizeof(my_rec));

}


So, after filling a LogBuffer with 1000 records, and fwrite with option as "ab" one time.
So I expect a file with structure like as following
0000001 ..... CRLF 

0000002 ..... CRLF

.....

....

0001000 ...... CRLF




But, sometimes,
It make a file with space sequence or without CRLF.
It looks like broken records...

What is the cause of ths problem?

1. Simple coding mistake?
2. Or any other cause of PC?

Please help me.

Thank you in advance

What I have tried:

More 1 week wasted for this problem.
Posted
Updated 13-Jul-17 16:35pm
v2
Comments
Richard MacCutchan 13-Jul-17 12:23pm    
Why not just fill your structure and write each record one at a time, rather than copying to the log buffer? That way you can use your debugger and check exactly what is happening. Also why do you need CRLF sequence for fixed length records in a binary file?
Member 12330615 13-Jul-17 21:13pm    
It needs sequential number of records. So I use a Logbuffer filled with 1000 sequential records....
CRLF are needed what I see the file with my eyes...
Richard MacCutchan 14-Jul-17 4:15am    
Neither of which are good reasons for what you are doing. And you still have not shown all the code so it is impossible to guess what you are doing wrong.
Espen Harlinn 13-Jul-17 19:30pm    
Did you remember to call fclose on the handle before exiting the program?
You can also drop CString and call sprintf on your buffer (my_rec.sequence) directly - then the call to Widechartomultibyte will not be required as sprintf works with char and not wchar_t ...
Member 12330615 13-Jul-17 21:29pm    
This file is a index file as the sequences are very important. So, a Logbuffer with 1000 sequential records. But in the multiple transactions, The indexes are broken(?) and filled with spaces and no CRLF when I see the file by "notepad".

1 solution

Try something like this:
#pragma pack(push)
#pragma pack(1)
struct LogRecord
{
    char sequence[8];
    int   index;
    int   cord;
    char  crlf[2];

    LogRecord()
        : sequence{ }, index{ }, cord{ }, crlf{'\r','\n' }
    { }

};
#pragma pack(pop)


int main()
{
    auto handle = fopen( "F:\\src\\SimpleLogger\\log.dat", "ab" );

    LogRecord records[1000];

    for ( int i = 0; i < 1000; i++ )
    {
        sprintf( records[i].sequence, "%07d", i );
    }


    fwrite( records, 1, sizeof( records ), handle );

    fclose( handle );


    return 0;
}

Best regards
Espen Harlinn
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900