Hello

I have a following line inside a .txt file
This$is$my$Input$203
I would like to know whether is it possible only to store the 203 value in variable using fscanf. So without declaring char arrays to store all those strings.
I was thinking of something like this ;

fscanf(fp,"%*[^$]$%*[^$]$%*[^$]$%*[^$]$%d",&number)

But its just plain stupid ..

Thanks for any input
Joey

This is more of a job for something like strrchr. Assuming you have the line in memory already:

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

int main(void)
{
    const char *line = "This$is$my$Input$203";
    const char *start = strrchr(line, '$');

    if (start != NULL) {
        /* Error checking omitted for brevity */
        printf("Found %d\n", (int)strtol(start + 1, NULL, 0));
    }

    return 0;
}

fscanf(fp,"%*[^$]$%*[^$]$%*[^$]$%*[^$]$%d",&number)

How is that "just plain stupid"? It looks fine to me. Of course, there are other ways to do it, but fscanf would work just fine.

How is that "just plain stupid"? It looks fine to me. Of course, there are other ways to do it, but fscanf would work just fine.

If the input is guaranteed to have four words separated by '$', then an integer, it'll work fine (in an ugly way), but the example string doesn't strike me as strictly formatted, in which case fscanf would be a bad idea. What happens if another word is added, or a word is removed?

Finally it is complete !!
Thanks Naure!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.