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?

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

}

how do i read and assign the data?

argc =?

argv =?

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

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).

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.

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.

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.

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.

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.

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.

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.).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.