| | |
bizzare problem with files
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 42
Reputation:
Solved Threads: 0
hi
i have aquestion i really need an answer for it because it frustrated me...
look at these two functions: AND
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
i have aquestion i really need an answer for it because it frustrated me...
look at these two functions:
C++ Syntax (Toggle Plain Text)
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 } }
C++ Syntax (Toggle Plain Text)
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; //Put text in Edit box's variable fprintf(file2,"%s\n",m_EDIT2); //print the content of the editbox into the file fclose(fp); //Close file fclose(file2); //Close file2 UpdateData(FALSE); //Force data to go to Edit control } }
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
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.
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Modem keeps dialling when i open files (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: miniproject on c++
- Next Thread: Regarding complex naming in Enums
Views: 806 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






