Leslie_Wen

lab10.c

Dec 8th, 2019
106
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #define GOOD 0
  6. #define BAD 1
  7. int isInt(char *S);
  8. int main(int argc,char *argv[]){
  9. int X,line;
  10. FILE *f1;
  11. FILE *f2;
  12. if(argc!=4){
  13. fprintf(stderr, "Usage: %s <int> <file1> <file2> \n",argv[0]);
  14. exit(BAD);
  15. }
  16. if(isInt(argv[1])==0){
  17. fprintf(stderr, "Usage: %s <int> <file1> <file2> \n",argv[0]);
  18. exit(BAD);
  19. }
  20. if((f1=fopen(argv[2],"r"))==NULL){
  21. fprintf(stderr,"%s is not a readable file\n",argv[1]);
  22. exit(BAD);
  23. }
  24. if((f2=fopen(argv[3],"w"))==NULL){
  25. fprintf(stderr, "%s: Cannot open file %s\n",argv[0],argv[3]);
  26. exit(BAD);
  27. }
  28. X=atoi(argv[1]); //Change first argument into int
  29. while ( fscanf(f1, "%d", &line ) != EOF ) { //get int from file 1
  30. fprintf(f2,"%d\n", line+X); //add X to int and put into file 2
  31. }
  32. exit(GOOD);
  33. }
  34. int isInt(char *S){
  35. int i,length;
  36. length=strlen(S);
  37. if (length>0 && S[0] != '-' && !isdigit(S[0]) ) return 0;
  38. for (i=1;i<length;i++) {
  39. if ( ! isdigit (S[i]) ) return 0;
  40. }
  41. return 1;
  42. }
RAW Paste Data