-
March 24th, 2011, 09:19 PM
#1
Reading Text line from .txt File
Hello
I want to read line from txt file when i pressed the button
please any one have code using MFC.
-
March 24th, 2011, 09:27 PM
#2
Re: Reading Text line from .txt File
-
March 24th, 2011, 10:19 PM
#3
Re: Reading Text line from .txt File
CStdioFile inFile;
CFileException efile;
CString strFirstLine;
inFile.Open("test.txt",CFile::modeRead|CFile::typeText);
inFile.ReadString(strFirstLine);
m_List1.AddString(inFile);
am doing this , but giving me error
-
March 24th, 2011, 11:01 PM
#4
Re: Reading Text line from .txt File
When i Used fstream..
CString str,Str1;
int a;
UpdateData();
str= m_EditBox;
fout.open("abc.txt");
fout<< "Name";
fout.close();
ifstream in;
in.open("abc.txt");
while(!in.eof())
{
in >> Str1; /// Error Comes here no operator ">>" matches these operands
}
UpdateData(FALSE);
m__myListBox.AddString(Str1);
-
March 24th, 2011, 11:24 PM
#5
Re: Reading Text line from .txt File
use ifstream with char or char string.
Code:
char readStr[512];
ifstream myFile("fileName");
while (!myFile.eof())
{
myFile >> readStr;
}
myFile.close();
And from next post use CODE tag.
◄◄ hypheni ►►
-
March 25th, 2011, 12:16 AM
#6
Re: Reading Text line from .txt File
Thanks for the Help.
I did this but this time nothing is getting in the ReadStr variable
-
March 25th, 2011, 12:47 AM
#7
Re: Reading Text line from .txt File
It perfectly does. Check the loop, it read and store each strings from file, then again reads next string. For storing all string in a single buffer you need to add each readStr in a buffer. ie:
Code:
while (!myFile.eof())
{
myFile >> readStr;
szBuf += readStr; //CString szBuf
}
// now you have all the txt from file in CString szBuf here.
Check the file name and path for be sure that you are reading correct file.
And for using fstream did you put using namespace std; at the top. ?
Last edited by hypheni; March 25th, 2011 at 12:48 AM.
Reason: `
◄◄ hypheni ►►
-
March 25th, 2011, 01:47 AM
#8
Re: Reading Text line from .txt File
Yup i did..its working
in this program i given the string.
Now i am trying to do to take string from the Edit Box but its not Writing in the file puts ASCI and radam numbers
-
March 25th, 2011, 03:32 AM
#9
Re: Reading Text line from .txt File
Originally Posted by adnan86
Code:
CStdioFile inFile;
CFileException efile;
CString strFirstLine;
inFile.Open("test.txt",CFile::modeRead|CFile::typeText);
inFile.ReadString(strFirstLine);
m_List1.AddString(inFile);
am doing this , but giving me error
1. Please, use Code tags around code snippets.
2. To help us to help you you should always explain what exact error you had and, if possible, show the exact error message.
3. What is m_List1? Is it a CListBox instance? If *yes* then what can it have to do with a CStdioFile object passed in as a parameter?
You, perhaps, wanted to pass in the CString strFirstLine object containing the text you had read from a file?
Victor Nijegorodov
-
March 25th, 2011, 05:41 AM
#10
Re: Reading Text line from .txt File
VictorN thanks for your reply. this prob is solved
now i want to write data from the Edit Box.. its writing random numbers not the string
@code:
ofstream Students("test.txt", ios:ut);
CString name;
name=m_editbox;
Students << name ;
Students.close();
-
March 25th, 2011, 05:41 AM
#11
Re: Reading Text line from .txt File
I think you need some books and study about windows programming (as well as basic c++) then try to develop some application, rather than asking each steps in forum.
◄◄ hypheni ►►
-
March 25th, 2011, 05:42 AM
#12
Re: Reading Text line from .txt File
I dont know eactly how to tag code...SOrry
-
March 25th, 2011, 05:44 AM
#13
Re: Reading Text line from .txt File
hypheni : Yup i am new to MFC Windows programming. i asked for the related to this ..but no body tell me, so thats why am facing problem in syntax mostly
-
March 25th, 2011, 05:53 AM
#14
Re: Reading Text line from .txt File
Originally Posted by adnan86
hypheni : Yup i am new to MFC Windows programming. i asked for the related to this ..but no body tell me, so thats why am facing problem in syntax mostly
New to MFC, Start off with some windows programming book then move to MFC.
◄◄ hypheni ►►
-
March 25th, 2011, 05:55 AM
#15
Re: Reading Text line from .txt File
Originally Posted by adnan86
...
now i want to write data from the Edit Box.. its writing random numbers not the string
@code:
ofstream Students("test.txt", ios: ut);
CString name;
name=m_editbox;
Students << name ;
Students.close();
As you were already told in your another thread:
Originally Posted by GCDEF
... CStdioFile makes more sense in an MFC app than streams because it will write directly to a CString and uses MFC Exceptions.
Victor Nijegorodov
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|