bizarre problem with files
-
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; -
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;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
-
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;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] -
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;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.
-
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
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
-
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] -
:confused: how???? did u tried it multiple times... and in both way worked??? please i really need to know whats wrong...
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] -
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
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
-
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;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
-
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] -
ok... what version of vc r u using>>>>>iam using 6 someone else tried it also and it didnot work out
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] -
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] -
Well, I'll try also on
VS6
. Maybe this evening (here I've onlyVS2005
). :)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]