CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2011
    Posts
    14

    reading a line from text file

    // text_read.cpp
    // compile with: /clr
    #using<system.dll>
    using namespace System;
    using namespace System::IO;

    #include <iostream>

    using namespace std;
    int main()
    {
    String^ fileName = "e:\\test.txt";
    try
    {
    //Console::WriteLine("trying to open file {0}...", fileName);
    StreamReader^ din = File::OpenText(fileName);

    String^ str;
    int count = 0;
    while ((str = din->ReadLine()) != nullptr)
    {
    count++;
    Console::WriteLine("line {0}: {1}", count, str );
    }
    }
    catch (Exception^ e)
    {
    if (dynamic_cast<FileNotFoundException^>(e))
    Console::WriteLine("file '{0}' not found", fileName);
    else
    Console::WriteLine("problem reading file '{0}'", fileName);
    }


    system("pause");
    //std::cout << "Paused, press ENTER to continue." << std::endl;
    //cin.ignore(1000000);

    return 0;

    }




    with that above code i am able to read from a text file and display the content. But it displays the whole content of the file. Please help me modify the codes so that I can only display one particular line.
    Help will really be appreciated

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

    Re: reading a line from text file

    I didn't check you code, for reading line wise use this code..

    Code:
    char line[100];
    ifstream in("file.txt");
    while (!in.eof())
    {
      in.getline(line, 200, '\n');
    }
    in.close();
    And from next time post simple C/C++ query in C/C++ forum not in VC++ forum..

  3. #3
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: reading a line from text file

    put a break statement within the while loop after reading one single line.. it will come out after displaying one line..

  4. #4
    Join Date
    Feb 2011
    Posts
    14

    Re: reading a line from text file

    Quote Originally Posted by vcdebugger View Post
    put a break statement within the while loop after reading one single line.. it will come out after displaying one line..
    could you please show me how it is done..
    thank u

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

    Re: reading a line from text file

    You dont know how to use 'break' and whats the use of break.

    If not, then go and read some C books before posting further.

  6. #6
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: reading a line from text file

    Your code is managed C++, not native C++. Try asking your question here.

  7. #7
    Join Date
    Jul 1999
    Location
    Sth Australia, Australia
    Posts
    492

    Re: reading a line from text file

    Is this to do with your previous post regarding the setting of controls using a text file if it is the code I attached to my last response reads the file and retreives the required data just fine.
    Please advise of solution it makes it easier to find an answer.

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

    Re: reading a line from text file

    Quote Originally Posted by Skizmo View Post
    Your code is managed C++, not native C++. Try asking your question here.
    Oh yes. You are right. I didn't even check those namespace before posting an ans.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured