3

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.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
  • 1
    fgetc() ? Read char by char? – P.P Aug 24 '13 at 01:28
  • The newline usually is what triggers the system to send to your program whatever input characters have been buffered. It's up to your program whether you act on them immediately or continue reading more input before you do. I'm not sure I understand what you mean by the newline being replaced by a space. – lurker Aug 24 '13 at 01:29
  • Perhaps this link helps you: [Safe scanf and the like...][1] [1]: http://stackoverflow.com/questions/17314796/max-string-length-using-scanf-ansi-c/18388259#18388259 – pablo1977 Aug 24 '13 at 01:31
  • I'm not very familiar with C but so far when I've tried to send it input with a newline in part of the string it terminates the string when it encounters the new line. I'm not sure how to get around that –  Aug 24 '13 at 01:31
  • fgets terminates on a newline, or after it reads a certain amount of characters if it doesn't encounter a newline. You can append a newline to the final string everytime you get input. –  Aug 24 '13 at 01:31
  • ^exactly. That's my problem. I need a way to get the input from console where it doesn't terminate after reading a newline –  Aug 24 '13 at 01:33
  • Right, so keep reading input until you receive some kind of termination signal (i.e., user types 'quit'). –  Aug 24 '13 at 01:34
  • @pablo1977: fyi, to do links in a comment, use the short syntax [link text](link target). Example: [link](http://google.com) – nneonneo Aug 24 '13 at 01:37
  • From the docs I read, it seemed like fgets terminates the first time it encounters a newline. I might be mistaken in its function, but I need a way to process the entire string regardless of whether it contains a newline or not –  Aug 24 '13 at 01:37
  • What I'm getting at is, you can continue to ask for input in a loop until the user terminates. The final string will be built by the series of inputs appended by newlines. –  Aug 24 '13 at 01:38
  • 3
    How do you know when the user has finished typing the input? Is it when you read no more data (EOF)? Or is there some other character that tells you that it is time to stop reading and start processing? Reading until EOF is sometimes called 'slurping' a file (into memory). Since you're reading from a keyboard, in general, you don't have an option to memory map the file (that requires a disk file). When slurping, you'll allocate some space, read into it; if there's more to read, you'll allocate more space for the extra material; rinse and repeat until you're done. – Jonathan Leffler Aug 24 '13 at 02:37
  • @JonathanLeffler Hello young Jon lol. Though I'm satisfied with the accepted answer, it's as always interesting to hear your views on such things and I got the first part of your comment. But I didn't quite understand the second part. That's very new to me, about slurping and rinsing. Any link to start with? – Ardent Coder Jun 15 '20 at 16:37
  • 1
    @ArdentCoder: The "rinse and repeat" part is colloquial English — instructions for things like how to use hair shampoo say (sometimes used to say) "rinse and repeat", encouraging you to use twice as much shampoo. The term 'slurping' is used in Perl (see for example the [`File::Slurp`](https://metacpan.org/pod/File::Slurp) module) to describe reading a complete file into a variable, A Google search on 'file slurp c' comes up with links, including [How to read the content of a file to a string in C?](https://stackoverflow.com/q/174531/15168). You could search 'file slurp python' etc. – Jonathan Leffler Jun 15 '20 at 17:29

4 Answers4

3

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

EhevuTov
  • 20,205
  • 16
  • 66
  • 71
2

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.

0decimal0
  • 3,884
  • 2
  • 24
  • 39
1

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.

Gilbert
  • 3,740
  • 17
  • 19
1

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.

JackCColeman
  • 3,777
  • 1
  • 15
  • 21