line breaks in popen with c ++

2

I am working on a project where I have to execute a command and print the output. Use popen, and it serves, but the output does not show the line breaks, if not, it shows everything as a single line.

My code:

#include <iostream>
#include <string.h>
#include <stdio.h>

int main () {
    FILE *output;
    string sys;
    char line[200];
    output = popen("ls -l", "r");
    while ( !feof(output) ){
        fgets(line, 180, output);
        cout << line;
    }
    return 0;
}

the output it gives me is something like this:

-rw-r--r-- 1 root www-data 0 Sep 6 10:40 WebSS_06_Sep_10:40:19.in -rw-r--r-- 1 root www-data 0 Sep 6 10:40 WebSS_06_Sep_10:40:31.in -rw-r--r-- 1 root www-data 0 Sep 6 10:40 WebSS_06_Sep_10:40:32.in

but I'd like it to look like the console shows normally

-rw-r--r-- 1 root www-data 0 Sep 6 10:40 WebSS_06_Sep_10:40:19.in 
-rw-r--r-- 1 root www-data 0 Sep 6 10:40 WebSS_06_Sep_10:40:31.in 
-rw-r--r-- 1 root www-data 0 Sep 6 10:40 WebSS_06_Sep_10:40:32.in

I was thinking, maybe separate the chain each space and print 8 of those substrings in each line or I do not know if there is an easier way to do it.

    
asked by fabian andres leon perez 06.09.2018 в 18:22
source

1 answer

3

You have labeled the question as C ++ so you should use that language correctly.

Headers <string.h> and <stdio.h> are from not from . These headers have a version adapted to C ++ that has the prefix c and has no extension. If you really need to use the C headers (which will never be the case) you should use the C ++ equivalents <cstring> and <cstdio> . Read this thread to find out why.

I also see that you are using std::cout without preceding the namespace, you may have included in some part of your code the using namespace std clause. Regarding this clause, if you decide to use it, do not do it in the global scope, use it in the smallest possible scope. Read this thread to find out why.

The std::fgets function.

If we consult the documentation of std::fgets (translation and highlighting mine):

  

std::fgets

     

Defined in the header <cstdio>

char* fgets( char* str, int count, std::FILE* stream );
     

Read up to count - 1 characters from the facilitated file data stream and save them in the character formation pointed to by str . The reading ends if the end-of-file is reached or if a new line character is found, in which case str will contain that character .

Since reading data using std::fgets includes the new line character ( \n ) if present, the only explanation is that your source data does not have that character. We can corroborate you by consulting the command ls (translation and highlighting mine):

  

10.1 ls : list contents of the directory

     

[...]

     

By default, the output is sorted alphabetically, according to the active language settings. If the output is to terminal, the output is displayed in columns (vertically ordered) and the control characters are shown as interrogations; otherwise, the output is listed one per line and the control characters are displayed raw .

Since you are not displaying data by console, each output will be displayed on a single line.

Try modifying the call to the ls command to force the output to be by lines, with some parameter .

    
answered by 07.09.2018 в 08:46