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. how to write/read in text file with mfc project??

how to write/read in text file with mfc project??

Scheduled Pinned Locked Moved C / C++ / MFC
c++comhelptutorialquestion
17 Posts 7 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.
  • A Offline
    A Offline
    antonio343
    wrote on last edited by
    #1

    I'm trying to read text from a text file. I look for in msdn how to read and write in text file, and I found this: http://msdn.microsoft.com/es-es/library/6337eske.aspx[^] I made this funtion to read from my text file:

    void CDlgResultados::loadFromFile(void)
    {
    CString path= GetUserHomeDir() + _T("\\Documents\\file.txt");

    TCHAR	szBuffer\[256\]; 
    UINT    nActual = 0; 
    CFile	myFile;
    
    if ( myFile.Open(path, CFile::modeCreate |   
       CFile::modeRead ) )
    {
       myFile.Write( szBuffer, sizeof( szBuffer ) ); 
       myFile.Flush();
       myFile.Seek( 0, CFile::begin );
       nActual = myFile.Read( szBuffer, sizeof( szBuffer ) ); 
    }
    m\_Edit.SetWindowTextW(szBuffer);
    

    }

    But when I run the app and I press the button to read the file, I get a error that it said: You dont have acces to this file.

    L M C M 4 Replies Last reply
    0
    • A antonio343

      I'm trying to read text from a text file. I look for in msdn how to read and write in text file, and I found this: http://msdn.microsoft.com/es-es/library/6337eske.aspx[^] I made this funtion to read from my text file:

      void CDlgResultados::loadFromFile(void)
      {
      CString path= GetUserHomeDir() + _T("\\Documents\\file.txt");

      TCHAR	szBuffer\[256\]; 
      UINT    nActual = 0; 
      CFile	myFile;
      
      if ( myFile.Open(path, CFile::modeCreate |   
         CFile::modeRead ) )
      {
         myFile.Write( szBuffer, sizeof( szBuffer ) ); 
         myFile.Flush();
         myFile.Seek( 0, CFile::begin );
         nActual = myFile.Read( szBuffer, sizeof( szBuffer ) ); 
      }
      m\_Edit.SetWindowTextW(szBuffer);
      

      }

      But when I run the app and I press the button to read the file, I get a error that it said: You dont have acces to this file.

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

      What is the actual value of the string in your path variable, at the time you try to create the file?

      Unrequited desire is character building. OriginalGriff

      A 1 Reply Last reply
      0
      • L Lost User

        What is the actual value of the string in your path variable, at the time you try to create the file?

        Unrequited desire is character building. OriginalGriff

        A Offline
        A Offline
        antonio343
        wrote on last edited by
        #3

        this is the value:

        C:\Users\Antonio\Documents\file.txt

        L 1 Reply Last reply
        0
        • A antonio343

          I'm trying to read text from a text file. I look for in msdn how to read and write in text file, and I found this: http://msdn.microsoft.com/es-es/library/6337eske.aspx[^] I made this funtion to read from my text file:

          void CDlgResultados::loadFromFile(void)
          {
          CString path= GetUserHomeDir() + _T("\\Documents\\file.txt");

          TCHAR	szBuffer\[256\]; 
          UINT    nActual = 0; 
          CFile	myFile;
          
          if ( myFile.Open(path, CFile::modeCreate |   
             CFile::modeRead ) )
          {
             myFile.Write( szBuffer, sizeof( szBuffer ) ); 
             myFile.Flush();
             myFile.Seek( 0, CFile::begin );
             nActual = myFile.Read( szBuffer, sizeof( szBuffer ) ); 
          }
          m\_Edit.SetWindowTextW(szBuffer);
          

          }

          But when I run the app and I press the button to read the file, I get a error that it said: You dont have acces to this file.

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          Use the class CStdioFile to read text file. google for example, there are many.

          Watched code never compiles.

          1 Reply Last reply
          0
          • A antonio343

            I'm trying to read text from a text file. I look for in msdn how to read and write in text file, and I found this: http://msdn.microsoft.com/es-es/library/6337eske.aspx[^] I made this funtion to read from my text file:

            void CDlgResultados::loadFromFile(void)
            {
            CString path= GetUserHomeDir() + _T("\\Documents\\file.txt");

            TCHAR	szBuffer\[256\]; 
            UINT    nActual = 0; 
            CFile	myFile;
            
            if ( myFile.Open(path, CFile::modeCreate |   
               CFile::modeRead ) )
            {
               myFile.Write( szBuffer, sizeof( szBuffer ) ); 
               myFile.Flush();
               myFile.Seek( 0, CFile::begin );
               nActual = myFile.Read( szBuffer, sizeof( szBuffer ) ); 
            }
            m\_Edit.SetWindowTextW(szBuffer);
            

            }

            But when I run the app and I press the button to read the file, I get a error that it said: You dont have acces to this file.

            C Offline
            C Offline
            Chris Meech
            wrote on last edited by
            #5

            Rule number 0 when working with files. Always check return codes, so you know which call is failing. :) Best guess is that when you call to Write() to the file, you do not have permissions, since your Open() call doesn't specify that permission. Maybe the create Permission implies this, but I'm not sure. :)

            Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

            1 Reply Last reply
            0
            • A antonio343

              I'm trying to read text from a text file. I look for in msdn how to read and write in text file, and I found this: http://msdn.microsoft.com/es-es/library/6337eske.aspx[^] I made this funtion to read from my text file:

              void CDlgResultados::loadFromFile(void)
              {
              CString path= GetUserHomeDir() + _T("\\Documents\\file.txt");

              TCHAR	szBuffer\[256\]; 
              UINT    nActual = 0; 
              CFile	myFile;
              
              if ( myFile.Open(path, CFile::modeCreate |   
                 CFile::modeRead ) )
              {
                 myFile.Write( szBuffer, sizeof( szBuffer ) ); 
                 myFile.Flush();
                 myFile.Seek( 0, CFile::begin );
                 nActual = myFile.Read( szBuffer, sizeof( szBuffer ) ); 
              }
              m\_Edit.SetWindowTextW(szBuffer);
              

              }

              But when I run the app and I press the button to read the file, I get a error that it said: You dont have acces to this file.

              M Offline
              M Offline
              Madhu Nair 0
              wrote on last edited by
              #6

              antonio343 wrote:

              , I get a error that it said:
              You dont have acces to this file.

              Open the file using

              CFile::modeReadWrite OR CFile::modeWrite

              it seems you don't have permission to write.

              A 1 Reply Last reply
              0
              • A antonio343

                this is the value:

                C:\Users\Antonio\Documents\file.txt

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

                OK, and does that directory exist and can you create that file manually? If so then check also the suggestions from other commenters below.

                Unrequited desire is character building. OriginalGriff

                A 1 Reply Last reply
                0
                • M Madhu Nair 0

                  antonio343 wrote:

                  , I get a error that it said:
                  You dont have acces to this file.

                  Open the file using

                  CFile::modeReadWrite OR CFile::modeWrite

                  it seems you don't have permission to write.

                  A Offline
                  A Offline
                  antonio343
                  wrote on last edited by
                  #8

                  I changed the mode by modeReadWrite and I get simbols like this:

                  <pre>쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌﫸Y쳌쳌쳌쳌꣨ɷ쳌쳌偁泇¬</pre>

                  I also try this:

                  void CDlgResultados::loadFromFile(void)
                  {
                  CString path= GetUserHomeDir() + _T("\\Documents\\file.txt");

                  CStdioFile f1;
                  f1.Open(path, CFile::modeCreate | CFile::modeRead || CFile::typeText);
                  
                  CStdioFile f(stdin);
                  TCHAR buf\[100\];
                  
                  f1.ReadString(buf, 99);
                  
                  m\_Edit.SetWindowTextW(buf);
                  

                  }

                  But I get simbols. I think that the problem is the function ReadString doesnt get the text from the file, becouse the simbol that I get are the "rubbish" which is inicializated buf

                  L D 2 Replies Last reply
                  0
                  • L Lost User

                    OK, and does that directory exist and can you create that file manually? If so then check also the suggestions from other commenters below.

                    Unrequited desire is character building. OriginalGriff

                    A Offline
                    A Offline
                    antonio343
                    wrote on last edited by
                    #9

                    Yes, this directory exist and I made the file manually.

                    1 Reply Last reply
                    0
                    • A antonio343

                      I changed the mode by modeReadWrite and I get simbols like this:

                      <pre>쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌﫸Y쳌쳌쳌쳌꣨ɷ쳌쳌偁泇¬</pre>

                      I also try this:

                      void CDlgResultados::loadFromFile(void)
                      {
                      CString path= GetUserHomeDir() + _T("\\Documents\\file.txt");

                      CStdioFile f1;
                      f1.Open(path, CFile::modeCreate | CFile::modeRead || CFile::typeText);
                      
                      CStdioFile f(stdin);
                      TCHAR buf\[100\];
                      
                      f1.ReadString(buf, 99);
                      
                      m\_Edit.SetWindowTextW(buf);
                      

                      }

                      But I get simbols. I think that the problem is the function ReadString doesnt get the text from the file, becouse the simbol that I get are the "rubbish" which is inicializated buf

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

                      In your original code you were writing from an uninitialised buffer, hence the garbage recorded in your file. Check the logic of your code thus:

                      1. Create the file with access for writing
                      2. Write some text either from a constant string, or from some data built by the program
                      3. Close the file
                      4. Open the file with access for reading
                      5. Read the text into a buffer
                      6. Close the file

                      In each case check the results of your function calls to ensure that your code has completed without error. You can also create your file manually using Notepad or similar text editor.

                      Unrequited desire is character building. OriginalGriff

                      A 1 Reply Last reply
                      0
                      • L Lost User

                        In your original code you were writing from an uninitialised buffer, hence the garbage recorded in your file. Check the logic of your code thus:

                        1. Create the file with access for writing
                        2. Write some text either from a constant string, or from some data built by the program
                        3. Close the file
                        4. Open the file with access for reading
                        5. Read the text into a buffer
                        6. Close the file

                        In each case check the results of your function calls to ensure that your code has completed without error. You can also create your file manually using Notepad or similar text editor.

                        Unrequited desire is character building. OriginalGriff

                        A Offline
                        A Offline
                        antonio343
                        wrote on last edited by
                        #11

                        Yes, I know, I try to do that, and the file is created manually. But I dont know how to read or write in mfc.

                        L 1 Reply Last reply
                        0
                        • A antonio343

                          I changed the mode by modeReadWrite and I get simbols like this:

                          <pre>쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌﫸Y쳌쳌쳌쳌꣨ɷ쳌쳌偁泇¬</pre>

                          I also try this:

                          void CDlgResultados::loadFromFile(void)
                          {
                          CString path= GetUserHomeDir() + _T("\\Documents\\file.txt");

                          CStdioFile f1;
                          f1.Open(path, CFile::modeCreate | CFile::modeRead || CFile::typeText);
                          
                          CStdioFile f(stdin);
                          TCHAR buf\[100\];
                          
                          f1.ReadString(buf, 99);
                          
                          m\_Edit.SetWindowTextW(buf);
                          

                          }

                          But I get simbols. I think that the problem is the function ReadString doesnt get the text from the file, becouse the simbol that I get are the "rubbish" which is inicializated buf

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #12

                          antonio343 wrote:

                          f1.Open(path, CFile::modeCreate | CFile::modeRead || CFile::typeText); TCHAR buf[100]; f1.ReadString(buf, 99);

                          You are creating an empty file and then attempting to read from it. Is that really what you want? Also, you may want to revise your usage of the | and || operators.

                          "One man's wage rise is another man's price increase." - Harold Wilson

                          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                          "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                          A 1 Reply Last reply
                          0
                          • A antonio343

                            Yes, I know, I try to do that, and the file is created manually. But I dont know how to read or write in mfc.

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

                            See CFile[^], and in particular the Read() and Write() functions.

                            Unrequited desire is character building. OriginalGriff

                            1 Reply Last reply
                            0
                            • D David Crow

                              antonio343 wrote:

                              f1.Open(path, CFile::modeCreate | CFile::modeRead || CFile::typeText); TCHAR buf[100]; f1.ReadString(buf, 99);

                              You are creating an empty file and then attempting to read from it. Is that really what you want? Also, you may want to revise your usage of the | and || operators.

                              "One man's wage rise is another man's price increase." - Harold Wilson

                              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                              "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                              A Offline
                              A Offline
                              antonio343
                              wrote on last edited by
                              #14

                              Nooo. I have a text file that I created manually,and I'd like to read the text from it. I made a mistake when I write ||, but it is not the error

                              enhzflepE 1 Reply Last reply
                              0
                              • A antonio343

                                Nooo. I have a text file that I created manually,and I'd like to read the text from it. I made a mistake when I write ||, but it is not the error

                                enhzflepE Offline
                                enhzflepE Offline
                                enhzflep
                                wrote on last edited by
                                #15

                                `So, why are you asking for the the file to be Created?

                                if ( myFile.Open(path, CFile::modeCreate | CFile::modeRead ) )

                                | is a bitwise OR, || is a logical OR

                                A 1 Reply Last reply
                                0
                                • enhzflepE enhzflep

                                  `So, why are you asking for the the file to be Created?

                                  if ( myFile.Open(path, CFile::modeCreate | CFile::modeRead ) )

                                  | is a bitwise OR, || is a logical OR

                                  A Offline
                                  A Offline
                                  antonio343
                                  wrote on last edited by
                                  #16

                                  Yes you are right, I didnt know what mean this argument, if I dont put this argument, everything run rightly

                                  enhzflepE 1 Reply Last reply
                                  0
                                  • A antonio343

                                    Yes you are right, I didnt know what mean this argument, if I dont put this argument, everything run rightly

                                    enhzflepE Offline
                                    enhzflepE Offline
                                    enhzflep
                                    wrote on last edited by
                                    #17

                                    I continue to feel very fortunate that the vast majority of programming (that I see) is done in english. I understand that with function/variable names in my own language I am at an advantage to those for whom they are in a different language. :thumbsup:

                                    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