?
Solved

opening another console at run time using system()

Posted on 2002-07-25
6
Medium Priority
?
173 Views
Last Modified: 2012-05-04
Hi I have two questions regarding system call in Unix in C language.

1. What function will I use to be able to open another console by using a system() in C? The scenario is to call an executable file inside the code and that executable will open another console(this is aside from the existing console used at run time).

2. The code is like this :

    /* line 1 : */ char w_temp[100] ;
    /* line 2 : */ strcpy(w_temp,"ls -l tempdir " ) ;
    /* line 3 : */ system(w_temp) ;

    NOTE : tempdir is not an existing directory.

  By executing these lines, the compiler execute   automatically the system call "ls -l tempdir". My aim is not to write "No such file or directory" to the console even if the file/directory is not existing. In this case even if I catch the return of line 3 I cannot control its automatic writing of the result of my command ("ls -l tempdir"). My question is, is there a way for me to control the result of the system call before it writes to the console? Like in this case, I would want the code not to write to the console the result of the string I passed( "ls -l tempdir" ). The string is a parameter for my function.

That's all thank you!
0
Comment
Question by:keziah0815
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 7179463
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char w_temp[100] ;
FILE *ptr;
strcpy(w_temp,"ls -l tempdir 2>&1" ) ;
if( ptr = popen(w_temp,"r") ){
    char buf[BUFSIZ];
    while( fgets(buf, BUFSIZ, ptr) ){
       (void) printf("%s", buf);
    }

}
0
 

Author Comment

by:keziah0815
ID: 7179482
The code you gave still prints "No such file or directory" on the console. I would like not to write "No such file or directory" on the console.
0
 
LVL 2

Expert Comment

by:jonnin
ID: 7179828
Can you redirect the output and remove the offending line?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Accepted Solution

by:
MFCRich earned 1200 total points
ID: 7179884
The offending line is printed to stderr. You could redirect this to /dev/null before the popen() call from ozo's comment and restore it when finished.

As for opening a console, you could fork() and exec() an xterm. Would that accomplish what you want?
0
 
LVL 84

Expert Comment

by:ozo
ID: 7181599
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char w_temp[100] ;
FILE *ptr;
strcpy(w_temp,"ls -l tempdir 2>&1" ) ;
if( ptr = popen(w_temp,"r") ){
   char buf[BUFSIZ];
   while( fgets(buf, BUFSIZ, ptr) ){
      /*(void) printf("%s", buf);*/
   }
}
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 9480608
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:
Accept MFCRich's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Kent (Kdo)
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question