Thread: programming a word scanner problem

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    40

    programming a word scanner problem

    Dear all , I tried to programe a scanner that scans a text file and tables the tokens inside , but my scanner can't see the spaces and the end lines

    my simple code tries to open the file and look inside for printing it on screen .
    please what can I do for this ?

    Code:
     #include < iostream.h>
      #include < fstream.h >
      #include < string.h  >
    
    
    void main()
    {  
         char d;
        	ifstream input("c:\\test\\Data_file.txt");
        	if ( !input.is_open() ) 
    	     cout << "the file couldn't be opened !" ;
    	else cout << "the file is opend successfully !" ;
    
    cout << endl;
    for(int i=0 ; i<50; i++)
    	{
    	   //input.seekg(i,ios::beg);
    	   input >> d;
    	   cout << d;}
    
    }

    thank you all.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    afew points:

    - since this is c++ get rid of the .h in the includes
    - put 'using namespace std;' after the last include
    - change 'void main' to 'int main'
    - change your for loop to: while (! input.eof()), and add semicolon at the end of the while brace
    - change 'input >> d' to 'd = input.get();'

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> change your for loop to: while (! input.eof()),
    This is usually not a good idea. In this case you might end up outputting the last character twice.

    Check the result of the read as the control for the while loop. In this case that doesn't quite work because you are suggesting get(), but you should still check the status of cin after calling get() and before using the value retrieved.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    ok, mine was just a quick suggestion.. but at least change it from what the initial loop was!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. New problem [Beginner]
    By Vintik in forum C++ Programming
    Replies: 2
    Last Post: 08-18-2005, 11:33 PM
  3. Passing structures to a function
    By earth_angel in forum C++ Programming
    Replies: 5
    Last Post: 07-13-2005, 06:13 AM
  4. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM