how get a whole line of file every time

This is a discussion on how get a whole line of file every time within the C++ Programming forums, part of the General Programming Boards category; Now, there is a file named a.txt whose contents is as follows: Code: adddd dddddddddd sdf I want to store ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    168

    how get a whole line of file every time

    Now, there is a file named a.txt whose contents is as follows:
    Code:
    adddd
    dddddddddd
    sdf
    I want to store them useing list structure,
    how to solve it ?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    1) open the file as an ifstream
    2) use getline on the ifstream to get a string
    3) push a copy of the string onto the list.
    Code:
            ifstream file("path_to_file",ios::in);
    	list<string> data;
    	string buffer;
            while (getline(file,buffer)) data.push_back(buffer);
            file.close();
    To check:
    Code:
    	list<string>::iterator it = data.begin();
    	while (it != data.end()) {
    		cout << *it <<endl;
    		it++;
    	}
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by MK27 View Post
    1) open the file as an ifstream
    2) use getline on the ifstream to get a string
    3) push a copy of the string onto the list.
    Code:
            ifstream file("path_to_file",ios::in);
    	list<string> data;
    	string buffer;
            while (getline(file,buffer)) data.push_back(buffer);
            file.close();
    To check:
    Code:
    	list<string>::iterator it = data.begin();
    	while (it != data.end()) {
    		cout << *it <<endl;
    		it++;
    	}
    great! thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Very strange error...
    By Blackroot in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2006, 12:37 AM
  4. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 05:04 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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