Reading Text line from .txt File
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: Reading Text line from .txt File

  1. #1
    Join Date
    Mar 2011
    Posts
    14

    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.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Reading Text line from .txt File

    Read up on CStdioFile

  3. #3
    Join Date
    Mar 2011
    Posts
    14

    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

  4. #4
    Join Date
    Mar 2011
    Posts
    14

    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);

  5. #5
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    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 ►►

  6. #6
    Join Date
    Mar 2011
    Posts
    14

    Re: Reading Text line from .txt File

    Thanks for the Help.
    I did this but this time nothing is getting in the ReadStr variable

  7. #7
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    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 ►►

  8. #8
    Join Date
    Mar 2011
    Posts
    14

    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

  9. #9
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011

    Re: Reading Text line from .txt File

    Quote Originally Posted by adnan86 View Post
    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

  10. #10
    Join Date
    Mar 2011
    Posts
    14

    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();

  11. #11
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    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 ►►

  12. #12
    Join Date
    Mar 2011
    Posts
    14

    Re: Reading Text line from .txt File

    I dont know eactly how to tag code...SOrry

  13. #13
    Join Date
    Mar 2011
    Posts
    14

    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

  14. #14
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Reading Text line from .txt File

    Quote Originally Posted by adnan86 View Post
    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 ►►

  15. #15
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,011

    Re: Reading Text line from .txt File

    Quote Originally Posted by adnan86 View Post
    ...
    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:
    Quote Originally Posted by GCDEF View Post
    ... CStdioFile makes more sense in an MFC app than streams because it will write directly to a CString and uses MFC Exceptions.
    Victor Nijegorodov

Page 1 of 2 12 LastLast

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center