We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,437 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

system call error

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 );
  }    
3
Contributors
3
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
4
Views
Dewey1040
Junior Poster
140 posts since Dec 2008
Reputation Points: 17
Solved Threads: 3
Skill Endorsements: 0
str = ("cat %s >> combinedObjects.o", argv[i]);

What in your opinion this line is doing?

nezachem
Posting Shark
913 posts since Dec 2009
Reputation Points: 719
Solved Threads: 197
Skill Endorsements: 0

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?

Dewey1040
Junior Poster
140 posts since Dec 2008
Reputation Points: 17
Solved Threads: 3
Skill Endorsements: 0

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.

kings_mitra
Junior Poster
122 posts since May 2009
Reputation Points: 9
Solved Threads: 23
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.2904 seconds using 2.73MB