Reading line by line Stringstream

I just want to get each line back from the command line.
So i copy the output into the stringstream.
But then i need to convert each line into a string to search through for a certain sequence of characters
Ive included some of the code below to give an idea of what i am trying to achieve

1
2
3
4
5
6
    using namespace std;
    stringstream ss;
    string File = "regina C:\\Users\\N\\Tester";
    std::cout << system( File.c_str() );
    File = "C:\\Users\\N\\FTPscript.bat C:\\Users\\N\\PUBSKEL";
    ss << system( File.c_str() );


If you need any more info just ask

Thanks in advance for any help :)
I just want to get each line back from the command line.

You can't do that directly via system.

system returns an int.
Lines 4 and 6 print insert the int that system returns into their respective output streams.
Last edited on
A simple approach would be: redirect the output to a file, and then read the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cstdlib>
#include <fstream>

int main()
{
    const std::string temp_file_path = "/tmp/temp.txt" ;
    const std::string cmd = "clang++ --version" ;
    
    const std::string cmd_with_stdout_to_file = cmd + " > " + temp_file_path ;  
    int status = std::system( cmd_with_stdout_to_file.c_str() ) ;

    if( status == 0 ) std::cout << std::ifstream(temp_file_path).rdbuf() ;
}

http://coliru.stacked-crooked.com/a/3b018c66e185b1d5
Hey thanks for the help guys

@cire
I dont think it does return an int, and if it does then something else must be happening behind the scenes because
std::cout << system( File.c_str() );
definitely prints the output from tester to screen.
The main problem i had with the one where i put it into the stringstream was that if i converted to a string, it would display everything instead of the line with the word i was looking for.

@JLBorges
I thought about doing it this way but im ending up doing this a lot and having to create and delete a lot of files.
Is there no way of just exracting individual lines from a stream and putting them into seperate strings ?

Thanks again for helping :)

EDIT: Okay so after some testing it does return an int
But it also outputs everything else to the command prompt/console window
So it must somehow return that also ?


EDIT2: Ive got it working now but i had to create the file holding the information in the batch script which i didnt really like doing because it means i will have to delete the file in my C++ program and im pretty sure thats not good practice so if anyone has any suggestion i would appreciate any help :)
Last edited on
> I thought about doing it this way but im ending up doing this a lot
> and having to create and delete a lot of files.

We can append to the same file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>
#include <fstream>

int system_to_file( const std::string& command, const std::string& file_name )
{ return std::system( ( command + " > " + file_name ).c_str() ) ; }

int system_append_to_file( const std::string& command, const std::string& file_name )
{ return std::system( ( command + " >> " + file_name ).c_str() ) ; }

int main()
{
    static const std::string temp_file = "/tmp/temp.txt" ;
    const std::string cmd = "clang++ --version" ;
    
    struct delete_file { ~delete_file() { std::remove( temp_file.c_str() ) ; } } do_delete_file ;
    
    system_to_file( cmd, temp_file ) ;
    system_append_to_file( "echo '\n------------ uname --------------\n'", temp_file ) ;
    system_append_to_file( "uname -a", temp_file ) ;

    std::cout << std::ifstream(temp_file).rdbuf() ;
}


> Is there no way of just exracting individual lines from a stream
> and putting them into seperate strings ?


Yes, there is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include <fstream>
#include <vector>

std::vector<std::string> extract_lines( const std::string& file_name )
{
    std::vector<std::string> extracted_lines ;

    std::ifstream file(file_name) ;
    std::string line ;
    while( std::getline( file, line ) ) extracted_lines.push_back(line) ;

    return extracted_lines ;
}
Topic archived. No new replies allowed.