Thread: help reading words from a datafile

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    67

    help reading words from a datafile

    so what im trying to do is read one line words at the time and seperate the words too.

    so I read line 1:
    add cars

    line 2:
    subtract cars
    my progarm should know they are in the same line and store the words separete
    and I will do something with word 2 depending on what word one was.

    so I will have a function that adds the word cars to an array in line 1

    here is my code that reads a datafile
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>  
    
    using namespace std;
    
    int main() {
    
        string word;
        ifstream input;
        
        input.open("test.dat",ios::in);
    
        while (input >> word) {
    
          cout << word << endl;}
        input.close();
    
    
    
      return 0;
    }

    i want it to do this read one line at the time and separate the words
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>  // for istringstream
    
    using namespace std;
    
    int main() {
      istringstream strLine;
      string line, word;
      int currentLine=0;
    
      while (getline(cin,line)) {
        ++currentLine;
        
        // clear the strstream and copy the entered line to it
        strLine.clear();
        strLine.str(line);
    
        // now get each word-sequence from the strLine stream
        while (strLine >> word)
          cout << currentLine << ": " << word << endl;
        }
    
      return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is there a question you're asking? What is wrong with your second version (other than "I am printing the words instead of storing them")?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    well I would like my first code the do the same as the second code post, that is read lines of words not just words separated by spaces.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    well I tried to modify my first program to read a line but this is what I have. We just started reading about datafiles.

    Code:
    0001 #include <iostream>
    0002 #include <string>
    0003 #include <sstream>  
    0004 
    0005 using namespace std;
    0006 
    0007 int main() {
    0008     
    0009     istringstream strLine;
    0010     string line, word;
    0011     //string word;
    0012     ifstream input;
    0013     
    0014     input.open("test.dat",ios::in);
    0015 
    0016     while (input.getline(cin,line)) {
         error: no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::getline(webistream&, std::string&)'
    0017 
    0018       input.strLine.clear();
         error: 'struct std::ifstream' has no member named 'strLine'
    0019      input.strLine.str(line);
         error: 'struct std::ifstream' has no member named 'strLine'
    0020 
    0021     // now get each word-sequence from the strLine stream
    0022     while (input.strLine >> word)
         error: 'struct std::ifstream' has no member named 'strLine'
    0023       cout << ": " << word << endl;
    0024 
    0025 
    0026 
    0027 
    0028 
    0029     }
    0030     input.close();
    0031 
    0032 
    0033 
    0034   return 0;
    0035 }

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    There are two versions of getline() one for C-strings the other for std::string. You first insure that you are using the correct version and that you are calling the function correctly. Also you may want to review the string stream class.

    Jim
    Last edited by jimblumberg; 08-09-2011 at 08:32 PM.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    So, basically what you want to do is read in your file line by line and break the lines up into individual words? If that is the case you would need to do something like this:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    
    //just used to create the file we are going to read from
    void createFile(void);
    
    int main(){
    
    	//our string to hold our line from file and our temp
    	std::string myLine, temp;
    	//our string stream object to parse myLine
    	std::stringstream myWord;
    	//call our function to make our file
    	createFile();
    
    	//open our file for reading
    	std::fstream myFile("example.txt", std::ios::in);
    
    	//Ensure our file is open
    	if(myFile.is_open()){
    
    		//Loop through our file until EOF
    		while(std::getline(myFile, myLine)){
    
    			std::cout << std::endl << "Our line from file: " << myLine << std::endl;
    			std::cout << "Our parsed line: " << std::endl;
    			
    			//Copy our line to parse
    			myWord.str(myLine);
    
    			//break up our string using spaces as delimiter
    			while(myWord >> temp){
    				std::cout<< temp << std::endl;
    			}
    			
    			//reset our object
    			myWord.clear();
    		}
    
    		//close our file
    		myFile.close();
    	}
    	
    	std::cin.get();
    	return (0);
    }
    void createFile(){
    	
    	//create our file for writing
    	std::fstream myFile("example.txt", std::ios::out);
    	
    	//ensure our file is open and write to it
    	if(myFile.is_open()){
    		myFile << "First line of text" << std::endl;
    		myFile << "Second line of text" << std::endl;
    	}
    	
    	//close our file
    	myFile.close();
    }
    Last edited by AndrewHunter; 08-10-2011 at 01:15 AM. Reason: Fixed stupidity
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading an unknown numbers of words
    By schutter07 in forum C Programming
    Replies: 3
    Last Post: 11-22-2010, 12:32 PM
  2. reading words into set
    By AJOHNZ in forum C++ Programming
    Replies: 3
    Last Post: 09-06-2009, 06:13 PM
  3. reading words using fread
    By -EquinoX- in forum C Programming
    Replies: 22
    Last Post: 05-05-2008, 02:09 PM
  4. Reading words and analyzing words from file
    By desmond5 in forum C Programming
    Replies: 7
    Last Post: 02-26-2008, 03:51 PM
  5. Error after reading all words from a .txt
    By Axolotl in forum C Programming
    Replies: 9
    Last Post: 02-02-2003, 08:35 PM