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. Other Discussions
  3. The Weird and The Wonderful
  4. Writing to a Text File 101

Writing to a Text File 101

Scheduled Pinned Locked Moved The Weird and The Wonderful
11 Posts 9 Posters 1 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.
  • Q Offline
    Q Offline
    QuiJohn
    wrote on last edited by
    #1

    for(i = 0; i < num; i++)
    {
    // Add data from each entry, each line is EXACTLY 105 bytes
    strIntString.Format(_T("%3d,%#9.*f,%#9.*f,%#9.*f,%#9.*f,%#6.*f,%#6.*f,%#6.*f,%#6.*f,%02d:%02d:%02d,%3d,%4d,%1d,%5d,%1d,%3d\r\n"),
    newObject->m_nIndex,
    temp.prec[0], newObject->val1[0], temp.prec[1], newObject->val1[1],
    temp.prec[2], newObject->val1[2], temp.prec[3], newObject->val1[3],
    temp.prec2[0], newObject->val2[0], temp.prec2[1], newObject->val2[1],
    temp.prec2[2], newObject->val2[2], temp.prec2[3], newObject->val2[3],
    newObject->m_nHours, newObject->m_nMinutes, newObject->m_nSeconds,
    newObject->m_nNext, newObject->m_nTotal, newObject->m_nGroup,
    newObject->m_Extra, newObject->m_nMode, options);

      //write this line out to the file
      numOut=strIntString.GetLength();
      m\_File->Seek(HEADERLEN+(105\*i),CFile::begin);
      m\_File->Write(strIntString,numOut);
      strIntString.Empty();
    

    }

    Guess what happens when any of the variables has more digits than was allocated in the Format call. Right, the line is no longer "EXACTLY" 105 bytes. File becomes a messy pile of bytes. The only thing I changed from the actual code were the variable names. This is in production code, currently being run by thousands of customers. You should see how these files are read in. It's worse, but too long to post. Sigh.


    He said, "Boy I'm just old and lonely, But thank you for your concern, Here's wishing you a Happy New Year." I wished him one back in return.

    A R D M C 6 Replies Last reply
    0
    • Q QuiJohn

      for(i = 0; i < num; i++)
      {
      // Add data from each entry, each line is EXACTLY 105 bytes
      strIntString.Format(_T("%3d,%#9.*f,%#9.*f,%#9.*f,%#9.*f,%#6.*f,%#6.*f,%#6.*f,%#6.*f,%02d:%02d:%02d,%3d,%4d,%1d,%5d,%1d,%3d\r\n"),
      newObject->m_nIndex,
      temp.prec[0], newObject->val1[0], temp.prec[1], newObject->val1[1],
      temp.prec[2], newObject->val1[2], temp.prec[3], newObject->val1[3],
      temp.prec2[0], newObject->val2[0], temp.prec2[1], newObject->val2[1],
      temp.prec2[2], newObject->val2[2], temp.prec2[3], newObject->val2[3],
      newObject->m_nHours, newObject->m_nMinutes, newObject->m_nSeconds,
      newObject->m_nNext, newObject->m_nTotal, newObject->m_nGroup,
      newObject->m_Extra, newObject->m_nMode, options);

        //write this line out to the file
        numOut=strIntString.GetLength();
        m\_File->Seek(HEADERLEN+(105\*i),CFile::begin);
        m\_File->Write(strIntString,numOut);
        strIntString.Empty();
      

      }

      Guess what happens when any of the variables has more digits than was allocated in the Format call. Right, the line is no longer "EXACTLY" 105 bytes. File becomes a messy pile of bytes. The only thing I changed from the actual code were the variable names. This is in production code, currently being run by thousands of customers. You should see how these files are read in. It's worse, but too long to post. Sigh.


      He said, "Boy I'm just old and lonely, But thank you for your concern, Here's wishing you a Happy New Year." I wished him one back in return.

      A Offline
      A Offline
      AspDotNetDev
      wrote on last edited by
      #2

      David Kentley wrote:

      when any of the variables has more digits than was allocated in the Format call

      Does that ever actually happen?

      [Forum Guidelines]

      Q 1 Reply Last reply
      0
      • A AspDotNetDev

        David Kentley wrote:

        when any of the variables has more digits than was allocated in the Format call

        Does that ever actually happen?

        [Forum Guidelines]

        Q Offline
        Q Offline
        QuiJohn
        wrote on last edited by
        #3

        aspdotnetdev wrote:

        Does that ever actually happen?

        Yes, as I discovered today when I had to track down a customer reported bug. It is an usual case, but it's completely possible and even legitimate. I had no idea what I would find under the rock I kicked over.


        He said, "Boy I'm just old and lonely, But thank you for your concern, Here's wishing you a Happy New Year." I wished him one back in return.

        1 Reply Last reply
        0
        • Q QuiJohn

          for(i = 0; i < num; i++)
          {
          // Add data from each entry, each line is EXACTLY 105 bytes
          strIntString.Format(_T("%3d,%#9.*f,%#9.*f,%#9.*f,%#9.*f,%#6.*f,%#6.*f,%#6.*f,%#6.*f,%02d:%02d:%02d,%3d,%4d,%1d,%5d,%1d,%3d\r\n"),
          newObject->m_nIndex,
          temp.prec[0], newObject->val1[0], temp.prec[1], newObject->val1[1],
          temp.prec[2], newObject->val1[2], temp.prec[3], newObject->val1[3],
          temp.prec2[0], newObject->val2[0], temp.prec2[1], newObject->val2[1],
          temp.prec2[2], newObject->val2[2], temp.prec2[3], newObject->val2[3],
          newObject->m_nHours, newObject->m_nMinutes, newObject->m_nSeconds,
          newObject->m_nNext, newObject->m_nTotal, newObject->m_nGroup,
          newObject->m_Extra, newObject->m_nMode, options);

            //write this line out to the file
            numOut=strIntString.GetLength();
            m\_File->Seek(HEADERLEN+(105\*i),CFile::begin);
            m\_File->Write(strIntString,numOut);
            strIntString.Empty();
          

          }

          Guess what happens when any of the variables has more digits than was allocated in the Format call. Right, the line is no longer "EXACTLY" 105 bytes. File becomes a messy pile of bytes. The only thing I changed from the actual code were the variable names. This is in production code, currently being run by thousands of customers. You should see how these files are read in. It's worse, but too long to post. Sigh.


          He said, "Boy I'm just old and lonely, But thank you for your concern, Here's wishing you a Happy New Year." I wished him one back in return.

          R Offline
          R Offline
          RugbyLeague
          wrote on last edited by
          #4

          rewrite it in COBOL, it's good at that sort of stuff ;P

          1 Reply Last reply
          0
          • Q QuiJohn

            for(i = 0; i < num; i++)
            {
            // Add data from each entry, each line is EXACTLY 105 bytes
            strIntString.Format(_T("%3d,%#9.*f,%#9.*f,%#9.*f,%#9.*f,%#6.*f,%#6.*f,%#6.*f,%#6.*f,%02d:%02d:%02d,%3d,%4d,%1d,%5d,%1d,%3d\r\n"),
            newObject->m_nIndex,
            temp.prec[0], newObject->val1[0], temp.prec[1], newObject->val1[1],
            temp.prec[2], newObject->val1[2], temp.prec[3], newObject->val1[3],
            temp.prec2[0], newObject->val2[0], temp.prec2[1], newObject->val2[1],
            temp.prec2[2], newObject->val2[2], temp.prec2[3], newObject->val2[3],
            newObject->m_nHours, newObject->m_nMinutes, newObject->m_nSeconds,
            newObject->m_nNext, newObject->m_nTotal, newObject->m_nGroup,
            newObject->m_Extra, newObject->m_nMode, options);

              //write this line out to the file
              numOut=strIntString.GetLength();
              m\_File->Seek(HEADERLEN+(105\*i),CFile::begin);
              m\_File->Write(strIntString,numOut);
              strIntString.Empty();
            

            }

            Guess what happens when any of the variables has more digits than was allocated in the Format call. Right, the line is no longer "EXACTLY" 105 bytes. File becomes a messy pile of bytes. The only thing I changed from the actual code were the variable names. This is in production code, currently being run by thousands of customers. You should see how these files are read in. It's worse, but too long to post. Sigh.


            He said, "Boy I'm just old and lonely, But thank you for your concern, Here's wishing you a Happy New Year." I wished him one back in return.

            D Offline
            D Offline
            Daniel Tak M
            wrote on last edited by
            #5

            Such constructs aren't so rare... in a company I worked years ago, we had to import lots of different file formats, and I'd say, 50% of them used a similar "design". Not to mention that our fantastic database also almost worked like that - yay! No wonder that company doesn't exist anymore.

            T 1 Reply Last reply
            0
            • D Daniel Tak M

              Such constructs aren't so rare... in a company I worked years ago, we had to import lots of different file formats, and I'd say, 50% of them used a similar "design". Not to mention that our fantastic database also almost worked like that - yay! No wonder that company doesn't exist anymore.

              T Offline
              T Offline
              Tom Chantler
              wrote on last edited by
              #6

              Doesn't the fact that the company is now defunct mean that such constructs are, at least, getting rarer...? :)

              L 1 Reply Last reply
              0
              • T Tom Chantler

                Doesn't the fact that the company is now defunct mean that such constructs are, at least, getting rarer...? :)

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Only in theory. For every horrible coder out of the race two new ones come to replace him. And the odds are good that the one who did this is still at large somewhere out there.

                A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

                T 1 Reply Last reply
                0
                • L Lost User

                  Only in theory. For every horrible coder out of the race two new ones come to replace him. And the odds are good that the one who did this is still at large somewhere out there.

                  A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

                  T Offline
                  T Offline
                  Tom Chantler
                  wrote on last edited by
                  #8

                  It's enough to make ya cry into your beer... :(( P.S. I like your sig.

                  1 Reply Last reply
                  0
                  • Q QuiJohn

                    for(i = 0; i < num; i++)
                    {
                    // Add data from each entry, each line is EXACTLY 105 bytes
                    strIntString.Format(_T("%3d,%#9.*f,%#9.*f,%#9.*f,%#9.*f,%#6.*f,%#6.*f,%#6.*f,%#6.*f,%02d:%02d:%02d,%3d,%4d,%1d,%5d,%1d,%3d\r\n"),
                    newObject->m_nIndex,
                    temp.prec[0], newObject->val1[0], temp.prec[1], newObject->val1[1],
                    temp.prec[2], newObject->val1[2], temp.prec[3], newObject->val1[3],
                    temp.prec2[0], newObject->val2[0], temp.prec2[1], newObject->val2[1],
                    temp.prec2[2], newObject->val2[2], temp.prec2[3], newObject->val2[3],
                    newObject->m_nHours, newObject->m_nMinutes, newObject->m_nSeconds,
                    newObject->m_nNext, newObject->m_nTotal, newObject->m_nGroup,
                    newObject->m_Extra, newObject->m_nMode, options);

                      //write this line out to the file
                      numOut=strIntString.GetLength();
                      m\_File->Seek(HEADERLEN+(105\*i),CFile::begin);
                      m\_File->Write(strIntString,numOut);
                      strIntString.Empty();
                    

                    }

                    Guess what happens when any of the variables has more digits than was allocated in the Format call. Right, the line is no longer "EXACTLY" 105 bytes. File becomes a messy pile of bytes. The only thing I changed from the actual code were the variable names. This is in production code, currently being run by thousands of customers. You should see how these files are read in. It's worse, but too long to post. Sigh.


                    He said, "Boy I'm just old and lonely, But thank you for your concern, Here's wishing you a Happy New Year." I wished him one back in return.

                    M Offline
                    M Offline
                    Momony
                    wrote on last edited by
                    #9

                    At least that block of code had line breaks. Just imagine trying to read that as one continuous line... I find the best way to have a positive attitude is to have extremely low expectations :)

                    1 Reply Last reply
                    0
                    • Q QuiJohn

                      for(i = 0; i < num; i++)
                      {
                      // Add data from each entry, each line is EXACTLY 105 bytes
                      strIntString.Format(_T("%3d,%#9.*f,%#9.*f,%#9.*f,%#9.*f,%#6.*f,%#6.*f,%#6.*f,%#6.*f,%02d:%02d:%02d,%3d,%4d,%1d,%5d,%1d,%3d\r\n"),
                      newObject->m_nIndex,
                      temp.prec[0], newObject->val1[0], temp.prec[1], newObject->val1[1],
                      temp.prec[2], newObject->val1[2], temp.prec[3], newObject->val1[3],
                      temp.prec2[0], newObject->val2[0], temp.prec2[1], newObject->val2[1],
                      temp.prec2[2], newObject->val2[2], temp.prec2[3], newObject->val2[3],
                      newObject->m_nHours, newObject->m_nMinutes, newObject->m_nSeconds,
                      newObject->m_nNext, newObject->m_nTotal, newObject->m_nGroup,
                      newObject->m_Extra, newObject->m_nMode, options);

                        //write this line out to the file
                        numOut=strIntString.GetLength();
                        m\_File->Seek(HEADERLEN+(105\*i),CFile::begin);
                        m\_File->Write(strIntString,numOut);
                        strIntString.Empty();
                      

                      }

                      Guess what happens when any of the variables has more digits than was allocated in the Format call. Right, the line is no longer "EXACTLY" 105 bytes. File becomes a messy pile of bytes. The only thing I changed from the actual code were the variable names. This is in production code, currently being run by thousands of customers. You should see how these files are read in. It's worse, but too long to post. Sigh.


                      He said, "Boy I'm just old and lonely, But thank you for your concern, Here's wishing you a Happy New Year." I wished him one back in return.

                      C Offline
                      C Offline
                      cpkilekofp
                      wrote on last edited by
                      #10

                      Sometimes you just have to hope for the best until you get the next job - at least, this was the credo of more than one programmer who I've met in person or simply in the trash they left behind :sigh:

                      1 Reply Last reply
                      0
                      • Q QuiJohn

                        for(i = 0; i < num; i++)
                        {
                        // Add data from each entry, each line is EXACTLY 105 bytes
                        strIntString.Format(_T("%3d,%#9.*f,%#9.*f,%#9.*f,%#9.*f,%#6.*f,%#6.*f,%#6.*f,%#6.*f,%02d:%02d:%02d,%3d,%4d,%1d,%5d,%1d,%3d\r\n"),
                        newObject->m_nIndex,
                        temp.prec[0], newObject->val1[0], temp.prec[1], newObject->val1[1],
                        temp.prec[2], newObject->val1[2], temp.prec[3], newObject->val1[3],
                        temp.prec2[0], newObject->val2[0], temp.prec2[1], newObject->val2[1],
                        temp.prec2[2], newObject->val2[2], temp.prec2[3], newObject->val2[3],
                        newObject->m_nHours, newObject->m_nMinutes, newObject->m_nSeconds,
                        newObject->m_nNext, newObject->m_nTotal, newObject->m_nGroup,
                        newObject->m_Extra, newObject->m_nMode, options);

                          //write this line out to the file
                          numOut=strIntString.GetLength();
                          m\_File->Seek(HEADERLEN+(105\*i),CFile::begin);
                          m\_File->Write(strIntString,numOut);
                          strIntString.Empty();
                        

                        }

                        Guess what happens when any of the variables has more digits than was allocated in the Format call. Right, the line is no longer "EXACTLY" 105 bytes. File becomes a messy pile of bytes. The only thing I changed from the actual code were the variable names. This is in production code, currently being run by thousands of customers. You should see how these files are read in. It's worse, but too long to post. Sigh.


                        He said, "Boy I'm just old and lonely, But thank you for your concern, Here's wishing you a Happy New Year." I wished him one back in return.

                        C Offline
                        C Offline
                        CDMTJX
                        wrote on last edited by
                        #11

                        I had something similar in production to fix. File had fixed length lines, every column meant something and couldn't change. But it did, suddenly some lines were one char too long, so some columns were invalid. A 2 char field was supposed to be 01, 02, or 03. An int was being used to calculate, and got incremented up to 98, 99, 100, .... More than 2 characters. The program was using C char* with strcpy and strcat, so this field being too long shifted the position of all following fields, bad data! I corrected the field to only be 1, 2 or 3 (zero prepended to make 2 chars), and added checks for the line length before outputting to a the required file. And starting using C++ class std::string instead of char* in my newer code to help my sanity.

                        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