Thread: Issues reading text files

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    36

    Issues reading text files

    Hi guys

    I've just started playing with reading files and am unsure how to read in files that have the foloowing format:

    <string> <string> <int>
    <string> <string> <int>
    ....

    A white space seperates each entry in the file.

    Heres my code to date:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    
    {
    	
    	string str1, str2, input, temp;
    	
    	int int1;
    
    	ifstream file ("test.txt");
    	if (file.fail()){
    
    		cout << "error on opening file";
    	}
    
    	if (file.is_open()){
    		while (!file.eof())
    		{
    			file >> input;
    
    			//how to get individual parts???
    			// str1 = 1st string
    			// str2 = 2nd string
    			// int1 = int
    
    			cout << "str1: " << str1
    				 << ", str2: " << str2
    				 << ", int1: " << int1 << endl;
    
    		}
    
    	}
    
    	file.close();
    	
    	cin.ignore();
    	cin.ignore();
    
    	return 0;
    }

    Any ideas what I need to do?

    BTW - Is the approach I've gone through so far the best??

    Thanks in advance

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Easy...
    Code:
    while (!file.eof())
    {
        file >> str1 >> str2 >> int1;
    
        cout << "str1: " << str1
               << ", str2: " << str2
               << ", int1: " << int1 << endl;
    }
    As long as the strings themselves don't contain any whitespace that should work.
    "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
    Registered User
    Join Date
    May 2004
    Posts
    36

    Neat

    Thanks for that.

    So if the strings were to contain white space what would be the best approach? I can change the deliminator to another character say a comma (,).

    How would you handle this?

    I looked to find a split string command in c++ but didnt coma across anything

    Cheers

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If your strings are going to contain possible whitespace and you want to use ',' as a delimiter, then you can use the getline function. As an example, if your file looks like this:

    Code:
    hello there, my friend, 46
    I'm doing fine, thank you, 16
    Then something like this should work:

    Code:
    while (!file.eof())
    {
        getline(file,str1,',');
        getline(file,str2,',');
        file >> int1;
        file.ignore(80,'\n');  // 80 is somewhat arbitrary but should be sufficient
        cout << "str1: " << str1
               << ", str2: " << str2
               << ", int1: " << int1 << endl;
    }
    Output should be:

    Code:
    str1: hello there, str2: my friend, int1: 46
    str1: I'm doing fine, str2: thank you, int1: 16
    "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

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    36
    Thanks for that - cracked it now.

    I did see the getLine function when hunting but didnt realise that you pull parts out of the input string according to the deliminator.

    Oh is there a split string function as in VB? Could be usefult to know...

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    There is a substr (sub-string) member function for string objects that may be what you want.

    Code:
    string str("Hello World!");
    cout << str << endl;             // Output: Hello World!
    cout << str.substr(2) << endl;   // Output: llo World!
    cout << str.substr(3,4) << endl; // Output: lo W
    str = str.substr(4);             // str now equals: o World!
    cout << str << endl;             // Output: o World!
    cout << str.substr(2) << endl;   // Output: World!
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. Reading in an array of text from a file?
    By suzakugaiden in forum C++ Programming
    Replies: 6
    Last Post: 01-04-2006, 03:17 PM
  3. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 07:13 PM
  4. reading text files
    By stimpyzu in forum C++ Programming
    Replies: 11
    Last Post: 04-17-2004, 07:45 AM
  5. reading certain parts of text files
    By Captain Penguin in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 09:45 AM