Trying to store system(command) Output

This is a discussion on Trying to store system(command) Output within the C++ Programming forums, part of the General Programming Boards category; I have had two C++ classes now and have a decent amount of experience, but bear with me still. I ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    Trying to store system(command) Output

    I have had two C++ classes now and have a decent amount of experience, but bear with me still.
    I am trying to write code for a network monitor of sorts, initially in just C++ and then if I can get it to truly work, I'll utilize Visual C++.
    Anyway, the only way that I can find to get current incoming and outgoing transfer information is by using the command line command "netstat -e".

    Which gives:
    <<<
    C:\Documents and Settings\Jim>netstat -e
    Interface Statistics

    Received Sent

    Bytes 153540201 7276711
    Unicast packets 126359 85861
    Non-unicast packets 173281 2197
    Discards 0 0
    Errors 0 0
    Unknown protocols 4887

    >>>
    as DOS output. This command can be called in C++ by using
    system("netstat -e") and the library <process.h>.
    What I truly need is to store the received and sent bytes values from this output, but as of yet I haven't had any luck.
    I have tried to store the output into a string, but only came out with a numerical value of 0, which I've read simply means the command was executed successfully.
    I have also tried to send the output to a text file so that data can be taken from it, but I haven't had luck getting that to work either.

    If anyone has any idea as to an easier way of getting network information, or of a way that information can be stored from a system output, it would be greatly appreciated.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    You could use system("netstat -e > output.txt") and then read in output.txt.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    How about redirecting the output of netstat to a temporary file and then opening up that file to read from it?

    Code:
    system("netstat -e > temp.out")
    ifstream fin("temp.out");
    edit: Damn, I'm beaten!

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Ha...apparently neither one of us read the whole thing.
    Quote Originally Posted by punxworm
    I have also tried to send the output to a text file so that data can be taken from it, but I haven't had luck getting that to work either.
    However, it looks like it works to me.
    Code:
    int main()
    {
    	system("netstat -e > output.txt");
    	std::ifstream infile("output.txt");
    	std::string buffer;
    	while(getline(infile,buffer))
    	{
    		std::cout << buffer << std::endl;
    	}
    	return 0;
    }
    I'm not too sure about my loop condition though. Is that while loop okay?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Yeah, getline will return false if an error or EOF is encountered. Actually, I think that's one of the best ways to get input from a file.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    2
    awesome thanks for the help guys. Now i can get my hands on that data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  2. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 01:27 PM
  3. store data from ifstream and store in link list
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2006, 08:50 AM
  4. Do you store store one off data arrays in a class?
    By blood.angel in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2002, 12:05 PM
  5. how to store a node on hard disk
    By ALLRIGHT in forum C Programming
    Replies: 3
    Last Post: 05-13-2002, 10:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21