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).