Fscanf to get number and replace.

Hi,
I have a file with contents like "abcd 1234" .What i need is get that integer and replace that with 0. So i used fscanf(fp,"%s %d", str, &num); This is having some problem.
There can be multiple space/tab between string and number. How to replace that number with 0 in same file?

This is having "some problem" -- which some problem? Be specific.

Files don't work that way. You can't delete characters in the middle like a text editor to make it shrink. What pretty much everything does is create a new file, and replace the contents of the old one with it.

Don't use fscanf. It's got "features" most people would consider bugs. Use fgets and sscanf to avoid its buffering problems.

char buf[4096];
FILE *fpold=fopen("filename", "r");
FILE *fpnew=fopen("newfile", "w+");

while(fgets(buf, 4096, fp) != NULL) // Read all lines from filename one-by-one
{
        int val;
        char *last=(buf+strlen(buf))-1; // Point to last character

        while(isspace(*last)) last--; // Loop backwards across the ending newline
        while(isdigit(*last)) last--; // Loop backwards across digits
        // 'last' now points to the first non-digit before the number.
        last[0]='\0'; // Change it into a NULL.
        last++; // Point to the character after, i.e. the number itself.

        // 'buf' now points to the string and only the string,
        // and 'last' points to the number and only the number.

        sscanf("%d", &val); // convert string into integer

        ////////
        // To alter the contents of the file, alter 'buf' or 'val' here.
        ////////

        fprintf(fpnew, "%s %d\n", buf, val); // Write all contents back into 'fpnew'.
}

fclose(fpold);

// Now, if you really want to, you can re-open fpold as write, read lines one-by-one from fpnew, and write them back into fpold.
rewind(fpnew);
...

I avoid the *scanf*() functions like the plague. If they are passed unvalidated input, results are "undefined". If you're lucky your process will just SEGV and not cause any damage, if you're not you'll get corrupt data in some way.

To validate the input you have to know what it is and what standards to check it against. In other words, you have to parse and interpret the data. Because you pretty much can't safely call one of those functions without doing that.

Once you do that, there's no point in calling one of the *scanf*() functions.

False. They stop scanning at the first result they don't recognize, and return a convenient value to tell you if/where it stopped.

Also untrue. They crash if you give them bad parameters and don't check the return value. So give them good parameters... And check the return value.

fscanf has a buffering problem, as I mentioned above, so sscanf is preferred.

What you say does not make sense.
If you specify a space in the format multiple white space is skipped:

fscanf

"A directive composed of one or more white-space characters is executed by reading input until no more valid input can be read, or up to the first byte which is not a white-space character which remains unread."

Bare scanf is not a great idea, use fgets and sscanf:

something like:

#include <stdio.h>
#include <string.h>

typedef char buffer[128];


char * chomp(char * line)
{
    char **p = &line;
    strsep(p, "\r\n");
    return line;
}

int main( int argc, char **argv )
{
    buffer      in, out;
    int number;

    while ( fgets(in, sizeof in, stdin) ) {
        chomp(in);
        sscanf(in, "%s %d\n", out, &number); 
        printf("%s = %s %d\n", in, out, number);
    }

    return 0;
}