how to write/read in text file with mfc project??
-
antonio343 wrote:
, I get a error that it said:
You dont have acces to this file.Open the file using
CFile::modeReadWrite OR CFile::modeWrite
it seems you don't have permission to write.
I changed the mode by modeReadWrite and I get simbols like this:
<pre>쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌Y쳌쳌쳌쳌꣨ɷ쳌쳌偁泇¬</pre>
I also try this:
void CDlgResultados::loadFromFile(void)
{
CString path= GetUserHomeDir() + _T("\\Documents\\file.txt");CStdioFile f1; f1.Open(path, CFile::modeCreate | CFile::modeRead || CFile::typeText); CStdioFile f(stdin); TCHAR buf\[100\]; f1.ReadString(buf, 99); m\_Edit.SetWindowTextW(buf);
}
But I get simbols. I think that the problem is the function ReadString doesnt get the text from the file, becouse the simbol that I get are the "rubbish" which is inicializated buf
-
OK, and does that directory exist and can you create that file manually? If so then check also the suggestions from other commenters below.
Unrequited desire is character building. OriginalGriff
Yes, this directory exist and I made the file manually.
-
I changed the mode by modeReadWrite and I get simbols like this:
<pre>쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌Y쳌쳌쳌쳌꣨ɷ쳌쳌偁泇¬</pre>
I also try this:
void CDlgResultados::loadFromFile(void)
{
CString path= GetUserHomeDir() + _T("\\Documents\\file.txt");CStdioFile f1; f1.Open(path, CFile::modeCreate | CFile::modeRead || CFile::typeText); CStdioFile f(stdin); TCHAR buf\[100\]; f1.ReadString(buf, 99); m\_Edit.SetWindowTextW(buf);
}
But I get simbols. I think that the problem is the function ReadString doesnt get the text from the file, becouse the simbol that I get are the "rubbish" which is inicializated buf
In your original code you were writing from an uninitialised buffer, hence the garbage recorded in your file. Check the logic of your code thus:
- Create the file with access for writing
- Write some text either from a constant string, or from some data built by the program
- Close the file
- Open the file with access for reading
- Read the text into a buffer
- Close the file
In each case check the results of your function calls to ensure that your code has completed without error. You can also create your file manually using Notepad or similar text editor.
Unrequited desire is character building. OriginalGriff
-
In your original code you were writing from an uninitialised buffer, hence the garbage recorded in your file. Check the logic of your code thus:
- Create the file with access for writing
- Write some text either from a constant string, or from some data built by the program
- Close the file
- Open the file with access for reading
- Read the text into a buffer
- Close the file
In each case check the results of your function calls to ensure that your code has completed without error. You can also create your file manually using Notepad or similar text editor.
Unrequited desire is character building. OriginalGriff
Yes, I know, I try to do that, and the file is created manually. But I dont know how to read or write in mfc.
-
I changed the mode by modeReadWrite and I get simbols like this:
<pre>쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌Y쳌쳌쳌쳌꣨ɷ쳌쳌偁泇¬</pre>
I also try this:
void CDlgResultados::loadFromFile(void)
{
CString path= GetUserHomeDir() + _T("\\Documents\\file.txt");CStdioFile f1; f1.Open(path, CFile::modeCreate | CFile::modeRead || CFile::typeText); CStdioFile f(stdin); TCHAR buf\[100\]; f1.ReadString(buf, 99); m\_Edit.SetWindowTextW(buf);
}
But I get simbols. I think that the problem is the function ReadString doesnt get the text from the file, becouse the simbol that I get are the "rubbish" which is inicializated buf
antonio343 wrote:
f1.Open(path, CFile::modeCreate | CFile::modeRead || CFile::typeText); TCHAR buf[100]; f1.ReadString(buf, 99);
You are creating an empty file and then attempting to read from it. Is that really what you want? Also, you may want to revise your usage of the
|
and||
operators."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
Yes, I know, I try to do that, and the file is created manually. But I dont know how to read or write in mfc.
-
antonio343 wrote:
f1.Open(path, CFile::modeCreate | CFile::modeRead || CFile::typeText); TCHAR buf[100]; f1.ReadString(buf, 99);
You are creating an empty file and then attempting to read from it. Is that really what you want? Also, you may want to revise your usage of the
|
and||
operators."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
Nooo. I have a text file that I created manually,and I'd like to read the text from it. I made a mistake when I write ||, but it is not the error
-
Nooo. I have a text file that I created manually,and I'd like to read the text from it. I made a mistake when I write ||, but it is not the error
-
`So, why are you asking for the the file to be Created?
if ( myFile.Open(path, CFile::modeCreate | CFile::modeRead ) )
| is a bitwise OR, || is a logical OR
Yes you are right, I didnt know what mean this argument, if I dont put this argument, everything run rightly
-
Yes you are right, I didnt know what mean this argument, if I dont put this argument, everything run rightly
I continue to feel very fortunate that the vast majority of programming (that I see) is done in english. I understand that with function/variable names in my own language I am at an advantage to those for whom they are in a different language. :thumbsup: