0

Anyone knows how to capture the data pipe in from linux ls command in c++ program?

the command is ls - ali and I need to pipe it to a c++ program by ls - ali|./a.out

how do I code the main in c++ to capture the data?

int main()
{
}

what parameters to I need to include?

3
Contributors
11
Replies
12
Views
9 Years
Discussion Span
Last Post by dougy83
0

how do i read and assign the data?

argc =?

argv =?

Edited by Nick Evan: Fixed formatting

0

The first argument (argc) is the number of elements in the array, which is the second argument (argv).

0

any examples of how to use it in my case? ls-ali?

The first argument (argc) is the number of elements in the array, which is the second argument (argv).

0
int main(int argc, char **argv) {
    
}

I have a feeling that's not what he's after. He asked how to pipe data from ls

Anyone knows how to capture the data pipe in from linux ls command in c++ program?

the command is ls - ali and I need to pipe it to a c++ program by ls - ali|./a.out

If you're piping to your c++ program the commandline would be something like: ls -ali ./a.out | ./myCppProgram

You can just read from cin or stdin in your c++ program to read whatever ls spits out.

Edited by Nick Evan: Fixed formatting

0

Hi,

do you have any code example?

I have a feeling that's not what he's after. He asked how to pipe data from ls


If you're piping to your c++ program the commandline would be something like: ls -ali ./a.out | ./myCppProgram

You can just read from cin or stdin in your c++ program to read whatever ls spits out.

0
int main(){
   __int64 node;
   string flags, user, group, month;
   int someNum, size, day;
   
   cin >> node;
   cin >> flags;
   cin >> someNum;
   cin >> user;
   cin >> group;
   cin >> size;
   cin >> month;
   cin >> day;
   // etc
   
   // do something with what you got.
   return 0;
   }

You might instead want to use the getline function with cin.

0

I try testing this code:

int main ()
{
string sdata;

cin >> sdata;

cout << sdata;
}

i compile using g++ test.cpp
run it with ls -ali|./a.out

nothing was printed

what am i missing?

int main(){
   __int64 node;
   string flags, user, group, month;
   int someNum, size, day;
   
   cin >> node;
   cin >> flags;
   cin >> someNum;
   cin >> user;
   cin >> group;
   cin >> size;
   cin >> month;
   cin >> day;
   // etc
   
   // do something with what you got.
   return 0;
   }

You might instead want to use the getline function with cin.

0

Test your program with the keyboard. Does it print out what you type?

I ran your code and it worked (well it showed the first word piped into it) here's the output

Owner@doogie /c/c/documents/code/dev-cpp/Projects/crud
$ ls -ali | ./conio_.exe
total
Owner@doogie /c/c/documents/code/dev-cpp/Projects/crud

you can see that 'total' was printed out. I'm using a linux shell from windows, but I doubt that has any effect on the program function.

0

how do i loop through the output of ls-ali?

since it is not output to a file but directly to the c++ program, i can't open the file.

what else can i use?

Test your program with the keyboard. Does it print out what you type?

I ran your code and it worked (well it showed the first word piped into it) here's the output

Owner@doogie /c/c/documents/code/dev-cpp/Projects/crud
$ ls -ali | ./conio_.exe
total
Owner@doogie /c/c/documents/code/dev-cpp/Projects/crud

you can see that 'total' was printed out. I'm using a linux shell from windows, but I doubt that has any effect on the program function.

0

You can just keep using the >> operator of cin, or getline.

since it is not output to a file but directly to the c++ program, i can't open the file.

Why don't we just pretend it is a file, and use stdin as your file descriptor; e.g. fread(buff, 1, 100, stdin); (or however you use fread, fgets, fscanf, etc.).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.