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. bizarre problem with files

bizarre problem with files

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
13 Posts 4 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.
  • L Offline
    L Offline
    lahom
    wrote on last edited by
    #1

    hi i have aquestion i really need an answer for it because it frustrated me... look at these two functions:

    void CSecondDlg::OnButton1()
    {
    //**BROWSE DIALOG 1**//
    FILE *fp;
    FILE *file1 = fopen("file1.txt","w"); //Create txt file for saving editbox content
    int nFileLong;
    //***********************************************//
    char strFilter[] = { "CPP Files (*.cpp)|*.cpp|All Files (*.*)|*.*||" };
    CFileDialog m_ldFile(TRUE, ".cpp", NULL, 0, strFilter);
    if (m_ldFile.DoModal() == IDOK) //Start File dlg box
    {
    m_sFileName=m_ldFile.GetPathName(); //Get file name
    fp=fopen(m_sFileName,"rb"); //Open file for reading
    fseek(fp,0,SEEK_END); //Go to file end
    nFileLong=ftell(fp); //Get length
    char* sText = new char[nFileLong+1]; //reserve string space
    fseek(fp,0,SEEK_SET); //Go to file start
    int i=fread(sText,1,nFileLong,fp); //Read the characters
    sText[i]=0; //Set string terminating null
    m_EDIT1=sText; //Put text in Edit box's variable
    fprintf(file1,"%s\n",m_EDIT1); //print the content of the editbox into the file
    fclose(file1); //Close file2
    fclose(fp); //Close file
    UpdateData(FALSE); //Force data to go to Edit control
    }
    }

    AND

    void CSecondDlg::OnButton2()
    {
    //**BROWSE DIALOG 2**//
    FILE *fp;
    FILE *file2 = fopen("file2.txt","w"); //Create txt file for saving editbox content
    int nFileLong;
    //***************************************************//
    char strFilter[] = { "CPP Files (*.cpp)|*.cpp|All Files (*.*)|*.*||" };
    CFileDialog m_ldFile(TRUE, ".cpp", NULL, 0, strFilter);
    if (m_ldFile.DoModal() == IDOK) //Start File dlg box
    {
    m_sFileName=m_ldFile.GetPathName(); //Get file name
    fp=fopen(m_sFileName,"rb"); //Open file for reading
    fseek(fp,0,SEEK_END); //Go to file end
    nFileLong=ftell(fp); //Get length
    char* sText = new char[nFileLong+1]; //reserve string space
    fseek(fp,0,SEEK_SET); //Go to file start
    int i=fread(sText,1,nFileLong,fp); //Read the characters
    sText[i]=0; //Set string terminating null
    m_EDIT2=sText;

    S CPalliniC K 4 Replies Last reply
    0
    • L lahom

      hi i have aquestion i really need an answer for it because it frustrated me... look at these two functions:

      void CSecondDlg::OnButton1()
      {
      //**BROWSE DIALOG 1**//
      FILE *fp;
      FILE *file1 = fopen("file1.txt","w"); //Create txt file for saving editbox content
      int nFileLong;
      //***********************************************//
      char strFilter[] = { "CPP Files (*.cpp)|*.cpp|All Files (*.*)|*.*||" };
      CFileDialog m_ldFile(TRUE, ".cpp", NULL, 0, strFilter);
      if (m_ldFile.DoModal() == IDOK) //Start File dlg box
      {
      m_sFileName=m_ldFile.GetPathName(); //Get file name
      fp=fopen(m_sFileName,"rb"); //Open file for reading
      fseek(fp,0,SEEK_END); //Go to file end
      nFileLong=ftell(fp); //Get length
      char* sText = new char[nFileLong+1]; //reserve string space
      fseek(fp,0,SEEK_SET); //Go to file start
      int i=fread(sText,1,nFileLong,fp); //Read the characters
      sText[i]=0; //Set string terminating null
      m_EDIT1=sText; //Put text in Edit box's variable
      fprintf(file1,"%s\n",m_EDIT1); //print the content of the editbox into the file
      fclose(file1); //Close file2
      fclose(fp); //Close file
      UpdateData(FALSE); //Force data to go to Edit control
      }
      }

      AND

      void CSecondDlg::OnButton2()
      {
      //**BROWSE DIALOG 2**//
      FILE *fp;
      FILE *file2 = fopen("file2.txt","w"); //Create txt file for saving editbox content
      int nFileLong;
      //***************************************************//
      char strFilter[] = { "CPP Files (*.cpp)|*.cpp|All Files (*.*)|*.*||" };
      CFileDialog m_ldFile(TRUE, ".cpp", NULL, 0, strFilter);
      if (m_ldFile.DoModal() == IDOK) //Start File dlg box
      {
      m_sFileName=m_ldFile.GetPathName(); //Get file name
      fp=fopen(m_sFileName,"rb"); //Open file for reading
      fseek(fp,0,SEEK_END); //Go to file end
      nFileLong=ftell(fp); //Get length
      char* sText = new char[nFileLong+1]; //reserve string space
      fseek(fp,0,SEEK_SET); //Go to file start
      int i=fread(sText,1,nFileLong,fp); //Read the characters
      sText[i]=0; //Set string terminating null
      m_EDIT2=sText;

      S Offline
      S Offline
      Saurabh Garg
      wrote on last edited by
      #2

      I can't seem to find the difference between two functions. Can you step through your program and see what exactly is going on? -Saurabh

      modified on Monday, June 23, 2008 2:07 AM

      L 1 Reply Last reply
      0
      • L lahom

        hi i have aquestion i really need an answer for it because it frustrated me... look at these two functions:

        void CSecondDlg::OnButton1()
        {
        //**BROWSE DIALOG 1**//
        FILE *fp;
        FILE *file1 = fopen("file1.txt","w"); //Create txt file for saving editbox content
        int nFileLong;
        //***********************************************//
        char strFilter[] = { "CPP Files (*.cpp)|*.cpp|All Files (*.*)|*.*||" };
        CFileDialog m_ldFile(TRUE, ".cpp", NULL, 0, strFilter);
        if (m_ldFile.DoModal() == IDOK) //Start File dlg box
        {
        m_sFileName=m_ldFile.GetPathName(); //Get file name
        fp=fopen(m_sFileName,"rb"); //Open file for reading
        fseek(fp,0,SEEK_END); //Go to file end
        nFileLong=ftell(fp); //Get length
        char* sText = new char[nFileLong+1]; //reserve string space
        fseek(fp,0,SEEK_SET); //Go to file start
        int i=fread(sText,1,nFileLong,fp); //Read the characters
        sText[i]=0; //Set string terminating null
        m_EDIT1=sText; //Put text in Edit box's variable
        fprintf(file1,"%s\n",m_EDIT1); //print the content of the editbox into the file
        fclose(file1); //Close file2
        fclose(fp); //Close file
        UpdateData(FALSE); //Force data to go to Edit control
        }
        }

        AND

        void CSecondDlg::OnButton2()
        {
        //**BROWSE DIALOG 2**//
        FILE *fp;
        FILE *file2 = fopen("file2.txt","w"); //Create txt file for saving editbox content
        int nFileLong;
        //***************************************************//
        char strFilter[] = { "CPP Files (*.cpp)|*.cpp|All Files (*.*)|*.*||" };
        CFileDialog m_ldFile(TRUE, ".cpp", NULL, 0, strFilter);
        if (m_ldFile.DoModal() == IDOK) //Start File dlg box
        {
        m_sFileName=m_ldFile.GetPathName(); //Get file name
        fp=fopen(m_sFileName,"rb"); //Open file for reading
        fseek(fp,0,SEEK_END); //Go to file end
        nFileLong=ftell(fp); //Get length
        char* sText = new char[nFileLong+1]; //reserve string space
        fseek(fp,0,SEEK_SET); //Go to file start
        int i=fread(sText,1,nFileLong,fp); //Read the characters
        sText[i]=0; //Set string terminating null
        m_EDIT2=sText;

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Well your code (though a bit weird: duplication) works on my system. BTW why don't you delete the allocated memory? :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        L 1 Reply Last reply
        0
        • L lahom

          hi i have aquestion i really need an answer for it because it frustrated me... look at these two functions:

          void CSecondDlg::OnButton1()
          {
          //**BROWSE DIALOG 1**//
          FILE *fp;
          FILE *file1 = fopen("file1.txt","w"); //Create txt file for saving editbox content
          int nFileLong;
          //***********************************************//
          char strFilter[] = { "CPP Files (*.cpp)|*.cpp|All Files (*.*)|*.*||" };
          CFileDialog m_ldFile(TRUE, ".cpp", NULL, 0, strFilter);
          if (m_ldFile.DoModal() == IDOK) //Start File dlg box
          {
          m_sFileName=m_ldFile.GetPathName(); //Get file name
          fp=fopen(m_sFileName,"rb"); //Open file for reading
          fseek(fp,0,SEEK_END); //Go to file end
          nFileLong=ftell(fp); //Get length
          char* sText = new char[nFileLong+1]; //reserve string space
          fseek(fp,0,SEEK_SET); //Go to file start
          int i=fread(sText,1,nFileLong,fp); //Read the characters
          sText[i]=0; //Set string terminating null
          m_EDIT1=sText; //Put text in Edit box's variable
          fprintf(file1,"%s\n",m_EDIT1); //print the content of the editbox into the file
          fclose(file1); //Close file2
          fclose(fp); //Close file
          UpdateData(FALSE); //Force data to go to Edit control
          }
          }

          AND

          void CSecondDlg::OnButton2()
          {
          //**BROWSE DIALOG 2**//
          FILE *fp;
          FILE *file2 = fopen("file2.txt","w"); //Create txt file for saving editbox content
          int nFileLong;
          //***************************************************//
          char strFilter[] = { "CPP Files (*.cpp)|*.cpp|All Files (*.*)|*.*||" };
          CFileDialog m_ldFile(TRUE, ".cpp", NULL, 0, strFilter);
          if (m_ldFile.DoModal() == IDOK) //Start File dlg box
          {
          m_sFileName=m_ldFile.GetPathName(); //Get file name
          fp=fopen(m_sFileName,"rb"); //Open file for reading
          fseek(fp,0,SEEK_END); //Go to file end
          nFileLong=ftell(fp); //Get length
          char* sText = new char[nFileLong+1]; //reserve string space
          fseek(fp,0,SEEK_SET); //Go to file start
          int i=fread(sText,1,nFileLong,fp); //Read the characters
          sText[i]=0; //Set string terminating null
          m_EDIT2=sText;

          K Offline
          K Offline
          Kwanalouie
          wrote on last edited by
          #4

          I'm not real familiar with code on the heap, but the statement char* sText = new char[nFileLong+1]; //reserve string space is created with "new". Doesn't that require a "delete" or equivalent (I don't know the syntax) to de-allocate that space? If you don't then that space can't get re-allocated since the pointer is lost when you exit the procedure. Don't know what kinds of problems could occur as a result.

          1 Reply Last reply
          0
          • S Saurabh Garg

            I can't seem to find the difference between two functions. Can you step through your program and see what exactly is going on? -Saurabh

            modified on Monday, June 23, 2008 2:07 AM

            L Offline
            L Offline
            lahom
            wrote on last edited by
            #5

            thanks for ur reply but my application is simple ...two edit controls each with its "browse" button. first load afile to edit .....then store the content of the edit to ".txt" file... its that simple.... if u try it for the first time ...it works for both ... but in second and third and .....etc times....the odd problem occures ?????????? what is it that i did wrong please would u try it urself and help me understand and solve the problem my project stop on this :(( thank u

            S 1 Reply Last reply
            0
            • CPalliniC CPallini

              Well your code (though a bit weird: duplication) works on my system. BTW why don't you delete the allocated memory? :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              L Offline
              L Offline
              lahom
              wrote on last edited by
              #6

              :confused: how???? did u tried it multiple times... and in both way worked??? please i really need to know whats wrong...

              CPalliniC 1 Reply Last reply
              0
              • L lahom

                :confused: how???? did u tried it multiple times... and in both way worked??? please i really need to know whats wrong...

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                Yes it actually works. I've made three tests: (1) Button1 followed by Button2 (loaded two different files) (2) Button2 followed by Button1 (loaded two different files) (3) Button1 followed by Button2 (loaded the same file) All of them were OK. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                In testa che avete, signor di Ceprano?

                L 1 Reply Last reply
                0
                • L lahom

                  thanks for ur reply but my application is simple ...two edit controls each with its "browse" button. first load afile to edit .....then store the content of the edit to ".txt" file... its that simple.... if u try it for the first time ...it works for both ... but in second and third and .....etc times....the odd problem occures ?????????? what is it that i did wrong please would u try it urself and help me understand and solve the problem my project stop on this :(( thank u

                  S Offline
                  S Offline
                  Saurabh Garg
                  wrote on last edited by
                  #8

                  It doesn't matter that application is simple or complex. Point is you have a bug in your application that means there is a problem some where. So as I suggested why don't you try to step through your program and check whats going on. Another idea is just create a new application and copy this code over because the code you provided works fine for me. -Saurabh

                  1 Reply Last reply
                  0
                  • L lahom

                    hi i have aquestion i really need an answer for it because it frustrated me... look at these two functions:

                    void CSecondDlg::OnButton1()
                    {
                    //**BROWSE DIALOG 1**//
                    FILE *fp;
                    FILE *file1 = fopen("file1.txt","w"); //Create txt file for saving editbox content
                    int nFileLong;
                    //***********************************************//
                    char strFilter[] = { "CPP Files (*.cpp)|*.cpp|All Files (*.*)|*.*||" };
                    CFileDialog m_ldFile(TRUE, ".cpp", NULL, 0, strFilter);
                    if (m_ldFile.DoModal() == IDOK) //Start File dlg box
                    {
                    m_sFileName=m_ldFile.GetPathName(); //Get file name
                    fp=fopen(m_sFileName,"rb"); //Open file for reading
                    fseek(fp,0,SEEK_END); //Go to file end
                    nFileLong=ftell(fp); //Get length
                    char* sText = new char[nFileLong+1]; //reserve string space
                    fseek(fp,0,SEEK_SET); //Go to file start
                    int i=fread(sText,1,nFileLong,fp); //Read the characters
                    sText[i]=0; //Set string terminating null
                    m_EDIT1=sText; //Put text in Edit box's variable
                    fprintf(file1,"%s\n",m_EDIT1); //print the content of the editbox into the file
                    fclose(file1); //Close file2
                    fclose(fp); //Close file
                    UpdateData(FALSE); //Force data to go to Edit control
                    }
                    }

                    AND

                    void CSecondDlg::OnButton2()
                    {
                    //**BROWSE DIALOG 2**//
                    FILE *fp;
                    FILE *file2 = fopen("file2.txt","w"); //Create txt file for saving editbox content
                    int nFileLong;
                    //***************************************************//
                    char strFilter[] = { "CPP Files (*.cpp)|*.cpp|All Files (*.*)|*.*||" };
                    CFileDialog m_ldFile(TRUE, ".cpp", NULL, 0, strFilter);
                    if (m_ldFile.DoModal() == IDOK) //Start File dlg box
                    {
                    m_sFileName=m_ldFile.GetPathName(); //Get file name
                    fp=fopen(m_sFileName,"rb"); //Open file for reading
                    fseek(fp,0,SEEK_END); //Go to file end
                    nFileLong=ftell(fp); //Get length
                    char* sText = new char[nFileLong+1]; //reserve string space
                    fseek(fp,0,SEEK_SET); //Go to file start
                    int i=fread(sText,1,nFileLong,fp); //Read the characters
                    sText[i]=0; //Set string terminating null
                    m_EDIT2=sText;

                    K Offline
                    K Offline
                    Kwanalouie
                    wrote on last edited by
                    #9

                    One problem your code has is in the method you are opening the files with fopen. There are two modes for reading and writing to files - binary mode and text mode. Some character conversions change in those two modes. When you open file1 and file2 you do not specify what mode to use, so the default mode is used. When you open m_sFileName you tell it to open it to read in binary mode instead of text mode. Try changing the "w" to "wt" in this statement of yours FILE *file1 = fopen("file1.txt","w"); And more importantly change the "rb" to "rt" since you are trying to read characters in the following statement in your code fp=fopen(m_sFileName,"rb"); //Open file for reading The ftell and fseek functions may not give the correct lengths if the file is opened in text mode, however. So you should simplify things and use the fgets() function to get the string you want. Since you are working with text files they should be opened with the text attribute.

                    modified on Tuesday, June 24, 2008 6:30 AM

                    1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      Yes it actually works. I've made three tests: (1) Button1 followed by Button2 (loaded two different files) (2) Button2 followed by Button1 (loaded two different files) (3) Button1 followed by Button2 (loaded the same file) All of them were OK. :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      L Offline
                      L Offline
                      lahom
                      wrote on last edited by
                      #10

                      ok... what version of vc r u using>>>>>iam using 6 someone else tried it also and it didnot work out

                      CPalliniC 1 Reply Last reply
                      0
                      • L lahom

                        ok... what version of vc r u using>>>>>iam using 6 someone else tried it also and it didnot work out

                        CPalliniC Offline
                        CPalliniC Offline
                        CPallini
                        wrote on last edited by
                        #11

                        Are you kidding?

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        In testa che avete, signor di Ceprano?

                        L 1 Reply Last reply
                        0
                        • CPalliniC CPallini

                          Are you kidding?

                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                          [My articles]

                          L Offline
                          L Offline
                          lahom
                          wrote on last edited by
                          #12

                          no iam not .... iam serious whats wrong???? :((

                          CPalliniC 1 Reply Last reply
                          0
                          • L lahom

                            no iam not .... iam serious whats wrong???? :((

                            CPalliniC Offline
                            CPalliniC Offline
                            CPallini
                            wrote on last edited by
                            #13

                            Well, I'll try also on VS6. Maybe this evening (here I've only VS2005). :)

                            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                            [My articles]

                            In testa che avete, signor di Ceprano?

                            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