Thread: Reading lines of a text file

  1. #1
    george7378
    Guest

    Reading lines of a text file

    Hi,

    Is there a way to read the contents of a text file line by line? For example, I have a text file which has two numbers on two different lines like this:

    4
    5

    ...is there a way in C++ to load the text file, and then take the '4' from line one and assign it to a variable, and then read the '5' from line two and assign it to a separate variable? e.g:

    Code:
    // Code to open text file and read the values of lines one and two.
    
    int line1, line2;
    
    line1 = textfile.line1;
    line2 = textfile.line2;
    
    cout << line1 << " is the first line of the file, and " << line2 << " is the second line." << endl;
    Failing that, is there a way to define variables in a text file, for example if I have a text file with the following contents:

    variable1 = 4
    variable2 = 5

    ...is it possible to define variable1 and variable2 in a program, open the text file and then assign the values given to variable1 and variable2 in the text file to the variables in the program?

    Thanks!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Well, to read in two numbers from a user using std::cin you'd do:
    Code:
    cin >> line1 >> line2;
    You can do the exact same thing using your file stream object in place of the cin. That would work provided your file contains just those two values regardless of whether they were on one line (white space separated) or on separate lines.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    george7378
    Guest
    Oh right - so if I define the variables line1 and line2 in my program, and then make a text file containing:

    line1 = 4
    line2 = 5

    ...and then use the file stream to input the values, I will end up with 4 and 5 stored in line1 and line2?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You may want to study this link on basic file i/o.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice reading lines from a text file.
    By Fujitaka in forum C Programming
    Replies: 2
    Last Post: 08-11-2009, 09:43 PM
  2. Reading Lines From a File
    By Dangerous Dave in forum C++ Programming
    Replies: 6
    Last Post: 02-22-2005, 01:17 PM
  3. Reading lines in from a file?
    By JarJarBinks in forum C++ Programming
    Replies: 8
    Last Post: 08-30-2004, 09:46 PM
  4. Reading new lines in a file
    By abrege in forum C++ Programming
    Replies: 11
    Last Post: 12-16-2002, 09:57 PM
  5. lines of a text file
    By face_master in forum C++ Programming
    Replies: 8
    Last Post: 11-06-2001, 07:09 AM