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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. CFile Insert not Write...

CFile Insert not Write...

Scheduled Pinned Locked Moved ATL / WTL / STL
helptutorialdatabase
6 Posts 2 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.
  • L Offline
    L Offline
    loid grey manuel
    wrote on last edited by
    #1

    hello, i'm having a hard time figuring out how to insert a character/string on my CFile, i have no problem writing the character set at the end or at the beginning of the file, but when i try to add it in between, it overwrites the file. Example: i have "JON SMITH" on my file, when i try to insert "H" at index 2, it should look like this "JOHN SMITH", but what happens is this "JOH SMITH" it overwrites, it does not insert. here is my code

    //write to file
    CString txtInsert;
    m_txtInsert.GetWindowText(txtInsert);
    int szSize = txtInsert.GetLength();

    //create the file
    if (!cfile\_object.Open(l\_ExePath + "Logs\\\\cfile\_example.txt", CFile::modeWrite))
    {
    	AfxMessageBox(\_T("Cannot Open File for Reading!\\nPls. Check Directory of File!"));
    }
    else
    {
    cfile\_object.Seek( 2, CFile::begin );
    cfile\_object.Write(txtInsert, szSize);
    //close file
    cfile\_object.Close();
    

    i needed to type "H SMITH" just to get the result "JOHN SMITH" but all i want is the insert the character "H" on the right place.. any help or hints will be much appreciated, thankyou

    L 1 Reply Last reply
    0
    • L loid grey manuel

      hello, i'm having a hard time figuring out how to insert a character/string on my CFile, i have no problem writing the character set at the end or at the beginning of the file, but when i try to add it in between, it overwrites the file. Example: i have "JON SMITH" on my file, when i try to insert "H" at index 2, it should look like this "JOHN SMITH", but what happens is this "JOH SMITH" it overwrites, it does not insert. here is my code

      //write to file
      CString txtInsert;
      m_txtInsert.GetWindowText(txtInsert);
      int szSize = txtInsert.GetLength();

      //create the file
      if (!cfile\_object.Open(l\_ExePath + "Logs\\\\cfile\_example.txt", CFile::modeWrite))
      {
      	AfxMessageBox(\_T("Cannot Open File for Reading!\\nPls. Check Directory of File!"));
      }
      else
      {
      cfile\_object.Seek( 2, CFile::begin );
      cfile\_object.Write(txtInsert, szSize);
      //close file
      cfile\_object.Close();
      

      i needed to type "H SMITH" just to get the result "JOHN SMITH" but all i want is the insert the character "H" on the right place.. any help or hints will be much appreciated, thankyou

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

      File I/O does not work this way, you can only overwrite the data that is already there. You can also add more information at the end by appending data to the existing file. However, if you want to modify what is already there then you must read it in, modify the contents in memory and write out the new information, either to the original, thus overwriting it, or to a new file.

      Just say 'NO' to evaluated arguments for diadic functions! Ash

      L 1 Reply Last reply
      0
      • L Lost User

        File I/O does not work this way, you can only overwrite the data that is already there. You can also add more information at the end by appending data to the existing file. However, if you want to modify what is already there then you must read it in, modify the contents in memory and write out the new information, either to the original, thus overwriting it, or to a new file.

        Just say 'NO' to evaluated arguments for diadic functions! Ash

        L Offline
        L Offline
        loid grey manuel
        wrote on last edited by
        #3

        resolution: READ from whatever starting pointing from your file, save to buffer, then add whatever character you want at the beginning of the buffer. then WRITE the buffer to your file, this way you can insert...

        //points to index where to start reading
        cfile_object.Seek( insIndex, CFile::begin );

        //Reads the content of file starting at the SEEK index, in my case its index \[2\]..
        int flen = cfile\_object.GetLength();
        char \*fBuf= new char\[flen\];
        
        UINT file = cfile\_object.Read(fBuf,flen);
        fBuf\[file\] = '\\0';
        //Joins the user input and the file read..
        txtInsert = txtInsert + fBuf;
        
        //Points to index where to insert, in my case its index \[2\]..
        cfile\_object.Seek( insIndex, CFile::begin );
        
        //writes to file where index pointer is..
            cfile\_object.Write(txtInsert, szSize + file);
        
        //close file
        cfile\_object.Close();
        

        modified on Tuesday, September 28, 2010 2:53 AM

        L 1 Reply Last reply
        0
        • L loid grey manuel

          resolution: READ from whatever starting pointing from your file, save to buffer, then add whatever character you want at the beginning of the buffer. then WRITE the buffer to your file, this way you can insert...

          //points to index where to start reading
          cfile_object.Seek( insIndex, CFile::begin );

          //Reads the content of file starting at the SEEK index, in my case its index \[2\]..
          int flen = cfile\_object.GetLength();
          char \*fBuf= new char\[flen\];
          
          UINT file = cfile\_object.Read(fBuf,flen);
          fBuf\[file\] = '\\0';
          //Joins the user input and the file read..
          txtInsert = txtInsert + fBuf;
          
          //Points to index where to insert, in my case its index \[2\]..
          cfile\_object.Seek( insIndex, CFile::begin );
          
          //writes to file where index pointer is..
              cfile\_object.Write(txtInsert, szSize + file);
          
          //close file
          cfile\_object.Close();
          

          modified on Tuesday, September 28, 2010 2:53 AM

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

          As I explained in my previous answer there is no 'insert' facility in file I/O, it has to be implemented by reading the data into memory, modifying the memory buffer(s) and rewriting all of the file from the point of insertion. The line

          fBuf[file] = '\0';

          will cause random memory corruption. I am not sure what the line

          txtInsert = txtInsert + fBuf;

          is supposed to do, but I doubt that it is what you expect.

          Just say 'NO' to evaluated arguments for diadic functions! Ash

          L 1 Reply Last reply
          0
          • L Lost User

            As I explained in my previous answer there is no 'insert' facility in file I/O, it has to be implemented by reading the data into memory, modifying the memory buffer(s) and rewriting all of the file from the point of insertion. The line

            fBuf[file] = '\0';

            will cause random memory corruption. I am not sure what the line

            txtInsert = txtInsert + fBuf;

            is supposed to do, but I doubt that it is what you expect.

            Just say 'NO' to evaluated arguments for diadic functions! Ash

            L Offline
            L Offline
            loid grey manuel
            wrote on last edited by
            #5

            Well i have to agree with everything that you have written, i am totally new in visual C++... unlike the .NET that i came from, coding is more easier there, but MFC just gives me what i needed, MFC gives me "CONTROL OVER MY CODE" i am very thankful about your comments, i got a good hint how to control my Project... thanks a lot for posting,. the bigger problem that i face now is how to SORT the record on the CFile., i needed real time sorting for my main project.. every-time a user input is made, the record should be automatically sorted A-Z.. how to sort:

            BANANA
            APPLE
            ORANGE
            MANGO
            PINEAPPLE

            and i got NO idea how to do that, any help would be very much appreciated

            L 1 Reply Last reply
            0
            • L loid grey manuel

              Well i have to agree with everything that you have written, i am totally new in visual C++... unlike the .NET that i came from, coding is more easier there, but MFC just gives me what i needed, MFC gives me "CONTROL OVER MY CODE" i am very thankful about your comments, i got a good hint how to control my Project... thanks a lot for posting,. the bigger problem that i face now is how to SORT the record on the CFile., i needed real time sorting for my main project.. every-time a user input is made, the record should be automatically sorted A-Z.. how to sort:

              BANANA
              APPLE
              ORANGE
              MANGO
              PINEAPPLE

              and i got NO idea how to do that, any help would be very much appreciated

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

              loid grey manuel wrote:

              i got NO idea how to do that

              Well, this is the place we all had to start from, but it does require you to spend some time learning. If you are serious about writing C++ programs then you should learn some of the features that come with it, such as the Standard C++ Library[^]. There are also lots of useful classes in MFC if you are using those libraries; Here[^] is an article that should help you with your sorting problem. In either (or both) case(s) you should get familiar with the MSDN[^] site to help you find answers to many of the problems you will meet on the way.

              Just say 'NO' to evaluated arguments for diadic functions! Ash

              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