1

I'm writing a program for project in C, where I have such kind of input:

  ............xcx............
  .........qeztodlea.........
  .......ecnedivorpuzy.......
  .....bqfjwxqindnrsatrs.....
  ....ucaamadisonoctoieax....
  ...ozkttqdxwltstaivcilex...
  ...ujknnakigzfasxninltxc...
  ..rabxaa...kohce...oelnyd..
  ..rithls...momrl...spayvh..

      honolulu
  oklahomacity
  charleston
  madison
  montgomery
  saltlakecity
  springfield

First set of data is separated from second data set by empty line, I need on one Enter press process it.

If I copy-past this data in terminal window and press Enter and then Ctr+D ( which means end of input ) it works fine, but if to press only Enter I still need to enter data. I can't understand what to change so only on first Enter I'll finish input and proceed to my program? I know that this question sounds stupid, but in my function for reading line I use fgetc, because I need to check some letters, if to use e.g. fgets then it will stop on first nl, which function to use? Maybe I don't get something, is it possible in general?

I already have rLine function for reading line ( using fgetc ):

char * rLine( int * length, int * ha ){
   char *buff = malloc( LMAX ), *old = buff;
   int count = 0, maxlen = LMAX, len = maxlen, c;

    while ( (c = fgetc( stdin ) ) != '\n' ){

       if ( c == EOF ) { *ha = R_EOF; break; }

       if ( /* some conditions for c */ ) *ha = R_FALSE;

    *buff ++ = c;
     count++;

     if ( -- len == 0 ){  
       len = maxlen;
       buff = (char *)realloc( old, maxlen *= 2 );
       old = buff;
       buff += count;
   }
 }
 *length = count;
 *buff = '\0';
 return old;
}

, where ha some kind of error-message handler. Tnx

NOTE: OK, I've found out that end of input is driven same as CTRL + D combination. so actually the check if ( c == EOF ) ( or c == '\0' ) works fine for me. So actually the question can be closed by now.

NGix
  • 2,542
  • 8
  • 28
  • 39
  • Why don't you just use `fgets`, then check later whether the line you read contains the characters you want to handle specially? `strchr` would seem like the right tool for that. – Fred Foo Nov 23 '12 at 00:17
  • but `fgets` will stop on first new line? how to verify if enter is pressed and it's an end of input? – NGix Nov 23 '12 at 00:17
  • 2
    Check whether `fgets` has read an empty line. `fgets(ln, size, file)` then check whether `ln[0] == '\n'`. – Fred Foo Nov 23 '12 at 00:21

1 Answers1

1

Are you familiar with '\n' for a new line and '\r\n' for carriage return?

add this line and handle the new line case:

  if ( c == '\n' ) { // that is a new line }

Have you seen this post:

How to read a line from the console in C?

Community
  • 1
  • 1
0x90
  • 39,472
  • 36
  • 165
  • 245
  • That isnt completely correct - http://stackoverflow.com/questions/7013034/does-windows-carriage-return-r-n-consist-of-two-characters-or-one-character – asheeshr Nov 23 '12 at 01:39
  • @AshRj no matter what the line will break with \n. If you think it is not clear in my answer let me know please. thanks – 0x90 Nov 23 '12 at 08:18
  • 1
    "Are you familiar with '\n' for a new line and '\r\n' for carriage return" I was referring to this. **Linux uses /n for carriage return.** – asheeshr Nov 23 '12 at 08:21
  • /n ?? where did you see it? – 0x90 Nov 23 '12 at 08:25
  • 1
    I am typing this from a Linux machine. Carriage return is ascii 10 -> newline. – asheeshr Nov 23 '12 at 09:09
  • Helloo, if you read my POST I already have this line in `rLine` function - I break loop if `while ( (c = fgetc( stdin ) ) != '\n' )`. Actually the problem is solved and question can be closed, see NOTE in post – NGix Nov 24 '12 at 01:26
  • Should be double quotes around the newline. "\n" – squiguy Nov 24 '12 at 01:46
  • 2
    @squiguy: I don't want to be blunt here and I know that your comment is 4 years old at the time of this writing, but `'\n'` **is not** supposed to be quoted with double quotes! In `C` `'\n'` and `"\n"` are two different things. `'\n'` is a `char` and `"\n"` is a `char *`, `char array`, etc. Furthermore, the separate types cannot be compared with `==` or `!=`. – J. Allan Sep 06 '16 at 00:52