is there substitute for scanf/scanf_s?

Is there any substitute for command scanf to extract number from char array? I found one problem with it: it waits for keyboard input. Do I use it incorrectly or is there any way how to make it no waiting? Just to run and print the number without pause.

1
2
3
4
5
6
7
8
#include <stdio.h>

int main()
{
    int theNumber = 0;
    scanf_s("file%d.txt", &theNumber);
    printf("your number is %d", theNumber);
}
Last edited on
It doesn't make sense that it would wait for keyword input unless the file you are reading from is somehow mapped to the keyboard.

Maybe your problem is that you are not flushing stdout. printf will automatically flush if you output a newline character.
 
printf("your number is %d\n", theNumber);

If you don't want a newline you can instead call fflush yourself, right after the call to printf.
 
fflush(stdout);
What you're looking for is sscanf:

http://www.cplusplus.com/reference/cstdio/sscanf/

See how it's used
Oh, thanks. The scanf does not do what I expected. This code works like charm:
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include "conio.h"

int main()
{
char* filename = (char*)"file21.txt";
int theNumber = 0;
sscanf_s(filename, "file%d.txt", &theNumber);
printf("your number is %d", theNumber);
_getch();
}
Topic archived. No new replies allowed.