Questions tagged [scanf]

Questions related to the scanf() family of functions in the C runtime library, which read and convert formatted data. (Includes scanf(), sscanf(), fscanf(), and their variadic equivalents.)

The scanf() function reads data with specified format from stdin. Originating from the C runtime library, scanf() is present in many other programming languages.

The scanf() function, in the C runtime library, reads input text for numbers and other data types from standard input. It returns the total number of items successfully matched, which can be less than the number requested. If the input stream is exhausted or reading from it otherwise fails before any items are matched, EOF is returned. The C prototype for scanf() is as follows:

int scanf(const char *format, ...);

The specification of conversion identifiers in format is a rich treasure of solutions—and occasional slight incompatibilities. The core functionality goes back to the beginnings of C. All of the ... parameters must be a pointer—note that an array name is a pointer type—to matching types of data.

Though popular for simple input in instructional programs, scanf() does not handle errors or complicated situations well, and many programmers recommend using alternative input techniques.

The tag is used for questions related to the use of the scanf(), sscanf(), and fscanf() functions and their derivatives. sscanf() reads from a string buffer; fscanf() reads from a FILE *; vscanf(), vsscanf(), vfscanf do—respectively—the same using a va_list instead of an explicit list of variables.

See also:

scanf documentation.

scanf() is the converse operation to and shares many of the same format specifiers.

7386 questions
2
votes
3 answers

sscanf function usage in c

I'm trying to parse xxxxxx(xxxxx) format string using sscanf as following: sscanf(command, "%s(%s)", part1, part2) but it seems like sscanf does not support this format and as a result, part1 actually contains the whole string. anyone has…
user1011346
  • 33
  • 1
  • 7
2
votes
3 answers

sscanf : retrieving int from a pattern with unknown occurrence number

I read an ascii file with an fstream. A line contains at least two repetitions of the following patern (and at most 128) : %d %llu %d %d %llu %d %d %llu For each line i need the max of the third %d of each pattern in the line i can't find a way to…
Tony Morris
  • 435
  • 4
  • 15
2
votes
1 answer

Can't pass pointers to scanf("%s",ptr);

So I commented out the scanf part (and just initialized it with my own string), why does it crash if I use scanf? I believe the actual arguments I've put in scanf(); are correct. #include #include int strendmilan(char *s,char…
jantristanmilan
  • 4,188
  • 14
  • 53
  • 69
2
votes
4 answers

referencing int in scanf

This question may look stupid but that's how people learn :).I learned C++ where you didn't have to worry about referencing int variable in cin>> . Today I was learning filehandling in C for school work but When I tried to run the program with this…
Alfred James
  • 1,029
  • 6
  • 17
  • 27
2
votes
4 answers

Read unsigned long long using fscanf()

How do i read a 64-bit unsigned integer from a file? I've got it stored as actual binary data, not a string representation.
nuju
  • 798
  • 2
  • 9
  • 17
2
votes
2 answers

Scanf doesn't appear to work in debug mode in Eclipse CDT with GDB

When running this code in debug mode: #include #include int main() { int a, b, c; scanf("%d%d%d", &a, &b, &c); printf("Values entered: %d %d %d\n", a, b, c); return EXIT_SUCCESS; } The program would not request…
Răzvan Flavius Panda
  • 21,730
  • 17
  • 111
  • 169
2
votes
3 answers

Stripping encapsulating characters in PHP using sscanf

Using PHP to call a Java app, the Java app returns a string encapsulated by "[....]", I need to remove the outside brackets and maintain what's inside -a mix of integers, strings, and other special chars like (, etc.. I'm using sscanf successfully…
jbrain
  • 561
  • 3
  • 7
  • 18
2
votes
2 answers

I need fscanf advice

I want to fprintf() 3 strings to a file, all on the same line. The first two cannot contain spaces while the 3rd may. I.E. word word rest of line Can some one tell me how to fscanf() that into 3 variables? I don't mind putting some delimiters if it…
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
2
votes
3 answers

C, Parsing some http headers

Im having some trouble parsing http headers. Here is my problem: char resp[] = "HTTP/1.1 200 OK\r\n" "Content-Type: text/html\r\n" "Content-Length: 4\r\n" "\r\n" "text"; // some stuff sscanf(resp,…
Zentdayn
  • 62
  • 2
  • 9
2
votes
2 answers

can't copy/read in right mode from a file to a list

i've a problem with read/write from file and view correctly the input: // LOAD THE LIST FROM THE FILE struct elemento *caricalista(struct elemento *p) { struct elemento *punt; FILE * utenti = fopen ("miarubrica.txt", "r"); char…
myself
  • 89
  • 1
  • 8
2
votes
1 answer

Separating fscanf error checking so I can deliver specific message?

Brand new to C here. The program is supposed to read in a file of fragments on a single line, separated by a "#" before and after them. For example #fragment1##fragment2##fragment3# The two errors I want to check for are that a fragment is not over…
Jason Block
  • 155
  • 1
  • 3
  • 9
2
votes
3 answers

Example: scanf and char errors

I am reading C for Dummies and am doing a example in the book. It told me to write it out line by line. Then it proceeds through the book even though the code has bugs. Here it is: #include int main() { char me[20]; printf("What is your…
IC2D
  • 471
  • 4
  • 11
  • 22
2
votes
1 answer

Why does Xcode seem to skip the second scanf() call?

I am new to Xcode development tool. To debug a problem, I have tried a very simple code: int main() {char N; char M; scanf("%c",&N); scanf("%c",&M); printf("%c",N); printf("%c",M); } But the problem is that the compiler doesn't seem to read…
epsilones
  • 11,279
  • 21
  • 61
  • 85
2
votes
1 answer

Understanding that scanf is doing in this code

Please help me in understanding the below code. The function get_digit takes a character argument by address. I am unable to get what scanf("%1[0123456789]", ch) does here. If I give 1234 on the terminal then it takes only the first digit. Same is…
Kundan Kumar
  • 1,974
  • 7
  • 32
  • 54
2
votes
1 answer

parse syslog message through sscanf

I got a syslog message <14>2012-04-19T03:54:18+08:00.527800 server3000 status: 00|00|The average CPU, memory, disk usage and total network flow are 5.0%, 5.0%, 5.0% and 5 bytes respectively for the last hour.\n How can I get the word…
EricDai
  • 25
  • 3
1 2 3
99
100