-4

I need to write a program that reads in (potentially) multiple lines of text from an input. The program needs to use scanf and printf, and no other library functions ( so no fgets, getchar, or anything else.

I know can use the following to read in one line:

  scanf("%[^\n]s", text);

but that leaves behind a \n , which:

a) needs to be added to the end of the array called text

and

b) stops the rest of the lines of input from getting read in.

Heres the code I have so far

void print_line()
{
  int i = 0;

  while(i < 12)
  {
      char input_text[100];


      /*printf("Input text here: ");*/
      /*scanf("%[^\n]s\n", input_text);*/
     /*EDIT */
      scanf("%99[^\n]", input_text);

      /*Can't use fgets*/
     /*fgets(input_text, 100, input);*/
     printf("Text: %s", input_text);

     i++;
  }  

}

I've run it and it only reads in the first line from the file and repeats it 12 times.

EDIT: Someone suggested I try this:

      scanf(" %99[^\n]", input_text);

which sort of worked, it read in all of the lines of input, but it read them all in as one big line, ignoring both the line breaks and indentation characters (spaces, tabs, etc).

  • 1
    If it were me, knowing what I know today, I would deliberately flunk this assignment. There is nothing -- *nothing!* -- positive to be gained by trying to read lines of input using `scanf`. It is in exercise in pure futility. After completing this exercise, you will at best have learned something you will never need to know, and at worst you may be induced to use your newfound knowledge to write production programs that try to read input using `scanf`, which will do you (and those programs) no good at all. – Steve Summit Sep 18 '17 at 17:57
  • 3
    Seriously: tell your instructor that this is a terrible assignment. – Steve Summit Sep 18 '17 at 17:58
  • "I know can use the following to read in one line: `scanf("%[^\n]s", text);`" --> does not work well when the line is very short or long. – chux - Reinstate Monica Sep 18 '17 at 18:12
  • @SteveSummit Agree with both comments except I have come across 1 corner use for `scanf()` vs. `fgets()`. `if (scanf("%99[^\n]%n", input_text, &n) == 1) { ...` allows code to detect if _null characters_ are read as `n` is the true length. `fgetc()` or `getline()`, of course, are other alternatives. – chux - Reinstate Monica Sep 18 '17 at 18:17
  • 1
    SilentDrew In C, a _line_ of text includes the `'\n'`. "each line consisting of zero or more characters plus a terminating new-line character." C11 7.21.2 2. Specify in the post if `input_text[100];` should include the `'\n'` when the function ends. – chux - Reinstate Monica Sep 18 '17 at 18:26
  • Possible duplicate of [Reading multiple lines of input with scanf()](https://stackoverflow.com/questions/13592875/reading-multiple-lines-of-input-with-scanf) also [this](https://stackoverflow.com/questions/14494309/reading-multiple-lines-of-input-with-scanf) – IP002 Sep 18 '17 at 18:49
  • David Bowling see the code up there? I need to be able to read that in and output it and have the output look exactly the same. Not that it matters though, it turns out I can't use %99[^\n] either. – SilentDrew Sep 18 '17 at 23:00

2 Answers2

0

Please read the manual page for scanf. Remove the s in the format string. Check the return value as weĺl.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

This will try to read a line up to the newline then read the newlines. If result is zero, a newline is in the stream and an if will try to read the newline.

    char input_text[100];
    char newlines[100];
    int result = 0;

    do {
        result = scanf("%99[^\n]%99[\n]", input_text, newlines);//scan line to newline then trailing newline
        if ( result == 0) {
            result = scanf("%99[\n]", newlines);//scan leading newline
        }
        if ( result == 1) {
            printf("Text: %s\n", input_text);//no newline so print line with newline
        }
        if ( result == 2) {
            printf("Text: %s%s", input_text, newlines);//print line and newlines
        }
    } while ( result != EOF);
xing
  • 2,125
  • 2
  • 14
  • 10