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

    Get specific line from text file

    I am trying to get a specific line in a text file which only has one word per line and copy it to a string. Can anyone give a simple example?

    thanks

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Get specific line from text file

    You cannot "get" it directly from a file.
    You should read the whole file line-by-line and only save the lines you do need.
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2011
    Posts
    10

    Re: Get specific line from text file

    thanks I figured that one out, how about writing a word to the line you want.

    Is it possible to post an example please?

    thanks

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Get specific line from text file

    The same way: read the file, change the line you want to, then write in to the file back (overwriting it)
    Victor Nijegorodov

  5. #5
    Join Date
    Nov 2011
    Posts
    10

    Re: Get specific line from text file

    This is what I used to read a specific line. :
    Code:
    ifstream f("input.txt");
    	string s;
    	getline(f, s);
    	getline(f, s);
    	getline(f, s); //here is 3rd line
    	
    	f.close();
    	
    	cout << s << endl;
    I need to overwrite that line now, can you post some hints pls.

    thanks

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Get specific line from text file

    You cannot overwrite "one line only". You have to overwrite the whole tail of the file beginning with this line.
    Victor Nijegorodov

  7. #7
    Join Date
    Nov 2011
    Posts
    10

    Re: Get specific line from text file

    Where can I find more examples using the fstream and familiarize myself with it?

    thanks

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Get specific line from text file

    1. MSDN
    2. Google
    3. CG Forums.

    Just search for the appropriate examples/samples...
    Victor Nijegorodov

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