Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. read/ write to screen/ file

read/ write to screen/ file

Scheduled Pinned Locked Moved C / C++ / MFC
com
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tanarnelinistit
    wrote on last edited by
    #1

    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; }

    C N 2 Replies Last reply
    0
    • T tanarnelinistit

      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; }

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      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

      T 1 Reply Last reply
      0
      • C Cedric Moonen

        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

        T Offline
        T Offline
        tanarnelinistit
        wrote on last edited by
        #3

        I've also tried that version, and I agree with you, but it still doesn't work.

        1 Reply Last reply
        0
        • T tanarnelinistit

          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; }

          N Offline
          N Offline
          normanS
          wrote on last edited by
          #4

          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;

          }

          T 1 Reply Last reply
          0
          • N normanS

            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;

            }

            T Offline
            T Offline
            tanarnelinistit
            wrote on last edited by
            #5

            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?

            N 1 Reply Last reply
            0
            • T tanarnelinistit

              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?

              N Offline
              N Offline
              normanS
              wrote on last edited by
              #6

              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';

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups