943,724 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1059
  • C++ RSS
Jun 22nd, 2008
0

bizzare problem with files

Expand Post »
hi
i have aquestion i really need an answer for it because it frustrated me...
look at these two functions:
C++ Syntax (Toggle Plain Text)
  1. void CSecondDlg::OnButton1()
  2. {
  3. //**BROWSE DIALOG 1**//
  4. FILE *fp;
  5. FILE *file1 = fopen("file1.txt","w"); //Create txt file for saving editbox content
  6. int nFileLong;
  7. //***********************************************//
  8. char strFilter[] = { "CPP Files (*.cpp)|*.cpp|All Files (*.*)|*.*||" };
  9. CFileDialog m_ldFile(TRUE, ".cpp", NULL, 0, strFilter);
  10. if (m_ldFile.DoModal() == IDOK) //Start File dlg box
  11. {
  12. m_sFileName=m_ldFile.GetPathName(); //Get file name
  13. fp=fopen(m_sFileName,"rb"); //Open file for reading
  14. fseek(fp,0,SEEK_END); //Go to file end
  15. nFileLong=ftell(fp); //Get length
  16. char* sText = new char[nFileLong+1]; //reserve string space
  17. fseek(fp,0,SEEK_SET); //Go to file start
  18. int i=fread(sText,1,nFileLong,fp); //Read the characters
  19. sText[i]=0; //Set string terminating null
  20. m_EDIT1=sText; //Put text in Edit box's variable
  21. fprintf(file1,"%s\n",m_EDIT1); //print the content of the editbox into the file
  22. fclose(file1); //Close file2
  23. fclose(fp); //Close file
  24. UpdateData(FALSE); //Force data to go to Edit control
  25. }
  26. }
AND

C++ Syntax (Toggle Plain Text)
  1. void CSecondDlg::OnButton2()
  2. {
  3. //**BROWSE DIALOG 2**//
  4. FILE *fp;
  5. FILE *file2 = fopen("file2.txt","w"); //Create txt file for saving editbox content
  6. int nFileLong;
  7. //***************************************************//
  8. char strFilter[] = { "CPP Files (*.cpp)|*.cpp|All Files (*.*)|*.*||" };
  9. CFileDialog m_ldFile(TRUE, ".cpp", NULL, 0, strFilter);
  10. if (m_ldFile.DoModal() == IDOK) //Start File dlg box
  11. {
  12. m_sFileName=m_ldFile.GetPathName(); //Get file name
  13. fp=fopen(m_sFileName,"rb"); //Open file for reading
  14. fseek(fp,0,SEEK_END); //Go to file end
  15. nFileLong=ftell(fp); //Get length
  16. char* sText = new char[nFileLong+1]; //reserve string space
  17. fseek(fp,0,SEEK_SET); //Go to file start
  18. int i=fread(sText,1,nFileLong,fp); //Read the characters
  19. sText[i]=0; //Set string terminating null
  20. m_EDIT2=sText; //Put text in Edit box's variable
  21. fprintf(file2,"%s\n",m_EDIT2); //print the content of the editbox into the file
  22. fclose(fp); //Close file
  23. fclose(file2); //Close file2
  24. UpdateData(FALSE); //Force data to go to Edit control
  25. }
  26. }
as u can see the two function do the same thing :
open afile into an edit box ....then save the content of the edit box in another file (.txt)
what is bizzare??
well if i open browse1 and browse2 button ...i would find that the first file is created and the second not...(because i hit the button browse1 first).
if i try another time but with hitting browse2 first the opposite thing happend (the second file is changed but not the first )
what is wrong ????
i reaaaly dont know what to do ??
please help me with this
thank u all
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
lahom is offline Offline
42 posts
since Apr 2008
Jun 23rd, 2008
0

Re: bizzare problem with files

1) In the first program snippet you read the input file as binary file then write the output file as if it were a text file. You can't do that because the buffer the end-of-line characters will not be interpreted correctly. Solution: open the output file as binary and use fwrite() to write out the data just as you used fread() to read it.

2) Use your compiler's debugger, set a breakpoint inside each of those functions, and find out what fopen() is returning. Is it returning a NULL pointer? Put another breakpoint on the line just after fclose(). When that one is hit, switch to Windows Exploror and check the file system to see if the output file was created and in the directory you thought it should be.

3) After creating the CFileDialog object and before calling its DoModal(), set its m_ofn.Flag to OFN_NOCHANGEDIR so that it don't change directories on you.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: miniproject on c++
Next Thread in C++ Forum Timeline: Regarding complex naming in Enums





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC