user2494770
user2494770

Reputation:

Best way to read stdin that may include newline?

I have to write a program in C that handles the newline as part of a string. I need a way of handling the newline char such that if it is encountered, it doesn't necessarily terminate the input. So far I've been using fgets() but that stops as soon as it reaches a '\n' char. Is there a good function for processing the input from the console that doesn't necessarily end at the newline character?

To clarify:

I need a method that doesn't terminate at the newline char because in this particular exercise when the newline char is encountered it's replaced with a space char.

Upvotes: 4

Views: 12103

Answers (4)

0decimal0
0decimal0

Reputation: 3984

If I understand your question correctly you want to read from the standard input until user has finished typing ( which ain't be a newline of course ). This can be done by setting a flag like EOF while getting input. One way which I came out with is this:

#include <stdio.h>

int main(void)
{

  char ch;
  char str[100];
  int i = 0;

  setbuf (stdout,NULL);

  while ( (ch = getchar()) != EOF)// user can input until the EOF which he or she enters to mark the end of his/her typing or more appropriately input.
    {
      str[i] = ch;// you can store all the input character by character in a char array 
      i++;
    }
  printf ("%s",str);// then you can print it at last as a whole 
  return 0;
}

BEGINNER's NOTE- EOF can vary from system to system so check it and enter the proper EOF for your system.

Upvotes: 2

EhevuTov
EhevuTov

Reputation: 20463

fgets gets a line from a stream. A line is defined as ending with a newline, end-of-file or error, so you don't want that.

You probably want to use fgetc. Here's a code example of a c program file fgetc.c

#include <stdio.h>

int main (void) {
  int c;
  while ((c = fgetc(stdin)) != EOF) fputc(c, stdout);
}

compile like this:

cc fgetc.c -o fgetc

use like this (notice the newline character '\n'):

echo 'Hello, thar!\nOh, hai!' | ./fgetc

or like this:

cat fgetc.c | ./fgetc

Read the fgetc function manual to find out more: man fgetc

Upvotes: 3

JackCColeman
JackCColeman

Reputation: 3807

scanf works when used as directed. Specifically, it treats \n as white space.

Depending on how your application is coded (i.e. how buffers are defined), a \n prompts the system to flush the buffer and feed data into scanf. This should occur as a default without you having to do anything.

So the real question is, what kind of data or characters do you need from the console? In some cases scanf will remove white space and NOT pass blanks, tabs, new lines, into your program. However, scanf can be coded to NOT do this!

Define how data should be entered and I can guide you on how to code scanf.

Upvotes: 1

Gilbert
Gilbert

Reputation: 3776

If you are simply reading blocks of information without the need for scanf(), then fread() may be what you are after. But on consoles you can read to the \n, notice the \n, then continue reading more if you decide that \n is not for you.

Upvotes: 1

Related Questions