0

I am not allowed to call any function other in a C library other than scanf, printf, and feof. When I try to use the code below, it only reads a file up to space char. Is there a way to read a whole line without using "%[^\n]"? My end goal is to read the entire file using feof file but first, I need to know how to read a line using scanf. Is it possible?

#include <stdio.h>
int main(){

  char ar[50];

  scanf("%s", ar);
  printf("%s", ar);

}
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
mico
  • 9
  • 2
  • Why don't you want to use `"%[^\n]"`? – Daniel Walker Sep 23 '20 at 01:22
  • 1
    The only way to get `scanf` to read a whole line at a time is with `"%[^\n]"`. At the other extreme, you can read one character at time with `scanf("%c", &input_char)`. In that case, it's basically the same as calling `getchar`. – user3386109 Sep 23 '20 at 01:27
  • 2
    BTW, [don't use feof](https://stackoverflow.com/questions/5431941). The return value from `scanf` tells you when you've reached the end-of-file. `feof` doesn't provide any information that `scanf` doesn't already provide. – user3386109 Sep 23 '20 at 01:30
  • 4
    The correct way to read a file a line at a time in standard C is with `fgets`. Your instructor is wrong to forbid you to use `fgets`, and you may tell them I said so. Moreover, you may tell them I said [`scanf` should never be used at all](https://stackoverflow.com/questions/15664664/scanf-regex-c/15664816#15664816). – zwol Sep 23 '20 at 01:30
  • how can I make a loop to read one line at a time without fgets? – mico Sep 23 '20 at 02:10
  • @user3386109 `"%[^\n]"` fails to read a whole line aside form not reading the `'\n'`, is reads/saves nothing when the line is only `"\n"`. Lacking a width limit, it is bad like `gets()`, and worse. – chux - Reinstate Monica Sep 23 '20 at 02:36
  • @chux I didn't say it was a good idea, only that it's the only way to read a whole line with one call to `scanf`. The OP had already said that using `"%[^\n]"` was not an option, so I didn't bother with adding all the fine print, caveats, and dire warnings. – user3386109 Sep 23 '20 at 07:48
  • Detail: " only way to read a whole line with one call to scanf" --> STL has "... each line consisting of zero or more characters plus a terminating new-line character.". `"%[^\n]"` does not read a whole line, just most of it as it fails to read the last character of the line. `"%[^\n]%*c"` reads a whole line, except a line like `"\n"`. `scanf()` lacks a single format that reads all lines. – chux - Reinstate Monica Sep 23 '20 at 12:44

1 Answers1

1

Read one character at a time.

#include <stdio.h>
int main(void) {
  char ch;
  while (scanf("%c", &ch) == 1) {
    printf("%c", ch);
  }
}

P. S. Don't ever do this for production code and never admit your did this on a résumé.

Follow @zwol advice.


"My end goal is to read the entire file using feof file " --> Why is “while ( !feof (file) )” always wrong?.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256