Thread: Programme can not read data file

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    Programme can not read data file

    Hi, I was wondering if anyone could point me out the problem of the programme below. It can not read the data file it was supposed to read. When I run the programme, the output is just "Line:". I am kind of beginner for C++ so there is a chance that I might be making a very stupid mistake. Anyway, here is the code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    void Parse(string);
    
    int main()
    {
    	fstream InFile("WindStation00.data", ios::in);
    	
        string Line;
    
    	while(!InFile.eof())  {
    	    InFile >> Line;
    		cout << "LINE: " << Line << endl;
    	    Parse(Line);
    
    	}
        InFile.close();
    	system("pause");
    }
    	
    void Parse(string Line)
    {	
    		string ID, Date, Hour;
    		double Speed;
    		int  Direction;
    
    		cout << "\t" << strtok((char*)Line.c_str(), ":") << endl;
    		cout << "\t" << strtok(0, ":") << endl;
    		cout << "\t" << strtok(0, ":") << endl;
    		cout << "\t" << strtok(0, ":") << endl;
            cout << "\t" << strtok(0, ":") << endl;
           
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    To read a line, you probably want to use std::getline instead of the overloaded operator>>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Since you are using std::string I would recommend that you use a stringstream and std::string getline() function, with the optional third argument, instead of the strtok() to parse your string in your Parse function.

    Jim

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jimblumberg
    instead of the strtok() to parse your string.
    In fact, what you are currently doing now is wrong: c_str() returns a const char*, and cast this to non-const is only acceptable if you will not modify the contents, but you do by using strtok.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    He's still making the same mistakes he did a coupe days ago. Doesn't seem to be learning.
    "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

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I am kind of beginner for C++ so there is a chance that I might be making a very stupid mistake.
    Yes, it's called "Not paying attention!"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 11-03-2007, 02:56 PM
  2. how can i read data from file... plz help me
    By fnfn in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2007, 06:39 AM
  3. read data from file
    By matth in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2005, 09:37 AM
  4. Read data from file !!!
    By frankiepoon in forum C Programming
    Replies: 2
    Last Post: 10-14-2002, 11:45 PM
  5. read data out of *.txt file
    By dune911 in forum C Programming
    Replies: 3
    Last Post: 12-14-2001, 12:33 PM