473,473 Members | 2,286 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading outputs from the commandline

3 New Member
I'm using Dev C++ and trying to read the output/errors for system commands back into a variables (for logging and other tasks later on). At the moment I have:

Expand|Select|Wrap|Line Numbers
  1.  
  2.      char chBuffer4[]= "net stop mailservice";
  3.      char chBuffer5[128];
  4.      int lastChar;   
  5.      FILE *fp; 
  6.  
  7.      fp = popen(chBuffer4, "r");
  8.      lastChar = fread(chBuffer5, 1, 128, fp);
  9.      chBuffer5[lastChar] = '\0'; 
  10.      printf ( chBuffer5 );
  11.      fclose(fp);
  12.  
The problem is I either get back nothing, or I get back .εÉ|└@æ| ╗@æ|Ä╓☺... which despite looking very neat... isn't very useful.

I've been working on this for awhile now, and I am slightly baffled as this formate of code usually works on *nix servers, but it won't work on windows.

Any idea's? Suggestions?
May 16 '07 #1
3 1139
weaknessforcats
9,208 Recognized Expert Moderator Expert
What is popen???

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.      //char chBuffer4[]= "net stop mailservice";
  4.     char chBuffer4[]= "net stop mailservice.txt";
  5.      char chBuffer5[128];
  6.      int lastChar;   
  7.      FILE *fp; 
  8.  
  9.      //fp = popen(chBuffer4, "r");
  10.      fp =fopen(chBuffer4, "r");
  11.      if (!fp)
  12.      {
  13.          printf("no such file\n");
  14.          return 1;
  15.      }
  16.      lastChar = fread(chBuffer5, 1, 128, fp);
  17.      chBuffer5[lastChar] = '\0'; 
  18.      printf ( chBuffer5 );
  19.      fclose(fp);
  20.  
  21. }
  22.  
I changed your popen to fopen and added a test for the FILE* to be non-null befoire I use it. Also, changed the files name. Works fine on Windows XP. Compiled with Visual Studio.NET 2005.
May 16 '07 #2
T3kk
3 New Member
Its a pipe... I'm not trying to read a file at all. Its the best way I've found so far to pass a "system()" like command and still be able to interact with the output afterwards

http://www.opengroup.org/onlinepubs/007908799/xsh/popen.html Thats popen
May 16 '07 #3
T3kk
3 New Member
Oddly enough my code DOES work... it just doesn't display properly if the service DOESN'T exist... odd odd odd... looks like I'll have to work in a way to check for services now
May 18 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Etienne Labuschagne | last post by:
Thanks for the responses. I will look into using PySerial for talking to the modem as suggested by Peter. The code example I gave is probably not the best way to do it, but it seemed to work ok...
4
by: nick | last post by:
Hi all: In the winform i want to run commandline with return value(string), then display this string in winform textbox. anyone has idea about that? Thanks Nick
1
by: Rvo | last post by:
I'm writing an application that should work both as a GUI and a commandline program. When running from commandline I want to give all output to the commandline instead of showing it it an own...
5
by: jcrouse | last post by:
I have the following code: Dim MyStartupArguments() As String MyStartupArguments = System.Environment.GetCommandLineArgs UBound(MyStartupArguments) RomName =...
2
by: Christoph Borger | last post by:
Hello! I have wrote a windows service in vb.net. This service monitors the running processes with WMI and the Win32_Process class. Till last month all seems ok. But since the begin of september...
7
by: tiger786 | last post by:
I am new to programming and studying basics in college .Can somebody write a program on this? The program should monitor a possibly infinite stream of characters from the keyboard (standard...
0
by: axlq | last post by:
While trying to learn the ins and outs of the php CURL library, I decided to write a php script that posts a form on the Chicago Board of Options (CBOE) web site, which returns an ASCII text file. ...
1
by: RSH | last post by:
Hi, I have a silly question... I have a Windows Form project (VB.Net) that was created in Visual Studio 2003. The project runs great. Now I have to add the ability to run the same...
1
by: edmondsnake | last post by:
Dear all, I would like to ask questions about my VB.net program. The executable file of the program is called "PSP.exe" Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As...
2
by: andreas.huesgen | last post by:
Hello, I am writing a commandline tool in Python which is often feed with lots of commandline arguments. In practice, the commandline already reached a critical length which is to long for Windows...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.