fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(void)
  5. {
  6. FILE *fp1, *fp2;
  7. char fname1[256], fname2[256], fname3[256], str[256];
  8. int ch, line, col, linecnt=1, colcnt=1, out=0;
  9.  
  10. printf("filename:"); scanf("%s", fname1);
  11. if ((fp1 = fopen(fname1, "rt")) == NULL) {
  12. printf("Can't open input file(%s)\n", fname1);
  13. return 1;
  14. }
  15. strcpy(fname2, fname1);
  16. strcat(fname2, ".out");
  17. if ((fp2 = fopen(fname2, "w")) == NULL) {
  18. printf("Can't open output file(%s)\n", fname2);
  19. fclose(fp1);
  20. return 1;
  21. }
  22.  
  23. strcpy(fname3, fname1);
  24. strcat(fname3, ".bak");
  25. remove(fname3);
  26.  
  27. printf("insert string:"); scanf("%s", str);
  28. printf("insert line:"); scanf("%d", &line);
  29. // printf("insert col:"); scanf("%d", &col);###
  30. col = 1;
  31.  
  32. while ((ch = fgetc(fp1)) != EOF) {
  33. if (out == 0 && line == linecnt && col == colcnt){
  34. // fprintf(fp2,"%s",str);###
  35. fprintf(fp2,"%s\n",str);
  36. out = 1;
  37. }
  38. fputc(ch,fp2);
  39. if (ch == '\n') {
  40. linecnt++;
  41. colcnt = 1;
  42. }else{
  43. colcnt++;
  44. }
  45. }
  46.  
  47. fclose(fp1);
  48. fclose(fp2);
  49.  
  50. rename(fname1, fname3);
  51. rename(fname2, fname1);
  52.  
  53. return 0;
  54. }
  55.  
Runtime error #stdin #stdout 0s 2428KB
stdin
Standard input is empty
stdout
filename:Can't open input file(��ƿ)