Thread: Search a text file for a number

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    Search a text file for a number

    hey, I am a bit puzzle right now. I have a text file I must search to find a number 0

    where am I going wrong?

    Code:
    while(feof(fptr))
    	{
    		fscanf(fptr,"%s",zeros);
    			if (zeros == "0")
    			{
    				z++;
    			}
    
    	}

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because zeros == "0" can never be true. If you want to compare the contents of two strings, use the string compare function strcmp.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    You can find the reference for strcmp() here.

    Do note, however, that strcmp() will return NULL when the strings given are identical.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Do note, however, that strcmp() will return NULL when the strings given are identical.
    Zero actually, not NULL.
    Other string functions return a NULL pointer in some cases, but strcmp() returns 0, something >0 or something <0
    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.

  6. #6
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Quote Originally Posted by Salem View Post
    > Do note, however, that strcmp() will return NULL when the strings given are identical.
    Zero actually, not NULL.
    Other string functions return a NULL pointer in some cases, but strcmp() returns 0, something >0 or something <0
    Ah, my bad. Sorry.

    You'd want something like this, then . . .

    Code:
    char c[256];
    while(fgets(c, 256, fp)) {
        if(strcmp(c, "0") == 0) z ++;
    }

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You'd want something like this, then
    fgets leaves \n char in the buffer - so strcmp will never give positive result
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Been a while since I've done file I/O, can you tell?

    Yes, vart is correct. You'll have to strip the \n from the string. There should be something about it in the FAQ, IIRC.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    thanks

    thanks guy I got it done

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Replies: 19
    Last Post: 05-30-2007, 05:47 PM
  4. Counting the number of lines in a text file - help
    By Erkan in forum C Programming
    Replies: 11
    Last Post: 11-12-2005, 05:12 PM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM