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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.