I'm trying to use the system call to append an arbitrary number of files to one specific file... when I attempt to run the program i get the error:

sh: addints.o: not found
sh: main.o: not found
sh: rbyswt.o: not found
sh: readint.o: not found
sh: writeint.o: not found
sh: writestr.o: not found

all the files (addints.o, main.o...) are located in the directory, I can't figure out why the system call can't find these files, I even changed the system call to do a "ls" call to see if it was running the command in the current directory and it printed out said files it could not find.

disregard all the extra functions and variables as I might end up using them again if this attempt at appending the files ends up not working.

Ive attempted other ways of appending these files with no luck and I figured this way couldn't fail but I guess I'm just having bad luck.

int main(int argc, char* argv[]){

  struct stat st;

  int i = 1, j = 0;
  int n = argc - 1;
  long size[n];
  int num;

  char *str;


  FILE *oFile[n];
  FILE *command;
  FILE *combined = fopen("combinedObjects.o", "w");

  for(i = 0; i < n; i++){

    str = ("cat %s >> combinedObjects.o", argv[i]);
    system("ls");
  }    

  return 1;
}

The other way I was trying to do this was through this loop but I'm getting a segmentation fault midway through copying main.o for some reason I can't figure out.

  char ch;

  FILE *oFile[n];
  FILE *combined = fopen("combinedObjects.o", "w");

  for(i = 0; i < n; i++){

    oFile[i] = fopen(argv[i+1], "r");

    while(( ch = fgetc(oFile[i])) != EOF)
      fputc( ch, combined );
  }    

Edited 4 Years Ago by Dewey1040

I was thinking that that line would use the system call as "cat (filename) >> combinedObjects.o" with argv holding filenames. would you need to first make a string and put argv into it then use that line of code?

Do a "sprintf" to store the command string. Then use "system" call.
Also file names given as command line arguments "argv" should start from index 1 to (argc-1).
Make the changes and try again.

This article has been dead for over six months. Start a new discussion instead.