Problem in getting strings with space.
-
void CSaveTextDlg::OnRead() { FILE *f; CString temp1; if ( ( f = fopen("c:\\Log.txt","r") )==NULL) { AfxMessageBox("Unable to open file"); return; } while(fscanf(f,"%s",temp1)!=EOF) { str= CString(str) + CString(temp1); } fclose(f); m_Text=str; UpdateData(FALSE); } This is my code...I got full text of Log.txt...But i got full text without space.I need space also.My text contains some tables..I cudn't get tht. Pls findout the problem
-
void CSaveTextDlg::OnRead() { FILE *f; CString temp1; if ( ( f = fopen("c:\\Log.txt","r") )==NULL) { AfxMessageBox("Unable to open file"); return; } while(fscanf(f,"%s",temp1)!=EOF) { str= CString(str) + CString(temp1); } fclose(f); m_Text=str; UpdateData(FALSE); } This is my code...I got full text of Log.txt...But i got full text without space.I need space also.My text contains some tables..I cudn't get tht. Pls findout the problem
:laugh:Why don't you use CFile : CFile file; if(!file.Open("c:\\log.txt",CFile::modeRead)) { AfxMessageBox("Unable to Read File"); return; } char * str=new char[100]; file.Read((void*)str,100); delete[] str; str is the buffer that contains data retrieved from the file . Does this solve your problem???? Now you can use file.Seek() to get the data from a certain location in the file Regards Anshuman Happy Programming -- modified at 22:57 Tuesday 24th January, 2006
-
void CSaveTextDlg::OnRead() { FILE *f; CString temp1; if ( ( f = fopen("c:\\Log.txt","r") )==NULL) { AfxMessageBox("Unable to open file"); return; } while(fscanf(f,"%s",temp1)!=EOF) { str= CString(str) + CString(temp1); } fclose(f); m_Text=str; UpdateData(FALSE); } This is my code...I got full text of Log.txt...But i got full text without space.I need space also.My text contains some tables..I cudn't get tht. Pls findout the problem
-
void CSaveTextDlg::OnRead() { FILE *f; CString temp1; if ( ( f = fopen("c:\\Log.txt","r") )==NULL) { AfxMessageBox("Unable to open file"); return; } while(fscanf(f,"%s",temp1)!=EOF) { str= CString(str) + CString(temp1); } fclose(f); m_Text=str; UpdateData(FALSE); } This is my code...I got full text of Log.txt...But i got full text without space.I need space also.My text contains some tables..I cudn't get tht. Pls findout the problem
fscanf function reads a word from file. This is why you do not get any space. If you want to get space then use another function instead of fscanf. MANISH RASTOGI
-
void CSaveTextDlg::OnRead() { FILE *f; CString temp1; if ( ( f = fopen("c:\\Log.txt","r") )==NULL) { AfxMessageBox("Unable to open file"); return; } while(fscanf(f,"%s",temp1)!=EOF) { str= CString(str) + CString(temp1); } fclose(f); m_Text=str; UpdateData(FALSE); } This is my code...I got full text of Log.txt...But i got full text without space.I need space also.My text contains some tables..I cudn't get tht. Pls findout the problem
it seems that you're using either c and C++ functions. i suggest you using the standard STL library, with
std::iostream
andstd::fstream
. there, you'll dispose of agetline()
function whichcan be configured to read until a\n
character (by default) of whatever other...
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...] -
void CSaveTextDlg::OnRead() { FILE *f; CString temp1; if ( ( f = fopen("c:\\Log.txt","r") )==NULL) { AfxMessageBox("Unable to open file"); return; } while(fscanf(f,"%s",temp1)!=EOF) { str= CString(str) + CString(temp1); } fclose(f); m_Text=str; UpdateData(FALSE); } This is my code...I got full text of Log.txt...But i got full text without space.I need space also.My text contains some tables..I cudn't get tht. Pls findout the problem
Ok There are lots of good suggestions. I thought to give the answer in your style only. fscanf(f, "%[^\n], temp1); this will read string of character until it will get a new line;:) I just add as one of the answers. But the previous answers are more perfect.:-D
-
why u used fscanf..? why fgets not.. fscanf returns when white space is encountered. so you can use fgets which reads the file line by line. :) Thanks and Regards Laxman FAILURE is the first step towards SUCCESS :cool:
Laxman9 wrote:
fscanf returns when white space is encountered.
Not exclusively.
Laxman9 wrote:
so you can use fgets which reads the file line by line.
fscanf()
can also do this.
"The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb
-
void CSaveTextDlg::OnRead() { FILE *f; CString temp1; if ( ( f = fopen("c:\\Log.txt","r") )==NULL) { AfxMessageBox("Unable to open file"); return; } while(fscanf(f,"%s",temp1)!=EOF) { str= CString(str) + CString(temp1); } fclose(f); m_Text=str; UpdateData(FALSE); } This is my code...I got full text of Log.txt...But i got full text without space.I need space also.My text contains some tables..I cudn't get tht. Pls findout the problem
While you may get it to work, your code is not very efficient as it reads from the disk way too often. If all you are doing is reading from a file and displaying the contents in an edit control (i.e., you wanted words and spaces), I suggest something like:
CFile file("c:\\log.txt", CFile::modeRead);
LPBYTE lpBuffer = new BYTE[file.GetLength() + 1];
UINT uBytesRead = file.Read(lpBuffer, file.GetLength());
lpBuffer[uBytesRead] = '\0';
file.Close();
m_Text.SetWindowText(lpBuffer); // assuming that m_Text is a CEdit variable
// no need to use UpdateData()
"The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb
-
While you may get it to work, your code is not very efficient as it reads from the disk way too often. If all you are doing is reading from a file and displaying the contents in an edit control (i.e., you wanted words and spaces), I suggest something like:
CFile file("c:\\log.txt", CFile::modeRead);
LPBYTE lpBuffer = new BYTE[file.GetLength() + 1];
UINT uBytesRead = file.Read(lpBuffer, file.GetLength());
lpBuffer[uBytesRead] = '\0';
file.Close();
m_Text.SetWindowText(lpBuffer); // assuming that m_Text is a CEdit variable
// no need to use UpdateData()
"The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb
I have some doubts with regards using the above method. Now the data may need to b displayed on Editcontrol whih is on a different dlg than the one from which the read request has been initiated. So one needs to store the data on2 a CString or like to Xfer the data to dat dlg. Secondly, when u read file data u wud not know in advance how large the file length wud b, so allocation of a very large buffer wud not b advisable. How abt having a fixed size buffer that reads data and does CFile FileToRead; FileToRead.Open(); DWORD dwBytesRemaining = FileToRead.GetLength(); while(dwBytesRemaining) { UINT nBytesRead = FileToRead.Read( buffer, sizeof(buffer) ); CString += buffer; dwBytesRemaining -= nBytesRead; }; ?? CString wud manage the hassles of mem mgmt and u have read all the data w/o any allocation work by urself. Does CString have any limitations of not being able 2 store large data or newline probs etc??
-
I have some doubts with regards using the above method. Now the data may need to b displayed on Editcontrol whih is on a different dlg than the one from which the read request has been initiated. So one needs to store the data on2 a CString or like to Xfer the data to dat dlg. Secondly, when u read file data u wud not know in advance how large the file length wud b, so allocation of a very large buffer wud not b advisable. How abt having a fixed size buffer that reads data and does CFile FileToRead; FileToRead.Open(); DWORD dwBytesRemaining = FileToRead.GetLength(); while(dwBytesRemaining) { UINT nBytesRead = FileToRead.Read( buffer, sizeof(buffer) ); CString += buffer; dwBytesRemaining -= nBytesRead; }; ?? CString wud manage the hassles of mem mgmt and u have read all the data w/o any allocation work by urself. Does CString have any limitations of not being able 2 store large data or newline probs etc??
cpp_prgmer wrote:
Now the data may need to b displayed on Editcontrol whih is on a different dlg than the one from which the read request has been initiated.
It makes no difference, as long as the data being read is accessible by the other dialog.
cpp_prgmer wrote:
So one needs to store the data on2 a CString or like to Xfer the data to dat dlg.
The example I provided did store the data in a
BYTE
variable.cpp_prgmer wrote:
Secondly, when u read file data u wud not know in advance how large the file length wud b...
Sure you would. I also showed this in the example. Did you not see it? Strangly enough, you used the same
CString::GetLength()
call in your code snippet.cpp_prgmer wrote:
How abt having a fixed size buffer that reads data and does...
Like I previously indicated, multiple disk reads are not necessary, and can be detrimental to performance.
cpp_prgmer wrote:
CString wud manage the hassles of mem mgmt and u have read all the data w/o any allocation work by urself.
Yes,
CString
does this, but how much trouble is it to use thenew
operator once with the tradeoff being less disk I/O? Furthermore, theCString += buffer
statement can be a lot more expensive than a single use of thenew
operator.cpp_prgmer wrote:
Does CString have any limitations of not being able 2 store large data or newline probs etc??
No.
"The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb