简体   繁体   中英

c: scanf reading integers

This scanf statement will read in numbers from input like 10ss as 10 - how do I rewrite it to ignore an int that's followed by other characters? If 10ss is read, it should ignore it and stop the for loop.

for (int i = 0; scanf("%d", &val)==1; i++)

You can try reading the next character and analyzing it. Something along the lines of

int n;
char c;
for (int i = 0; 
     (n = scanf("%d%c", &val, &c)) == 1 || (n == 2 && isspace(c)); 
     i++)
  ...

// Include into the test whatever characters you see as acceptable after a number 

or, in an attempt to create something "fancier"

for (int i = 0; 
     scanf("%d%1[^ \n\t]", &val, (char[2]) { 0 }) == 1; 
     i++)
  ...

// Include into the `[^...]` set whatever characters you see as acceptable after a number 

But in general you will probably run into limitations of scanf rather quickly. It is a better idea to read your input as a string and parse/analyze it afterwards.

how do I rewrite it to ignore an int that's followed by other characters?

The robust approach is to read a line with fgets() and then parse it.

// Returns
//   1 on success
//   0 on a line that fails
//   EOF when end-of-file or input error occurs.
int read_int(int *val) {
  char buf[80];
  if (fgets(buf, sizeof buf, stdin) == NULL) {
    return EOF;
  }
  // Use sscanf or strtol to parse.
  // sscanf is simpler, yet strtol has well defined overflow funcitonaitly
  char sentinel;  // Place to store trailing junk
  return sscanf(buf, "%d %c", val, &sentinel) == 1;
}


for (int i = 0; read_int(&val) == 1; i++)

Additional code needed to detect excessively long lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM