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. C / C++ / MFC
  4. saving text in a file?

saving text in a file?

Scheduled Pinned Locked Moved C / C++ / MFC
question
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.
  • M mpallavi

    hi.. how do we save user entered text in a file.. i am using common dialog box and allowing the user to specify the file name and the location.. I used CArchive.. The first character in the saved file is always a garbage value.. Why? regards mpallavi

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

    If you use CArchive, maybe that's normal... In fact, I never used CArchive for saving data but maybe it adds its own data before your data. Try to use CFile instead: char* szText = "Your string To Save"; CFile* pFile = fopen("YourFile.txt","w"); fwrite(szText,strlen(szText),1,pFile); fclose(pFile);

    M 1 Reply Last reply
    0
    • M mpallavi

      hi.. how do we save user entered text in a file.. i am using common dialog box and allowing the user to specify the file name and the location.. I used CArchive.. The first character in the saved file is always a garbage value.. Why? regards mpallavi

      E Offline
      E Offline
      eli15021979
      wrote on last edited by
      #3

      try this :

      FILE *file;
      char *FileName = "C:\\TestFile.txt";
      char *string_to_save = "this is a test";

      file = fopen(FileName,"w");
      if(!file)
      {
      AfxMessageBox("Unable to open the specified file");
      return;
      }

      fputs(string_to_save,file);
      fputs("\n",file);
      fclose(file)
      return;

      regards, Eli

      M 1 Reply Last reply
      0
      • C Cedric Moonen

        If you use CArchive, maybe that's normal... In fact, I never used CArchive for saving data but maybe it adds its own data before your data. Try to use CFile instead: char* szText = "Your string To Save"; CFile* pFile = fopen("YourFile.txt","w"); fwrite(szText,strlen(szText),1,pFile); fclose(pFile);

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

        Hi cedric i am giving here my code: .......................... CFile flCPhone; CStdioFile fl; char strFilter[] = { "Text Files (*.txt)|*.txt|" }; CFileDialog FileDlg(FALSE, ".txt", NULL, 0, strFilter); if( FileDlg.DoModal() == IDOK ) { if( flCPhone.Open(FileDlg.GetFileName(), CFile::modeCreate | CFile::modeWrite) == FALSE ) { return;} CArchive ar(&flCPhone, CArchive::store); ar<

        C 1 Reply Last reply
        0
        • E eli15021979

          try this :

          FILE *file;
          char *FileName = "C:\\TestFile.txt";
          char *string_to_save = "this is a test";

          file = fopen(FileName,"w");
          if(!file)
          {
          AfxMessageBox("Unable to open the specified file");
          return;
          }

          fputs(string_to_save,file);
          fputs("\n",file);
          fclose(file)
          return;

          regards, Eli

          M Offline
          M Offline
          mpallavi
          wrote on last edited by
          #5

          hey friends .. i did it.. it was simple ................. CFile flCPhone; flCPhone.Open(FileDlg.GetFileName(), CFile::modeCreate | CFile::modeWrite); int len; len=strlen(m_strInput); flCPhone.Write(m_strInput,len); ..................... thanx for showing me the write track.. i was simply running behind CArchive.. (A beginner in vc++.. getting into it..) thanx pal

          E 1 Reply Last reply
          0
          • M mpallavi

            hey friends .. i did it.. it was simple ................. CFile flCPhone; flCPhone.Open(FileDlg.GetFileName(), CFile::modeCreate | CFile::modeWrite); int len; len=strlen(m_strInput); flCPhone.Write(m_strInput,len); ..................... thanx for showing me the write track.. i was simply running behind CArchive.. (A beginner in vc++.. getting into it..) thanx pal

            E Offline
            E Offline
            eli15021979
            wrote on last edited by
            #6

            if you allow the user to choose the file name and position with File dialog, in your flCPhone.open() use FileDlg.GetPathName() instead FileDlg.GetFileName(). regards, Eli

            M 1 Reply Last reply
            0
            • M mpallavi

              Hi cedric i am giving here my code: .......................... CFile flCPhone; CStdioFile fl; char strFilter[] = { "Text Files (*.txt)|*.txt|" }; CFileDialog FileDlg(FALSE, ".txt", NULL, 0, strFilter); if( FileDlg.DoModal() == IDOK ) { if( flCPhone.Open(FileDlg.GetFileName(), CFile::modeCreate | CFile::modeWrite) == FALSE ) { return;} CArchive ar(&flCPhone, CArchive::store); ar<

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

              Ouch... Sorry, I have mixed CFile with FILE* :~ But I think your question has already been answered ;)

              M 1 Reply Last reply
              0
              • E eli15021979

                if you allow the user to choose the file name and position with File dialog, in your flCPhone.open() use FileDlg.GetPathName() instead FileDlg.GetFileName(). regards, Eli

                M Offline
                M Offline
                mpallavi
                wrote on last edited by
                #8

                ya fine.. thanx Eli .. pal

                A 1 Reply Last reply
                0
                • C Cedric Moonen

                  Ouch... Sorry, I have mixed CFile with FILE* :~ But I think your question has already been answered ;)

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

                  Hi cedric its ok.. :) I wasn't using the member functions of CFile.. There is no function for appending text to a file..in CFile (..I suppose).. How do i go about it?

                  C D 2 Replies Last reply
                  0
                  • M mpallavi

                    Hi cedric its ok.. :) I wasn't using the member functions of CFile.. There is no function for appending text to a file..in CFile (..I suppose).. How do i go about it?

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

                    Then you can use FILE* and open it in appending mode ("-a"). Take a look at fopen in the doc: FILE* pFile = fopen("YourFile.txt","a"); fwrite(.....); // This will append text at the end of the file

                    1 Reply Last reply
                    0
                    • M mpallavi

                      ya fine.. thanx Eli .. pal

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

                      i want get HDD serial number, and encoding it to binary number, and write this binary number into file application (example: myapplication.exe) ? i can't completed, help me ! Regards.

                      A 1 Reply Last reply
                      0
                      • A Anonymous

                        i want get HDD serial number, and encoding it to binary number, and write this binary number into file application (example: myapplication.exe) ? i can't completed, help me ! Regards.

                        A Offline
                        A Offline
                        Anonymous
                        wrote on last edited by
                        #12

                        I using VC++ MFC.

                        1 Reply Last reply
                        0
                        • M mpallavi

                          Hi cedric its ok.. :) I wasn't using the member functions of CFile.. There is no function for appending text to a file..in CFile (..I suppose).. How do i go about it?

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

                          mpallavi wrote: There is no function for appending text to a file..in CFile (..I suppose).. Sure there is. Just go to the end of the file before writing to it. Remember to use CFile::modeNoTruncate when opening the file.


                          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                          M 1 Reply Last reply
                          0
                          • M mpallavi

                            hi.. how do we save user entered text in a file.. i am using common dialog box and allowing the user to specify the file name and the location.. I used CArchive.. The first character in the saved file is always a garbage value.. Why? regards mpallavi

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

                            mpallavi wrote: The first character in the saved file is always a garbage value It is the number of bytes in the CString object that were written to the archive. You should be able to verify this. Take a look at CArchive's << operator. If the length of the CString object is >= 255 and < 0xfffe, an extra 0xff is written to the archive followed by the length of the CString object.


                            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                            1 Reply Last reply
                            0
                            • D David Crow

                              mpallavi wrote: There is no function for appending text to a file..in CFile (..I suppose).. Sure there is. Just go to the end of the file before writing to it. Remember to use CFile::modeNoTruncate when opening the file.


                              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                              M Offline
                              M Offline
                              mpallavi
                              wrote on last edited by
                              #15

                              hi david.. thanx I could append text to a already existing file.. I want the newly entered text to appear on a new line.. I used CFile::seektoend().. how to take the pointer to new line? .. regards pal

                              D 1 Reply Last reply
                              0
                              • M mpallavi

                                hi david.. thanx I could append text to a already existing file.. I want the newly entered text to appear on a new line.. I used CFile::seektoend().. how to take the pointer to new line? .. regards pal

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

                                Insert a CR and/or LF character first.


                                "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                                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