Thread: Code not working

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    Code not working

    Hey guys I am just wondering why the line of code below doesn't work? Is it because i am reading a line of text a word at a time?

    Thanks.

    Code:
    char array [200];
    ifstream file;
    
    if (file.is_open()) 
    
    	{
                 while (file >> array)
    	          {
    			if(strstr(array,"hello james") != 0)
                            count++;
                       }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You're going to have to be more specific than "doesn't work".
    Does it compile?
    Does it run, but crashes
    Does it run, finishes but produces the wrong output?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Hi sorry for not being clear, the problem is that if i have the words hello james hello kim in one line of text it will not increment the count even though hello james is there.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    This cannot work.
    Code:
    file >> array
    will stop reading when it finds a space
    so
    Code:
    if(strstr(array,"hello james") != 0)
    will alwais fail
    use getline() instead.
    Kurt

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks, I am not sure how the getline() works in this way could you give me a getline example please?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    string line;
    getline(inFile, line);

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    another possibility
    Code:
    while (file.getline( array, sizeof(array) ))
    Kurt

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks i need to keep the other code I have as well for reading in a word at a time so do i have to close the file and open it again or can I just continue with another if and while as follows:

    Code:
    if (file.is_open()) 
    
    	{
                 while (file.getline( array, sizeof(array) ))
    	          {

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You can either close and open the file and read it another time, or use clear() to reset the eof condition and us seekg() to set the getpointer to the beginning of the stream.
    Kurt

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks so is it ok to close and open the file again in the same function using the same ifstream variable or would that cause problems?

  11. #11
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    No problem.

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Does getline allow me to find multiple things on the same line with the strstr function for example,

    hello james hello james

    Thanks again.

  13. #13
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    me no understand.
    Kurt

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by ZuK
    me no understand.
    Kurt
    lol....
    Does getline allow me to find multiple things on the same line
    getline() stuffs some characters into a variable. What you subsequently do with that variable is not up to getline().
    Last edited by 7stud; 02-17-2006 at 01:25 PM.

  15. #15
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Sorry if that was confusing I mean that if hi james appears twice on the same line will the code I used pick up on both?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code not working?
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2009, 01:57 AM
  2. Replies: 3
    Last Post: 02-24-2009, 08:49 PM
  3. C code not working
    By D3ciph3r in forum C Programming
    Replies: 2
    Last Post: 05-27-2005, 04:13 PM
  4. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 12:00 PM