Thread: Need Help Fixing My C Program. Deals with File I/O

  1. #1
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235

    Question Need Help Fixing My C Program. Deals with File I/O

    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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    I wonder what you think "(temp, reverse(c))" does. Note that reverse needs the entire line of input, not just one character at a time.

  3. #3
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Hmm ya, i figured im only sending it one character while it needs a string. I tried using fgets and fscanf, but still i get a bunch of errors. What approach would u take in solving this problem.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by Matus View Post
    I tried using fgets and fscanf, but still i get a bunch of errors.
    Then you didn't try hard enough. You need to work one line at a time, so you need to read one line at a time, and fgets is the main line-based input function. You do need to make sure you have a char array handy to put the input into.

  5. #5
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by tabstop View Post
    Then you didn't try hard enough. You need to work one line at a time, so you need to read one line at a time, and fgets is the main line-based input function. You do need to make sure you have a char array handy to put the input into.
    ahh i see its like something on what i was working with at the begining, i had something very close to what u were talking about when i started working with the program. Can you check out the one i worked with at first, i know its practically finished, if i could get that to run properly i can have the one above working in no time. i get these errors with the codes below:

    C:\Documents and Settings\Administrator\My Documents\100_php_scripts\Untitled1.c In function `flipstring':
    68 C:\Documents and Settings\Administrator\My Documents\100_php_scripts\Untitled1.c [Warning] function returns address of local variable


    These are the codes: Sorry i know indentation is crappy on this one.

    Code:
    #include <stdio.h> 
        #include <string.h> 
      
        #include <ctype.h>  
          
        // Our function declaration saying it takes one string pointer and  returns one string pointer.  
    char * flipstring(char *stringtoflip);  
       
     int main() {  
      //  Strings to hold our lines (forward and backward)  
      char  string[256];  
      char flipped[256];  
       
      //  Our file pointers (one in and one out)  
      FILE * infile;  
      FILE * outfile;  
       
      // Open files to valid  txt files  
      infile = fopen("input.txt","r");  
       
     outfile = fopen("output.txt","w");  
       
      // Loop  through the input file reading each line up to 256 chars  
       
     while (fgets(string,256,infile) != NULL) {  
      printf("The  value read in is: %s\n", string);  
       
      // Flip the  string and copy it to our "flipped" string  
       
     strcpy(flipped,flipstring(string));  
       
      // Write  "flipped" to output  
      printf("The value written is:  %s\n",flipped);  
      fputs(flipped,outfile);}  
       
      // Close files  
      fclose(infile);  
      fclose(outfile);  
       
      return 0;  
     }  
        
     // Flips strings by taking incoming   string, loop in reverse  
      // and write each char to reverse  string. Return reverse string.  
      char * flipstring(char *stringtoflip) {  
       char reverse[256];  
        
        
      int j = 0;  
      int i= strlen(stringtoflip)-1;
        
       // Loop through each char of our  string in reverse  
       for( i ;i>=0; i--)  
       {  
       // If it is a newline, just  ignore for now.  
       if (stringtoflip[i] != '\n') {  
        
      reverse[j]=stringtoflip[i];  
       j++;  
       }  
        
      }  
       
       // Terminate the new reverse string.  
       reverse[j] = '\0';  
        
       // Return reverse  string back to main()  
        return reverse;  
     }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    The error is just what it says: you are returning the address of a local variable; but once the function has ended (and the address returned) the local variable ceases to exist, which means you are pointing at nothing in particular. Pass in the array to hold the flipped value instead.

  7. #7
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by tabstop View Post
    The error is just what it says: you are returning the address of a local variable; but once the function has ended (and the address returned) the local variable ceases to exist, which means you are pointing at nothing in particular. Pass in the array to hold the flipped value instead.

    How could i fix that, i tried declaring it as a static variable but still nothing, can you help me out

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by Matus View Post
    How could i fix that, i tried declaring it as a static variable but still nothing, can you help me out
    I suggest reading:
    Quote Originally Posted by tabstop
    Pass in the array to hold the flipped value instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM