sscanf reading a string within a string - c

I have a small problem that I can't seem to fix. Say I have a string,
buffer = "1 1 X ./simple E"
And I want to extract the 2 ints, 2 chars and the filename,
sscanf(buffer, "%d %d %c %s %c, &a, &b, &c, d, &e);
printf("%d %d %c %s %c", a, b, c, d, e);
I don't get back quite what I expect. I get "11 1 X (null)". Any help appreciated.

You are declaring char *d, which will fail because it has no valid place to point. Use an array that has enough space will do:
#include <stdio.h>
#include <string.h>
int main()
{
int a, b;
char c, e;
char d[20];
char buffer[] = "1 1 X ./simple E";
sscanf(buffer, "%d %d %c %s %c", &a, &b, &c, d, &e);
printf("%d %d %c %s %c", a, b, c, d, e);
}
OUtput: 1 1 X ./simple E

c and e can be int's or chars. Note the overflow issues with d[100].
int a, b, c, e;
char d[100];
sscanf(buffer, "%d %d %c %s %c, &a, &b, &c, d, &e);
printf("%d %d %c %s %c", a, b, c, d, e);

#include <cstdio>
#include <cstdlib>
int main() {
char buffer[] = "1 1 X ./simple E", c, d[10], e;
int a, b;
//sscanf(buffer, "%d %d %c %*[./]%s %c", &a, &b, &c, d, &e); //To ignore "./"
sscanf(buffer, "%d %d %c %s %c", &a, &b, &c, d, &e); //Don't ignore "./"
printf("%d %d %c %s %c\n", a, b, c, d, e);
return 0;
}
Output:
1 1 X ./simple E

No blank space delimiter need in the sscanf function argument.
sscanf(buffer, "%d%d%c%s%c", &a, &b, &c, d, &e);
%d is blank separated when when reading buffer, and there should not be a space between %c and %s, cause it swallow the space, leaving the buffer with no separator between char and string.

Related

why cant my printf pick up my user input?

I have this
void main(int argnum, char** input)
{
int a=0, b=0, c=0, d=0, e=0, f=0, g=0, h=0, i=0, j=0, k=0, l=0, m=0, n=0, o=0, p=0;
printf("Enter the numbers from 1 to 16, in any order(separate by comma) : ");
scanf_s("%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d", &a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m, &n, &o, &p);
printf("%d " "%d " "%d " "%d \n", a, b, c, d);
printf("%d " "%d " "%d " "%d \n", e, f, g, h);
printf("%d " "%d " "%d " "%d \n", i, j, k, l);
printf("%d " "%d " "%d " "%d \n", m, n, o, p);
}
my display is
a 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
for some reason my variables other than a, arent being caught.. whats wrong?
(separate by comma)
If you're separating the numbers with commas, the second and subsequent scans will fails, because there's no way to turn a comma into an integer.
Scanning for an %d integer will:
skip white space (which does not include commas); then
scan as many characters as needed to populate an integer, up until the first character that's not part of an integer.
If the first non-whitespace character is a comma, that's not suitable for an integer. Easiest solution is probably just to tell user to separate with spaces rather than commas.
Let's begin with a cut-down version that exhibits the same behaviour (and with single strings rather than those disparate ones):
#include <stdio.h>
int main(void) {
int a=0, b=0;
printf("Enter numbers from 1 to 2 in any order (separate by comma): ");
scanf("%d %d", &a, &b);
printf("%d %d\n", a, b);
}
Entering 1,2 for that gives you 1 0 as per your code. Entering 1 2 (space-separated rather than comma) gives you 1 2, which is what you're after.
Of course, if you need the commas, you can put that into the format string as well:
scanf("%d ,%d", &a, &b);
This will:
%d - skip optional whitespace and read integer;
<space>: skip whitespace before comma;
,: skip comma;
%d - skip optional whitespace and read integer.
Of course, it goes without saying that you should always check things that can fail, if that failure can affect you adversely:
if (scanf("%d %d", &a, &b) != 2) { // expect two things scanned
fprintf(stderr, "Something went horribly wrong!\n");
exit(1);
}
#include<stdio.h>
void main(int argnum, char** input)
{
int a=0, b=0, c=0, d=0, e=0, f=0, g=0, h=0, i=0, j=0, k=0, l=0, m=0, n=0, o=0, p=0;
printf("Enter the numbers from 1 to 16, in any order(separate by comma) : ");
scanf("%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d" "%d", &a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m, &n, &o, &p);
printf("%d " "%d " "%d " "%d \n", a, b, c, d);
printf("%d " "%d " "%d " "%d \n", e, f, g, h);
printf("%d " "%d " "%d " "%d \n", i, j, k, l);
printf("%d " "%d " "%d " "%d \n", m, n, o, p);
}
after taking each value from the keyboard please press enter button it will work with this code

Reading from file does not work as expected with fscanf

I have this file:
0 -> 1:50 2:30 3:10
1 ->
2 -> 0:10 3:20
3 -> 1:20 2:10 3:30
And I want to extract all the numbers from the file with this code:
int a, b, c;
while (fscanf(fp, "%d ->", &a) == 1) {
printf("%d ->", a);
while (fscanf(fp, " %d:%d", &b, &c) == 2) {
printf(" %d:%d", b, c);
}
printf("\n");
}
The idea is that on the first loop it will scan the first number followed by space followed by -> and then the inner loop will scan the following sequence of " %d:%d" until the end of line which will then make the outer loop return 1 because it could successfully read "%d ->".
Ouput of code:
0 -> 1:50 2:30 3:10
It seems like it doesn't work as expected, and the outer while loop exits when it tried to read the number 1 from the second line(but it should?). I just find this weird because I've done something similar recently and it was working fine.
After removing the loops and replacing with this code I get the correct results so it had something to do with my loops:
fscanf(fp, "%d ->", &a);
fscanf(fp, " %d:%d", &b, &c);
fscanf(fp, " %d:%d", &b, &c);
fscanf(fp, " %d:%d", &b, &c);
fscanf(fp, "%d ->", &a);
fscanf(fp, "%d ->", &a);
fscanf(fp, " %d:%d", &b, &c);
fscanf(fp, " %d:%d", &b, &c);
fscanf(fp, "%d ->", &a);
fscanf(fp, " %d:%d", &b, &c);
fscanf(fp, " %d:%d", &b, &c);
fscanf(fp, " %d:%d", &b, &c);
An iteration of while (fscanf(fp, " %d:%d", &b, &c) == 2) { is consuming the "\n1" in "\n1 ->". This fouls the following fscanf(fp, "%d ->", &a), which starts at " ->" and stops the loop.
Code needs to detect the '\n'. format directives/specifiers like " " and "%d" consume end-of-line without reporting that.
Direct solution: As the file is lines of data, then read lines of text with fgets().
Use "%n" to record offset of scan.
// Generous maximum line size
#define LINE_N 1024
char line[LINE_N];
while (fgets(line, sizeof line, fp)) {
int a;
int n = 0;
// v--- space needed to consume trailing whitespace
sscanf(line, "%d -> %n", &a, &n) == 1) {
if (n == 0) {
fprintf(stderr, "Invalid line '%s'\n", line);
continue;
}
printf("%d ->", a);
char *p = line + n;
while (*p) {
int b,c;
n = 0;
// v--- space needed to consume trailing whitespace
sscanf(p, "%d:%d %n", &b, &c, &n);
if (n == 0) {
fprintf(stderr, "Invalid rest of line '%s'\n", p);
break;
}
printf(" %d:%d", b, c);
p += n;
}
printf("\n");
}
The algorithm isn't handling the newlines. So it gets stuck and the end of the first line and isn't able to read anything matching n ->.
One way that works reliably for all C RTL implementations is to use fgets() to read each whole line (including the newline at the end) and then use sscanf() to parse the string.
I think you just need to fseek one char ( newline ) at the last printf.

How do I display a message or respond towards an invalid input in C

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int a, result;
float b;
printf("**This is a simple arithmetic calculator.** \n");
printf("\n Please enter an integer: ");
scanf("%i ", a);
printf("Please enter a floating point number: ");
scanf("%f", b);
result = a + b
printf("Output: ");
printf("%i + %f = %lf \n", a, b, result);
printf("%i - %f = %lf \n", a, b, result);
printf("%i * %f = %lf \n", a, b, result);
}
I need to ensure that your program will not crash if the user enters an invalid input.
scanf is a function that has also got a return value which indicates how many inputs were inserted correctly.
So you can just do something like:
while (scanf("%i ", a) != 1)
{
printf("wrong input, try again");
}
use while loop or if for scanf
while(scanf("%i ", a) !=1){
printf("invalid input.\n");
}
and
while(scanf("%f", b) !=1){
printf("invalid input.\n");
}

C, Reading double values from text file

Hi just getting some weird outputs from trying to read the inputs as double values in C. This issue does not occur when the inputs are integers is there anyway to make it work with double?
#include "stdafx.h"
int main(void)
{
double a, b, c, d, i;
FILE *inp;
inp = fopen("C:\\Users\\student\\Documents\\Visual Studio2012\\Projects\\ConsoleApplication3\\test.txt", "r");
i = fscanf(inp, "%f %f %f %f", &a, &b, &c, &d);
while (i != EOF)
{
printf("a = %f & %d \n", a, i);
printf("b = %f & %d \n", b, i);
printf("c = %f & %d \n", c, i);
printf("d = %f & %d \n", d, i);
printf("%d \n", EOF);
i = fscanf(inp, "%f %f %f %f", &a, &b, &c, &d);
}
fclose(inp);
return 0;
}
Figured it out, the %f in fscanf should be %lf
Change the specifier to %lf in fsacnf and printf statements. Like this -
fscanf(inp,"%lf %lf %lf %lf", &a, &b, &c, &d);
Other problems -
1.Also i is declared as double but in printf you print it with specifier %d ,so you pass wrong argument -
printf("a = %f & %d \n", a, i); // similar in all printf's
So according to me declare i as int and then print it.
2.Also you should always check return of fopen so check it.

How can I insert 2 other value in scanf one time

I have an incorrect number in numB.
How can I insert 2 other value in scanf one time?
Or it can't do it?
#include <stdio.h>
main()
{
char a,b;
int numA,numB;
printf("A : ");
scanf("%c%d",&a,&numA);
printf("B : ");
scanf("%c%d",&b,&numB);
printf("\n\n%d %d",numA,numB);
}
result
A : S 67
B : D 56
67 1684370524
The problem is that the value read into b is the newline character (Enter key) that you pressed after typing in 67. Then reading numB fails because it tries to interpret D as numB.
If you had typed S 67 D 56 <Enter> (i.e. without the Enter in the middle) then you would get the right output.
To fix this, one way is to change your format string to " %c%d". The space means that it will consume any whitespace before trying to read a character.
You get a newline from after the first number in b, and then an undefined value in numB. Use " %c%d" to avoid this problem; it skips white space.
#include <stdio.h>
int main(void)
{
char a, b;
int numA, numB;
printf("A : ");
scanf(" %c%d", &a, &numA);
printf("B : ");
scanf(" %c%d", &b, &numB);
printf("\n%d %d\n", numA, numB);
return 0;
}
Try this out and see if it works:
#include <stdio.h>
void main(){
char a,b,e;
int c,d;
printf("A: ");
scanf("%c %d",&a,&c);
e=getchar();
printf("B: ");
scanf("%c %d",&b,&d);
printf("%d %d",c,d);
}

Resources