Guest User

Untitled

a guest
Jan 20th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. void reverse(char line[]){
  2. int i;
  3. int length;
  4. char tmp;
  5. ..
  6. ..
  7. ..
  8. return;
  9. }
  10.  
  11. #include<stdio.h>
  12. #include<string.h>
  13.  
  14. void reverse(char line[])
  15. {
  16. int i;
  17. int length;
  18. char temp;
  19. if (line == NULL)
  20. return;
  21. length = strlen(line);
  22. for (i = 0 ; i < length / 2 + length % 2 ; ++i)
  23. {
  24. if (line[i] == line[length - i - 1])
  25. continue;
  26. temp = line[i];
  27. line[i] = line[length - i - 1];
  28. line[length - i - 1] = temp;
  29. }
  30. return;
  31. }
  32.  
  33.  
  34.  
  35.  
  36. int main()
  37. {
  38.  
  39. FILE *src_fh, *dst_fh;
  40. char src_fn[256+1], dst_fn[256+1];
  41.  
  42.  
  43. printf("Enter Source File Name:n");
  44. fgets(src_fn, sizeof(src_fn), stdin); reverse(src_fn);
  45.  
  46.  
  47. if( (src_fh = fopen(src_fn, "r")) == NULL )
  48. {
  49. printf("ERROR: Source File %s Failed To Open...n",src_fn);
  50. return(-1);
  51. }
  52.  
  53.  
  54. printf("Enter Destination File Name:n");
  55. fgets(dst_fn, sizeof(dst_fn), stdin); reverse(dst_fn);
  56.  
  57.  
  58. if( (dst_fh = fopen(dst_fn, "w+")) == NULL )
  59. {
  60. fclose(src_fh);
  61. printf("ERROR: Destination File %s Failed To Open...n",dst_fn);
  62. return(-2);
  63. }
  64.  
  65. int ch;
  66. while( (ch = fgetc(src_fh)) != EOF )
  67. {
  68. fputc(ch, dst_fh);
  69. }
  70.  
  71.  
  72. fclose(src_fh);
  73. fclose(dst_fh);
  74. return 0;
  75. }
  76.  
  77. void reverse(char line[])
  78. {
  79. int i;
  80. int length;
  81. char temp;
  82. if (line == NULL)
  83. return;
  84. length = strlen(line);
  85. for (i = 0 ; i < length / 2 + length % 2 ; ++i)
  86. {
  87. if (line[i] == line[length - i - 1])
  88. continue;
  89. temp = line[i];
  90. line[i] = line[length - i - 1];
  91. line[length - i - 1] = temp;
  92. }
  93. return;
  94. }
  95.  
  96. void reverse(char line[])
  97. {
  98. int i;
  99. int length;
  100. int half;
  101.  
  102. if (line == NULL)
  103. return;
  104. length = strlen(line);
  105. half = length / 2 + length % 2;
  106. for (i = 0 ; i < half ; ++i)
  107. {
  108. if (line[i] == line[length - i - 1])
  109. continue;
  110. line[length] = line[i];
  111. line[i] = line[length - i - 1];
  112. line[length - i - 1] = line[length];
  113. }
  114. line[length] = '';
  115. return;
  116. }
  117.  
  118. the following code
  119. 1) incorporates proper error checking
  120. 2) outputs each input line, reversed, to the output file.
  121.  
  122. #include <stdio.h>
  123. #include <stdlib.h>
  124. #include <string.h>
  125.  
  126. char* chomp(char* p)
  127. {
  128. int len;
  129. if(!p) return(p);
  130. if( (len=strlen(p))<=0 ) return(p);
  131. if( p[len-1] == 'n' ) { p[--len] = ''; }
  132. if( p[len-1] == 'r' ) { p[--len] = ''; }
  133. return(p);
  134. } // end function: chomp
  135.  
  136. int main()
  137. {
  138. /* Create Usable Variables */
  139. FILE *src_fh = NULL;
  140. FILE *dst_fh = NULL;
  141. char src_fn[256+1] = {''};
  142. char dst_fn[256+1] = {''};
  143. char line[2048] = {''};
  144.  
  145. /* Retrieve Source File Name From User */
  146. printf("Enter Source File Name:n");
  147. if( NULL == (fgets(src_fn, sizeof(src_fn), stdin) ) )
  148. { // fgets failed
  149. perror("fgets for input file name failed" );
  150. exit(EXIT_FAILURE);
  151. }
  152.  
  153. // implied else, fgets successful
  154.  
  155. chomp(src_fn); // remove trailing newline characters
  156.  
  157. /* Attempt Opening Source File For Reading */
  158. if( (src_fh = fopen(src_fn, "r")) == NULL )
  159. {
  160. perror( "fopen failed" );
  161. printf("ERROR: Source File %s Failed To Open...n",src_fn);
  162. return(-1);
  163. }
  164.  
  165. // implied else, fopen source file successful
  166.  
  167. /* Retrieve Destination File Name From User */
  168. printf("Enter Destination File Name:n");
  169. if( NULL == (fgets(dst_fn, sizeof(dst_fn), stdin) ) )
  170. { // then fgets failed
  171. perror( "fgets for output file name failed" );
  172. fclose(src_fh); // cleanup
  173. exit( EXIT_FAILURE );
  174. }
  175.  
  176. // implied else, fgets for output file name successful
  177.  
  178. chomp(dst_fn); // remove trailing newline characters
  179.  
  180. /* Attempt Opening Destination File For Writing */
  181. if( NULL == (dst_fh = fopen(dst_fn, "w")) )
  182. {
  183. perror( "fopen for output file failed" );
  184. fclose(src_fh); // cleanup
  185. printf("ERROR: Destination File %s Failed To Open...n",dst_fn);
  186. return(-2);
  187. }
  188.  
  189. // implied else, fopen for output file successful
  190.  
  191. int index;
  192. /* Copy Source File Contents (reversed, line by line) to destination file */
  193. while( NULL != (fgets(line, sizeof(line), src_fh) ) )
  194. {
  195. chomp(line); // remove trailing newline characters
  196. index = strlen(line) - 1; // -1 because arrays start with offset 0
  197. // and strlen returns offset to ''
  198.  
  199. // output reversed line to file
  200. while( index >= 0 )
  201. {
  202. fputc( line[index], dst_fh );
  203. index--;
  204. } // end while
  205.  
  206. fputc( 'n', dst_fh );
  207. } // end while
  208.  
  209. /* Close Files On Success */
  210. fclose(src_fh);
  211. fclose(dst_fh);
  212. return 0;
  213. } // end function: main
Add Comment
Please, Sign In to add comment