Advertisement
Guest User

KDU easyAssignment

a guest
Oct 23rd, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void getFilename(char[], char[]);
  6.  
  7. int main(void) {
  8. FILE *fh1;
  9. char c;
  10. char fname1[40], fname2[50];
  11. printf("Enter input filename : ");
  12. scanf("%36s",fname1);
  13. //while(getchar()!='\0');
  14. fh1=fopen(fname1, "r");
  15. if (!fh1) {
  16. printf("ERROR: Failed to open %s\n", fname1);
  17. return 0;
  18. }
  19. getFilename(fname1, fname2);
  20. if (strlen(fname2)>0) {
  21. printf("Name part : [%s]\n", fname2);
  22. strcat(fname2, ".out");
  23. // check if the file exists
  24. FILE *fh2 = fopen(fname2,"r");
  25. if (fh2!=NULL) {
  26. printf("ERROR: File [%s] already exists\n", fname2);
  27. fclose(fh2);
  28. return 0;
  29. }
  30. else {
  31. fh2=fopen(fname2,"w");
  32. char tempData[100];
  33. while (!feof(fh1)) {
  34. fputs(fgets(tempData, 100, fh1), fh2);
  35. }
  36. fclose(fh2);
  37. }
  38. }
  39. else
  40. printf("Name [%s] has no DOT\n", fname1);
  41. fclose(fh1);
  42. return 0;
  43. }
  44.  
  45. void getFilename(char f1[], char f2[]) {
  46. char* lastDotLocation = strrchr(f1,'.');
  47. if (lastDotLocation!=NULL) {
  48. strncpy(f2, f1, lastDotLocation-f1);
  49. }
  50. else
  51. f2[0]='\0';
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement