My program is supposed read in a file supplied from the commandline (filename.ext,) and write/overwrite out a file.
This file will be essentially the same as the original file, but will have all characters on all lines the reverse of the original. For example:
The original file has:
Jack Spratt
could eat no fat.
The output file will look like:
ttartpS kcaJ
.taf on tae dluoc
The approach i decided to take was to read the input file, creat a temporary file, and then reverse it there, later toss the reverse d strings into the output file, but somehow its not working, i believe it has something to do with my reverse function, can anyone help fix it? I also get these errors:
C:\Documents and Settings\Administrator\My Documents\100_php_scripts\myown.c In function `main':
28 C:\Documents and Settings\Administrator\My Documents\100_php_scripts\myown.c [Warning] passing arg 1 of `reverse' makes pointer from integer without a cast
here are the codes i have!
Code:#include <stdio.h> void reverse( char *sPtr ); // function to revers the characters from input to output int main (void) { FILE *fileinput; //pointer to file being modified FILE *temp;//temporary file pointer FILE *fileoutput;//pointer to where contents will be written int c;//hold characters from input file char filename [30]; //file name create char array printf("This program reverses your text file.\n"); printf("Enter a File to be modified:\n" ); scanf("%29s",filename); //opens input file if ((fileinput = fopen (filename, "r")) != NULL){ //creates temporary file if ((temp = tmpfile()) != NULL) { printf("\nThe File before modification is:\n"); //read characters from file and place in temporary file while((c= getc(fileinput)) != EOF) { putchar (c); (temp,reverse(c)); }//end while rewind (temp); rewind(fileinput); printf("\n\n The File after modification i:\n"); //read from temporary file and write to output file while ((c = getc(temp)) != EOF) { putchar(c); putc(c,fileoutput); }// end while }//end if else{ // if temporary file could not be opened printf("unable to open temporary file\n"); }//end else } //end if else {//if input file could not be opened printf("Unable to open %s\n",filename); }//end else fclose (fileoutput); fclose(fileinput); fclose(temp); return 0; } void reverse(char * sPtr ) // reverse function { if( sPtr[ 0 ] == '\0' ){ return; } else{ reverse( &sPtr[ 1 ] ); putchar( sPtr[ 0 ] ); } } // end function



LinkBack URL
About LinkBacks



