1
votes
#include <stdio.h>
#include <stdlib.h>

int main(){

    char str[20];
    int number;
    while(2 == scanf("%s  %*d %*s  %*s    %d %*d %*s %*s %*s", str, &number)){
        printf("%s\n",str);
        printf("%d\n", number);

    }
}

I am trying to read multiple lines from standard input all of which have the same format. My current implementation works but I can't seem to figure out anyway to terminate the input once the user does not input a line and then presses enter. The program keeps expecting input from the user. Is there anyway to do this using scanf and with my current implementation?

NB: I am running commands like:

ls -l | my_program

and that is not giving me any output.

1
What's the input?Jonathan Leffler
You should indicate EOF by typing Control-D on a Unix-like system or Control-Z on a Windows system. The %s will skip leading white space, including newlines, waiting for non-blank characters. Only EOF will make it stop. If you want to do line-based input and stop on an empty or all blank line, use fgets() to read the line and sscanf() to parse it. Note that on Unix, Control-Z suspends rather than terminates the process.Jonathan Leffler
For example -ab--c-- 7 Yes No 1578Dean
I only need to extract the -ab--c-- and 1578Dean
Welcome to Stack Overflow. Please (re)read the How to Ask page, and note the guidelines for creating an MCVE (minimal reproducible example). It shouldn't have been so hard to find out that you're running ls -l | ./your_program; that should have been part of the initial question. Once the salient information is available, the problem is immediately resolvable. It is important to state restrictions like "must use scanf() and cannot use other input functions" up front. People will suggest the best way to do things unless they are told that you don't have any option about using it. Please remember for next time.Jonathan Leffler

1 Answers

2
votes

About the simplest modification using only scanf() to read the output from ls -l command is this:

#include <stdio.h>

int main(void)
{
    char str[20];
    int number;
    if (scanf("%*[^\n]") != 0)
        return 1;
    while (2 == scanf("%s %*d %*s %*s %d %*[^\n]", str, &number))
        printf("%s  %d\n", str, number);
    return 0;
}

The first scanf() read up to but not including the newline; that skips the total NNN line that ls -l outputs. The second scanf() is almost the same as yours except that it skips the data after the second number (the file size) up to the newline. Fortunately, the %s at the start skips leading white space, including newlines, so the fact that the scan sets %*[^\n] leave the newline behind doesn't matter in the slightest.

My directory yields:

$ ls -l
total 152
-rw-r--r--    1 jleffler  staff  22072 Dec 30 09:19 LICENSE.md
-rw-r--r--    1 jleffler  staff   2694 Dec 30 09:19 README.md
dr-xr-xr-x    4 jleffler  staff    128 Aug 14  2016 Safe
drwxr-xr-x   84 jleffler  staff   2688 Jan 12 00:58 Untracked
drwxr-xr-x   26 jleffler  staff    832 Dec 25 22:39 bin
-rw-r--r--    1 jleffler  staff   1875 Jan 19 00:08 crseq71.sql
drwxr-xr-x   14 jleffler  staff    448 Dec 30 09:19 doc
drwxr-xr-x   10 jleffler  staff    320 Jan 12 01:13 etc
-rw-r--r--    1 jleffler  staff    173 Mar  3  2017 get.jl.activity
drwxr-xr-x   21 jleffler  staff    672 Jan  7 23:02 inc
drwxr-xr-x    5 jleffler  staff    160 May 28  2017 lib
-rw-r--r--    1 jleffler  staff    390 Jun 21  2017 makefile
drwxr-xr-x    4 jleffler  staff    128 Jan 12 01:13 packages
-rw-r--r--    1 jleffler  staff    218 Oct 15 10:18 pending.20171015.101828
-rwxr-xr-x    1 jleffler  staff   8704 Jan 19 21:39 rl43
-rw-r--r--    1 jleffler  staff    248 Jan 19 21:39 rl43.c
drwxr-xr-x    3 jleffler  staff     96 Jan 19 21:21 rl43.dSYM
-rw-r--r--    1 jleffler  staff   2247 Jan  6 22:44 sll43.c
-rw-r--r--    1 jleffler  staff    126 Oct 24 12:52 so-4689-5145.info
drwxr-xr-x  227 jleffler  staff   7264 Jan 19 11:34 src
-rw-r--r--    1 jleffler  staff     92 Jan 19 21:20 testfile.txt
-rw-r--r--    1 jleffler  staff    645 Jan 18 23:37 union71.c
$ ls -l | ./rl43
-rw-r--r--  22072
-rw-r--r--  2694
dr-xr-xr-x  128
drwxr-xr-x  2688
drwxr-xr-x  832
-rw-r--r--  1875
drwxr-xr-x  448
drwxr-xr-x  320
-rw-r--r--  173
drwxr-xr-x  672
drwxr-xr-x  160
-rw-r--r--  390
drwxr-xr-x  128
-rw-r--r--  218
-rwxr-xr-x  8704
-rw-r--r--  248
drwxr-xr-x  96
-rw-r--r--  2247
-rw-r--r--  126
drwxr-xr-x  7264
-rw-r--r--  92
-rw-r--r--  645
$