Like Tree3Likes
  • 1 Post By laserlight
  • 2 Post By hk_mp5kpdw

Programme can not read data file

This is a discussion on Programme can not read data file within the C++ Programming forums, part of the General Programming Boards category; Hi, I was wondering if anyone could point me out the problem of the programme below. It can not read ...

  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
    21,914
    To read a line, you probably want to use std::getline instead of the overloaded operator>>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    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
    21,914
    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.
    jimblumberg likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,803
    He's still making the same mistakes he did a coupe days ago. Doesn't seem to be learning.
    Salem and stahta01 like this.
    "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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 11:33 AM

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