Getting one line of a text file

This is a discussion on Getting one line of a text file within the C++ Programming forums, part of the General Programming Boards category; How do i get one line of a text file and turn it into a string? Using fstream.h IE. Get ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    387

    Getting one line of a text file

    How do i get one line of a text file and turn it into a string?
    Using fstream.h

    IE. Get line 2 and make it char line2[100];

  2. #2
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    Heh ;something i actually know:

    #include <iostream.h>
    #include <fstream.h>

    char line[100];

    int main()
    {
    ifstream intput('whatever.txt");

    input>>line;
    cout<<line;
    input.close();
    return 0;
    }


    hehehe
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    where/can you specify what line you want?
    Last edited by Okiesmokie; 03-01-2002 at 08:11 PM.

  4. #4
    Registered User Tazar's Avatar
    Join Date
    Aug 2001
    Posts
    13
    Ok using fstream.h I would just loop throu all the lines you don't want and then grab the one you want like this:

    Code:
    #include <fstream.h>
    #include <iostream.h>
    
    #define LINE_NUMBER 2
    
    int main()
    {
     ifstream i;                                                   //Main file object.
     char buff[100];                                           //Input buffer.
     i.open("whatever.txt");                              //Open file.
     //Go throu all line tells the right one.
     for(int count=0;count<LINE_NUMBER;count++)
     {
       i.getline(buff,100);                                   //Get line.
     }  
     cout<<"Line "<<LINE_NUMBER<<" in the file was the data: "<<buff<<endl;                                 //Tell user results.
     i.close();                                                     //Close file.
     return 0;                                                    //Done.
    }
    Hope that helps ^_^
    Tazar The Demon
    -------------------------------------
    Please check my site out at: http://www.lrs.8m.com thanks =)
    -------------------------------------
    Note:
    I will help newbies and any other person that I can just ask I'll try to help you out =)

    My MSN Messager name is: Tazar37@hotmail.com see if I'm online maybe we can talk. =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 07:13 PM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21