0

I’m new at C language and I need help. Can anybody say how to know the length of the scanned string? I have a structure

typedef personand
{
char FirstName[20];
chat LastName [20];
}

and I need to check if user inputs not to long char into char FirstName[20] with scanf. Thank you.

lurker
  • 56,987
  • 9
  • 69
  • 103
NewAtC
  • 55
  • 9
  • 1
    Always use a read-buffer of sufficient size. E.g `char line[1024];` will do.. Then if reading input one at a time `while (fgets (line, sizeof line, fileptr)) { size_t len = strlen (line); if (len + 1 < 20) { /* ok to copy to struct */ }` If both in one line, then `while (fgets (line, sizeof line, fileptr)) { if (sscanf (line, "%19s %19s", structname.FirstName, structname.SecondName) == 2) { /* good input */ }}` – David C. Rankin Dec 14 '20 at 00:20
  • 2
    Never read input with `scanf()` It is so full of pitfalls for the new C programmer fully 1 in 10 C input questions on this site relate to its ***misuse***. Please read the [About](http://stackoverflow.com/tour) page soon and also visit the links describing [How to Ask a Question](http://stackoverflow.com/questions/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) (MCVE). Providing the necessary details, including your MCVE, compiler warnings and associated errors, if any, will allow everyone here to help you with your question. – David C. Rankin Dec 14 '20 at 00:22
  • Also in the 1st example it's simply `if (len < 20)`, no `+1` needed in that comparison. – David C. Rankin Dec 14 '20 at 00:26
  • @DavidC.Rankin Finally! Someone [other than me] who thinks `scanf` is a bad idea. Some have advocated for `fgets`, `strtok`, `strtol`, etc. But, I've seen [way too] many comments/answers that show how to torture `scanf` into doing what `fgets` et. al. can do more easily. I once got a vendor's program to program an FPGA with microcode. It used `scanf("%c",&mybyte)` to read the microcode. This was so slow that the hardware would frequently timeout waiting for the data. When I replaced this with `fread` [for the entire amount], it reduced loading time from 20 mins to 90 secs. – Craig Estey Dec 14 '20 at 01:04
  • @CraigEstey - after watching for the better part of the past 7 years, I've concluded the prevalent misuse of `scanf()` by virtually ever new C programmer is the result of the sheer number of poor (to bad) introductory C tutorials available. There are virtually no tutorials out there explaining the proper way to handle user input. So virtually all new C programmers think `scanf()` is the proper user-input function to use in every case. All we have to do is type `"[c] scanf"` into the SO search box to see the result `:)` – David C. Rankin Dec 14 '20 at 01:45

1 Answers1

1

Check the documentation of your compiler, scanf may be different depending of the operational system.

You can set the size before the s:

scanf("%19s", firstName);

This will truncate the value. You can get one byte extra to validate if the content has overwflowed.

  • You get the nod, not for suggesting `scanf()`, but for using the *field-width* modifier to limit the number of characters read to `19` (`+1` for the `'\0'`). Without a *field-width* modifier, `scanf ("%s", firstName);` is no safer than `gets(firstName);` If you look at `man 3 gets`, the first line of the Description is **Never use this function.**. Explained further in [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/q/1694036/3422102) – David C. Rankin Dec 14 '20 at 01:50