read/ write to screen/ file
-
I'm trying to make it clear this time. I have a function called Read that reads from the COM. Then i'm using it in the following piece of code to print the result to the screen and to write it in one file. The output to the screen it's ok (FFFF00000000028C) but the one to the file is : "ൃFFF " or smth like that. Note: all the output from my device that is connected to COM is preceded by a Line feed and terminated by a Carriage return I use the following code:
DWORD dwBytesRead = 0; char szBuffer[101]; FILE *out; serial.Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead); if (dwBytesRead > 0) { szBuffer[dwBytesRead] = '\0'; out = fopen( "output.txt", "w" ); fprintf( out, "%s", szBuffer ); printf("%s", szBuffer); fclose(out); // Check if EOF (CTRL+'[') has been specified if (strchr(szBuffer,EOF_Char)) fContinue = false; }
-
I'm trying to make it clear this time. I have a function called Read that reads from the COM. Then i'm using it in the following piece of code to print the result to the screen and to write it in one file. The output to the screen it's ok (FFFF00000000028C) but the one to the file is : "ൃFFF " or smth like that. Note: all the output from my device that is connected to COM is preceded by a Line feed and terminated by a Carriage return I use the following code:
DWORD dwBytesRead = 0; char szBuffer[101]; FILE *out; serial.Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead); if (dwBytesRead > 0) { szBuffer[dwBytesRead] = '\0'; out = fopen( "output.txt", "w" ); fprintf( out, "%s", szBuffer ); printf("%s", szBuffer); fclose(out); // Check if EOF (CTRL+'[') has been specified if (strchr(szBuffer,EOF_Char)) fContinue = false; }
If that code is inside a loop (I suppose that, because of the fContinue), then you will close and open the file each time (and of course erasing all its content). It is better top open the file it before and close it after the loop.
Cédric Moonen Software developer
Charting control -
If that code is inside a loop (I suppose that, because of the fContinue), then you will close and open the file each time (and of course erasing all its content). It is better top open the file it before and close it after the loop.
Cédric Moonen Software developer
Charting controlI've also tried that version, and I agree with you, but it still doesn't work.
-
I'm trying to make it clear this time. I have a function called Read that reads from the COM. Then i'm using it in the following piece of code to print the result to the screen and to write it in one file. The output to the screen it's ok (FFFF00000000028C) but the one to the file is : "ൃFFF " or smth like that. Note: all the output from my device that is connected to COM is preceded by a Line feed and terminated by a Carriage return I use the following code:
DWORD dwBytesRead = 0; char szBuffer[101]; FILE *out; serial.Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead); if (dwBytesRead > 0) { szBuffer[dwBytesRead] = '\0'; out = fopen( "output.txt", "w" ); fprintf( out, "%s", szBuffer ); printf("%s", szBuffer); fclose(out); // Check if EOF (CTRL+'[') has been specified if (strchr(szBuffer,EOF_Char)) fContinue = false; }
I can't see a problem with your code. As Cedric said, your code will overwrite anything that is already in the file. You can work round this by opening the file then moving to the end before writing, something like this:
DWORD dwBytesRead = 0, dwBytesWritten;
char szBuffer[101];
static HANDLE hFileHandle = INVALID_HANDLE_VALUE;serial.Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead);
if (dwBytesRead > 0)
{
szBuffer[dwBytesRead] = '\0';
hFileHandle = CreateFile(szLogFileName, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL);
SetFilePointer( hFileHandle, 0, NULL, FILE_END);
WriteFile(hFileHandle, (LPCVOID) szBuffer, dwBytesRead+1, &dwBytesWritten, NULL);
printf("%s", szBuffer);
fclose(out);// Check if EOF (CTRL+'[') has been specified
if (strchr(szBuffer,EOF_Char))
fContinue = false;}
-
I can't see a problem with your code. As Cedric said, your code will overwrite anything that is already in the file. You can work round this by opening the file then moving to the end before writing, something like this:
DWORD dwBytesRead = 0, dwBytesWritten;
char szBuffer[101];
static HANDLE hFileHandle = INVALID_HANDLE_VALUE;serial.Read(szBuffer,sizeof(szBuffer)-1,&dwBytesRead);
if (dwBytesRead > 0)
{
szBuffer[dwBytesRead] = '\0';
hFileHandle = CreateFile(szLogFileName, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL);
SetFilePointer( hFileHandle, 0, NULL, FILE_END);
WriteFile(hFileHandle, (LPCVOID) szBuffer, dwBytesRead+1, &dwBytesWritten, NULL);
printf("%s", szBuffer);
fclose(out);// Check if EOF (CTRL+'[') has been specified
if (strchr(szBuffer,EOF_Char))
fContinue = false;}
yes, it finally works. Thank you. But what if i want all of the data that i receive do be written on the same line? How can i strip the string that i receive of it's newline and carriage return?
-
yes, it finally works. Thank you. But what if i want all of the data that i receive do be written on the same line? How can i strip the string that i receive of it's newline and carriage return?
If you know that the every line you receive ends in CR/LF (or LF/CR), you could just replace the line:
szBuffer[dwBytesRead] = '\0';
with the following:
szBuffer[dwBytesRead-2] = '\0';
It is probably better to check, so I would do:
if (dwBytesRead > 0) // Your existing code
{
// strip CR/LF if it exists
if (dwBytesRead > 1 &&
(szBuffer[dwBytesRead-2] == 0x0d || szBuffer[dwBytesRead-2] == 0x0a) &&
&& (szBuffer[dwBytesRead-1] == 0x0a || szBuffer[dwBytesRead-1] == 0x0d))
{
szBuffer[dwBytesRead-2] = '\0';
}
else
szBuffer[dwBytesRead] = '\0';