Search a text file for a number

This is a discussion on Search a text file for a number within the C Programming forums, part of the General Programming Boards category; hey, I am a bit puzzle right now. I have a text file I must search to find 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,185
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    32,851
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    You'd want something like this, then
    fgets leaves \n char in the buffer - so strcmp will never give positive result
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 04: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, 06: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, 09:39 AM

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