scanf

scanf() (and cin) statements skipped when using gcc.

When multiple scanf() statements are encountered in the code, then, except the first scanf() statement, all others are skipped, that is, there is no prompt for input for those scanf() statements when the code is run. I have a tried a few suggestions. For eg, use of flushall() was suggested on some site, but that gives a compilation erro...

How do I use scanf() with fopen

I'm writing a program and am having trouble using the scanf and fopen working together. From what I can tell my erroneous lines seems to be: FiLE * DataFile DataFile = fopen("StcWx.txt","r"); scanf(DataFile, "%i %i %i %.2f %i %i", &Year, &Month, &Day, &Precip, &High, &Low); The file it opens from has a list of weather data that look...

replace the stdin for a file

Hello, I got a program with a fscanf like this: fscanf(stdin, "%d %d,.... I got many fscanf and files that I'd like to test, the files are like this 10485770 15 51200000 -2 10 10 10485760 10485760 10 10485760 10485760 10 10485760 10485760 Well my question is how can I tell to the program or the compiler to take the inputs not from ...

Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?

Why is it that scanf() needs the L in "%lf" when reading a double, when printf() can use "%f" regardless of whether its argument is a double or a regular-precision float? Example code: double d; scanf("%lf", &d); printf ("%f", d); ...

parsing input with scanf in C

I've been having a lot of problems trying to figure out how to use scanf. It seems to work fine with integers, being fairly straight forward scanf("%d", &i). Where I am running into issues is using scanf in loops trying to read input. For example: do { printf("counter: %d: ", counter); scanf("%c %c%d", &command, &prefix, &input);...

Making fscanf Ignore Optional Parameter

I am using fscanf to read a file which has lines like Number <-whitespace-> string <-whitespace-> optional_3rd_column I wish to extract the number and string out of each column, but ignore the 3rd_column if it exists Example Data: 12 foo something 03 bar 24 something #randomcomment I would want to extract 12,foo; 03,bar; 24, som...

Are there any practical applications for the format %n in printf/scanf family?

int x; printf("hello %n World\n", &x); printf("%d\n", x); ...

How to validate input using scanf

How can I validate the user input by using scanf. Right now I have something like this, but doesn't work. NOTE: I have the atoi just to validate that the scanf validation works. scanf("%[0987654321.-]s",buf); i = atoi(buf); if(i) index = i; ...

Looking for C# equivalent of scanf

I used to code in C language in the past and I found the scanf function very usefull. Unfortunately, there is no equivalent in C#. I am using using it to parse semi-structured text files. I found an interresting example of scanf implementation here. Unfortunately, it looks old and uncomplete. Do anyone knows a scanf C# implementation ...

A Beginner's scanf_s() Disability

int main(void) { char tmp, arr[100]; int i, k; printf("Enter a string: "); scanf_s("%s", arr); for ( k = 0, i = (strlen(arr) - 1); k < (int) (strlen(arr) / 2); --i, ++k) { tmp = arr[k]; arr[k] = arr[i]; arr[i] = tmp; } puts(arr); return 0; } I know that there is so...

C/C++ read a byte from an hexinput from stdin

Hi.. Can't exactly find a way on how to do the following in C/C++. Input : hexdecimal values, for example: ffffffffff... I've tried the following code in order to read the input : uint16_t twoBytes; scanf("%x",&twoBytes); Thats works fine and all, but how do I split the 2bytes in 1bytes uint8_t values (or maybe even read the first ...

fscanf reading problem

Hi, sorry, perhaps this is a stupid question. I have a file like this: 36 146 10 53 240 133 104 28 51 81 124 ... so I want to read the numbers from a program, so I do: .... some function ..... int i; unsigned char key[16]; FILE *fp; printf ("\n ------ \n"); // open filename fp = fopen("key.txt","a"); printf("...

sscanf and strings

Hi I'm parsing some CSV data in C for the purposes of a Ruby extension. In order to pull out the data from each row I'm using sscanf as follows: char* line = RSTRING_PTR(arg); double price; double volume_remaining; unsigned int type_id, range, order_id, volume_entered, minimum_volume, duration, station_id, region_id, solar_syst...

Hard-coded string in the format of scanf

I'm trying to match lines with a format like "point %d %d". So I only need to two those two integers, then the "point" is hard-coded in the format string. As I understand reading Linux man pages of scanf, this should work correctly. The next code, the way I want to use, the first call to scanf works, but the next calls scanf return with...

Can scanf identify a format character within a string?

Let's say that I expect a list of items from the standard input which are separated buy commas, like this: item1, item2, item3,...,itemn and I also want to permit the user to emit white-spaces between items and commas, so this kind of input is legal in my program: item1,item2,item3,...,itemn If I use scanf like this: scanf("%...

scanf() causing strange results

I have a piece of code that presents an interesting question (in my opinion). /*power.c raises numbers to integer powers*/ #include <stdio.h> double power(double n, int p); int main(void) { double x, xpow; /*x is the orginal number and xpow is the result*/ int exp;/*exp is the exponent that x is being raised to */ printf(...

How do you allow spaces to be entered using scanf?

Using the following code: char *name = malloc(sizeof(char) + 256); printf("What is your name? "); scanf("%s", name); printf("Hello %s. Nice to meet you.\n", name); A user can enter their name but when they enter a name with a space like Lucas Aardvark, scanf() just cuts off everything after Lucas. How do I make scanf() allow spaces...

Difference between scanf() and fgets().

I want to know what is the difference between fgets() and scanf(). I am using C as my platform. ...

How does scanf() work inside the OS?

I've been wondering how scanf()/printf() actually works in the hardware and OS levels. Where does the data flow and what exactly is the OS doing around these times? What calls does the OS make? And so on... ...

How can I debug a program using scanf with ddd?

When ddd encounters a scanf statement, it displays "Waiting until GDB gets ready" message. The debugging activity stops here. Please guide me of overcoming this bug. I'm using an amd64 athlon processor. ...