My C code which runs on Ubuntu has line
system("ls -l | wc > temp.txt");
I want to make it work on windows so that It has to be OS Independent.How can I do that.
Can any one help me?
I would guess that the particular code shown is probably going to get the first value from the "temp.txt" file at some point and use it as a count of files (actually number of files plus one)
Instead of that you could use C code like this
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main() {
DIR *cwd;
int c=1; /* like +1 */
struct dirent *d;
if ((cwd=opendir(".")) ) {
while((d=readdir(cwd))) {
if (*(d->d_name) != '.') c++; /* ignore dot files */
}
} else {
perror("opendir fail");
return(1);
}
printf("the first number in temp.txt would be %d", c);
return(0);
}
Whatever the system() call result is doing, this is my answer: rewrite it in C, which you have working on both systems
Related
I am in a Unix environment (Sun Ultra 5 with gcc v.2.95.3) and I am trying to understand where my compiler writes temporary files using the tmpnam function. I am aware that this works is deprecated but mine was only curiosity.
I acted like this:
I ran this source in c:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char nomefile[L_tmpnam];
strcpy(nomefile, "my_file.txt");
tmpnam(nomefile);
getchar();
return(0);
}
while waiting for the return key to be pressed, the program should create a temporary file (somewhere in the file system) named my_file.txt. So from another terminal I start the search in this way (as superuser):
find / -name my_file.txt -print
unfortunately it does not find it. Yet in the book I'm reading it says that this function (tmpnam) unlike the tmpfile function, you can specify the name of the temporary file and write it somewhere in the filesystem.
The correct source for identifying where temporary files are written is the following. As was suggested to me in the comments.
#include <stdio.h>
int main(void) {
char buffer[L_tmpnam] = "my_file.txt";
char *ptr;
tmpnam(buffer);
printf("Temporary name 1: %s\n", buffer);
ptr = tmpnam(NULL);
printf("Temporary name 2: %s\n", ptr);
return(0);
}
Output:
Temporary name 1: /var/tmp/aaagXay5b
Temporary name 2: /var/tmp/baahXay5b
I have two files in the same directory.
directory/
| a.c
| b.c
a.c
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
pid_t pid;
int status;
int wret;
if ((pid = fork()) < 0)
printf("error");
else if(pid == 0)
{
printf("%s", argv[1]);
execv(argv[1], &argv[1]);
}
else
{
/* respawn */
if ((wret = wait(&status)) != -1)
execv(argv[1], &argv[1]);
}
return 0;
}
b.c is just a simple program that print "hello".
I want to run ./a b from the command line to make the a program call exexXX to execute the b program.
I don't understand why if I use execv I can write just ./a b in the command line, instead if I use execvp I have to write ./a ./b.
The man exec page is not clear because it reports
"The initial argument for these functions is the name of a file that
is to be executed."
Thanks
If the program name argument contains no slashes, the execvp() function looks for the program to execute in the directories listed on your PATH environment variable. If you don't have . (the current directory) on your PATH and you aren't in one of the directories listed on your path, a plain name like b will not be executed, even if b is in the current directory. If the name contains a slash, it can be relative (./b) or absolute (/home/someone/src/programs/b) and it will be interpreted as a file name to be executed without consulting the PATH environment variable.
By contrast, execv() treats a plain b in the program name argument as ./b — the name of the file in the current directory and executes it if it is present, and fails if it is located somewhere else.
At one time, there was a comment that asked:
Are you saying if you have an executable b in . and you do execv("b", b_args), it will get executed?
On a normal Unix box, yes.
Code b.c:
#include <stdio.h>
int main(void)
{
puts("Hello");
return 0;
}
Code a.c:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
char *argv[] = { "b", 0 };
execv(argv[0], argv);
fprintf(stderr, "failed to execute '%s'\n", argv[0]);
return 1;
}
Running these:
$ (PATH=$(clnpath "$PATH" ".:$PWD"); echopath PATH; ./a)
/Users/jleffler/bin
/opt/informix/12.10.FC6/bin
/Users/jleffler/oss/bin
/Users/jleffler/oss/rcs/bin
/usr/local/mysql/bin
/opt/gcc/v7.3.0/bin
/Users/jleffler/perl/v5.24.0/bin
/usr/local/bin
/usr/bin
/bin
/opt/gnu/bin
/usr/sbin
/sbin
Hello
$
The clnpath script modifies the string provided as its first argument ("$PATH") by removing any occurrences of any of the directory names listed in its second path-like argument (".:$PWD") — it's how I edit my PATH on the fly when I need to. The echopath script echoes the directories on PATH (or any other path-like variable, or it will process the result of expanding a pathlike variable, such as "$PATH"), one per line — the output shows that neither . nor /Users/jleffler/soq (which is where I run the program) is on $PATH in the sub-shell. The ./a runs the code from a.c (it would not be executed without that ./ in front), which in turn runs the code from b.c, which produces the Hello. (If there is some system where this does not work, please identify it.)
I could also arrange for b.c to be:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
puts("Hello");
const char *env = "PATH";
char *val = getenv(env);
if (val == 0)
val = "<nothing>";
printf("%s=%s\n", env, val);
return 0;
}
which would print the value of $PATH directly from the executable (to verify that neither . nor the value of the current working directory is listed).
Please find below the code and the output which I am getting.
My C code is in c:/turboc3/bin directory
and my output macid.txt is in c:/turboc3/disk.
Here is the code which I am compiling
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main ()
{
//char mac[200];
FILE *fp;
clrscr();
// fp=fopen("c:\macid.txt","w");
system("GETMAC>c:/macid.txt");
fp=fopen("c:/macid.txt","r");
if(fp!=NULL)
{
char line[128];
while(fgets(line,sizeof line,fp)!=NULL)
{
char *nwln=strchr(line,'\n');
char *ptr;
if(nwln!=NULL)
*nwln='\0';
ptr=strstr(line,"Physical Address");
if(ptr!=NULL)
{
printf("%s\n",ptr);
break;
}
}
}
getch();
return 0;
}
The output is:
Illegal command: GETMAC.
Can anyone guide me through this?
Illegal command: GETMAC implies, that it tries to run it but can't.
First, things to fix : Remember to escape \ inside string. Do not use / as path separator.
Then, Things to try, in approximate order: Try with full path to GETMAC. Try without redirection. Try with different program. Wrap GETMAC into bat file, which does the redirection.
I am not sure how your file macid.txt resides in c:/turboc3/disk, but - I think you can solve this problem by giving a full path to the system() function call.
LIke this:
system("c:/windows/system32/getmac.exe > macid.txt");
Today , When i coding, met a question..my Code as follow:
#include<stdlib.h>
void main()
{
system("dir");
getch();
}
The question : The user Screen is nothing..Why ? where is my result?
If you want the output when using system, at least into something you can read in your application, you need to pipe the output:
system("dir > /tmp/output.txt");
FILE *f = fopen("/tmp/output.txt", "r");
char text[1024]; // max sizeof of 1 kb, any more and I'd consider using `malloc()` instead.
fread(text, 1, 1024, f);
printf("%s\n", text);
fclose(f);
There are some problems in your program, at least one of which has already been mentioned.
void main() should be int main(void).
As I recall, the Windows/DOS getch function is declared in <conio.h>; you should have a #include directive for it. Be aware that both <conio.h> and getch are non-standard.
Since main returns int, you should return an int result.
But none of these problems explain the problem you're seeing.
With these changes:
#include <stdlib.h>
#include <conio.h>
int main(void)
{
system("dir");
getch();
return 0;
}
This should work; it should show a directory listing of whatever directory your program runs in (which is determined by TC; I don't know the details).
It's possible that the program is running in an empty directory, which means the dir command wouldn't show any files, but it should still produce some output.
Try commenting out the system() call and adding a printf call (note the added #include <stdio.h>):
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
printf("Hello, world\n");
getch();
return 0;
}
This should open a console window, print "Hello, world" in it, and wait for you to type Enter.
If you still don't see any output (either no console window, or a console window with nothing in it), then you have a problem that's not related to the system() call. Most likely the problem has to do with the way you're using Turbo C (I presume that's what "TC" stands for).
The main function in every C program is supposed to return an int you are returning void
Change void to int:
#include<stdlib.h>
int main()
{
system("dir");
getch();
}
When I tested, the dir command ran in my console and printed to standard out.
May be he is the running the program directly in the Turbo C IDE and hence his output is not visible. If he runs the program directly from cmd line it works. I remember you need to run Alt - F5 or some other combination to see the output window in Turbo C++
I have a written a C program using Visual Studio 2008. The program compares to files in binary mode and tells us if the files are same or different.
I need to execute this program on command line and need to pass 2 arguments along with it.
the first argument is for the file to be compared and 2nd is the file to which it will be compared.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv){
int result_code;
char command_line[256];
sprintf(command_line, "FC /B %s %s > NUL:", argv[1], argv[2]);
result_code=system(command_line);
printf("%s file.\n", result_code ? "different" : "same");
return 0;
}
See this.
http://www.cprogramming.com/tutorial/print/lesson14.html
you can get plenty more from google.