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 and write in file

Read and write in file

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionhelp
16 Posts 5 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.
  • P polopo

    Hi All How can i read and write in .txt file in VC++(MFC)?Plz help me

    singh

    S Offline
    S Offline
    santhoshv84
    wrote on last edited by
    #2

    Hi, You can use CFile, like this you open the file..

    char* pszFileName = "c:\\test\\myfile.dat";
    CFile myFile;
    CFileException fileException;

    if ( !myFile.Open( pszFileName, CFile::modeCreate |
    CFile::modeReadWrite, &fileException ) )

    like this you can read and write..

    char szBuffer[256];
    UINT nActual = 0;
    CFile myFile;

    myFile.Write( szBuffer, sizeof( szBuffer ) );
    myFile.Seek( 0, CFile::begin );
    nActual = myFile.Read( szBuffer, sizeof( szBuffer ) );

    Like this you close the file..

    myFile.Close();

    The price of anything is the amount of life you exchange for it. Thanks and Regards. SANTHOSH V

    1 Reply Last reply
    0
    • P polopo

      Hi All How can i read and write in .txt file in VC++(MFC)?Plz help me

      singh

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #3

      CStdioFile::ReadString CStdioFile::WriteString

      Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

      P 2 Replies Last reply
      0
      • _ _AnsHUMAN_

        CStdioFile::ReadString CStdioFile::WriteString

        Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

        P Offline
        P Offline
        polopo
        wrote on last edited by
        #4

        i try both example but nothing is write in file..File is open but data is not write there? Plz help me

        singh

        T 1 Reply Last reply
        0
        • P polopo

          i try both example but nothing is write in file..File is open but data is not write there? Plz help me

          singh

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

          show your code dude !

          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          C 1 Reply Last reply
          0
          • _ _AnsHUMAN_

            CStdioFile::ReadString CStdioFile::WriteString

            Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

            P Offline
            P Offline
            polopo
            wrote on last edited by
            #6

            Thx's all I was wrong.. It's working.. THx's for help

            singh

            1 Reply Last reply
            0
            • T toxcct

              show your code dude !

              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

              C Offline
              C Offline
              cpvc
              wrote on last edited by
              #7

              Hi I more problem when new is came then old line is delete.But i don't want to delete old line..Plz help me

              T 1 Reply Last reply
              0
              • C cpvc

                Hi I more problem when new is came then old line is delete.But i don't want to delete old line..Plz help me

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #8

                cpvc++ wrote:

                Plz help me

                F*CK, you don't reply to my questions, and still you're asking for help ?!!! :mad:

                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                C 1 Reply Last reply
                0
                • T toxcct

                  cpvc++ wrote:

                  Plz help me

                  F*CK, you don't reply to my questions, and still you're asking for help ?!!! :mad:

                  [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                  C Offline
                  C Offline
                  cpvc
                  wrote on last edited by
                  #9

                  Hey what question Man?I am asking first time..

                  T 1 Reply Last reply
                  0
                  • C cpvc

                    Hey what question Man?I am asking first time..

                    T Offline
                    T Offline
                    toxcct
                    wrote on last edited by
                    #10

                    Does this post mean something to you ?[^]

                    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                    C 1 Reply Last reply
                    0
                    • T toxcct

                      Does this post mean something to you ?[^]

                      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

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

                      Mr. toxcct

                      I am not Mr. polop which is start this thread.I am different user and it's post help me and i want some more help me..Sorry for missunderstanding any way my code is here..Pla help me

                      CStdioFile file;
                      if (file.Open("\\log.txt", CFile::modeCreate|CFile::modeWrite ))
                      {
                      file.WriteString(logfile);

                      	file.WriteString("\\n");
                      	
                      }
                      file.Close();
                      

                      logfile is CString type which have some information.. Now i am able to write in file but old line is delete when new line write. Plz help me

                      T 1 Reply Last reply
                      0
                      • C cpvc

                        Mr. toxcct

                        I am not Mr. polop which is start this thread.I am different user and it's post help me and i want some more help me..Sorry for missunderstanding any way my code is here..Pla help me

                        CStdioFile file;
                        if (file.Open("\\log.txt", CFile::modeCreate|CFile::modeWrite ))
                        {
                        file.WriteString(logfile);

                        	file.WriteString("\\n");
                        	
                        }
                        file.Close();
                        

                        logfile is CString type which have some information.. Now i am able to write in file but old line is delete when new line write. Plz help me

                        T Offline
                        T Offline
                        toxcct
                        wrote on last edited by
                        #12

                        cpvc++ wrote:

                        I am not Mr. polop which is start this thread

                        sorry, I didn't notice that aspect of the things :) The problem in your code is that you're using CFile::modeCreate, but according to the documentation[^], this flag tells to truncate the file if already existing :

                        MSDN says:

                        CFile::modeCreate Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length

                        So, you have to add the flag CFile::modeNoTruncate to counter this undesired behavior. The use of this flag however keeps the file pointer at the beginning of the file. So if you want to append things at the end, you must call once after CStdioFile::Open() the function CStdioFile::Seek(CFile::end). Does the following work better ?

                        CStdioFile file;
                        if (file.Open(_T("\\log.txt"), CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate)) {
                        //file.Seek(0, CFile::end);
                        file.SeekToEnd();
                        file.WriteString(logfile);
                        file.WriteString(_T("\n"));
                        file.Close();
                        }

                        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                        modified on Thursday, September 4, 2008 6:46 AM

                        C 1 Reply Last reply
                        0
                        • T toxcct

                          cpvc++ wrote:

                          I am not Mr. polop which is start this thread

                          sorry, I didn't notice that aspect of the things :) The problem in your code is that you're using CFile::modeCreate, but according to the documentation[^], this flag tells to truncate the file if already existing :

                          MSDN says:

                          CFile::modeCreate Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length

                          So, you have to add the flag CFile::modeNoTruncate to counter this undesired behavior. The use of this flag however keeps the file pointer at the beginning of the file. So if you want to append things at the end, you must call once after CStdioFile::Open() the function CStdioFile::Seek(CFile::end). Does the following work better ?

                          CStdioFile file;
                          if (file.Open(_T("\\log.txt"), CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate)) {
                          //file.Seek(0, CFile::end);
                          file.SeekToEnd();
                          file.WriteString(logfile);
                          file.WriteString(_T("\n"));
                          file.Close();
                          }

                          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                          modified on Thursday, September 4, 2008 6:46 AM

                          C Offline
                          C Offline
                          cpvc
                          wrote on last edited by
                          #13

                          ok when i am useing your code then i am geting error..Plz help me

                          error C2660: 'CStdioFile::Open' : function does not take 1 arguments
                          error C2660: 'CStdioFile::Seek' : function does not take 1 arguments
                          error C2065: 'logfile' : undeclared identifier1>
                          error C2660: 'CStdioFile::Open' : function does not take 1 arguments
                          error C2660: 'CStdioFile::Seek' : function does not take 1 arguments
                          error C2065: 'logfile' : undeclared identifier

                          T 1 Reply Last reply
                          0
                          • C cpvc

                            ok when i am useing your code then i am geting error..Plz help me

                            error C2660: 'CStdioFile::Open' : function does not take 1 arguments
                            error C2660: 'CStdioFile::Seek' : function does not take 1 arguments
                            error C2065: 'logfile' : undeclared identifier1>
                            error C2660: 'CStdioFile::Open' : function does not take 1 arguments
                            error C2660: 'CStdioFile::Seek' : function does not take 1 arguments
                            error C2065: 'logfile' : undeclared identifier

                            T Offline
                            T Offline
                            toxcct
                            wrote on last edited by
                            #14

                            god, I made a typo. you shouldn't have read

                            if (_T(file.Open("\\log.txt"), CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate)) {

                            but this :

                            if (file.Open(_T("\\log.txt"), CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate)) {

                            I fixed the previous post BTW. and sorry, I am at work, and I have no compiler, so I couldn't test it before posting...

                            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                            C 1 Reply Last reply
                            0
                            • T toxcct

                              god, I made a typo. you shouldn't have read

                              if (_T(file.Open("\\log.txt"), CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate)) {

                              but this :

                              if (file.Open(_T("\\log.txt"), CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate)) {

                              I fixed the previous post BTW. and sorry, I am at work, and I have no compiler, so I couldn't test it before posting...

                              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                              C Offline
                              C Offline
                              cpvc
                              wrote on last edited by
                              #15

                              i was read but i think both have make same mistake..And mistake is

                              file.Seek(CFile::end)

                              this is not right and right one is this

                              file.Seek(0,CFile::end)

                              Any way thxs for help..

                              T 1 Reply Last reply
                              0
                              • C cpvc

                                i was read but i think both have make same mistake..And mistake is

                                file.Seek(CFile::end)

                                this is not right and right one is this

                                file.Seek(0,CFile::end)

                                Any way thxs for help..

                                T Offline
                                T Offline
                                toxcct
                                wrote on last edited by
                                #16

                                cpvc++ wrote:

                                this is not right and right one is this file.Seek(0,CFile::end)

                                Yes you're right. BTW, you still can call CFile::SeekToEnd() which takes no parameters, and moves directly to the end of file. anyway, does it work now for you ?

                                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                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