#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    UK
    Posts
    10
    Rep Power
    0

    Pointer problems opening file *formatted*


    I am getting an error for some reason in my complier refering to this line

    writefile (FILE *fPtr2, int *printArray);

    The program opens a file, allocates memory, adds to an array and is supposed to return the code in reverse to write to another file. Please can you help me the whole code is attached to see where i'm going wrong.

    Thanking you in advance
    LAKI

    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
     
    int countfile(FILE *fPtr);
    int writefile (FILE *fPtr2,int *printArray);
    int *allocateDynamicMemory(int memoryAmount);
    int assignDataToArray(FILE *fPtrint *anArray);
    int printArray (FILE *fPtr2int *assignDataToArray);
    /**************************************/  
    int main ()
    {   
    int charCount 0
    int *dynamicArray NULL;
    int *printArray =NULL;
      
    char infilename[20]; /* assign input name */
        
    FILE *fPtr, *fPtr2/* set opening pointers for first file opening and writing file*/

    /* open file and make sure it opens correctly */
    puts ("\n* Enter INPUT file name\n");
    gets (infilename);
        
    if ((
    fPtr=fopen(infilename"r")) == NULL)
                    
                    {
                    
    printf ("\n*** Cannot open file ***\a\n");
                    exit(
    1);
                    };
                    
    charCount countfile (fPtr);  /* counfile function, returns results pointing to charCount*/          
    dynamicArray allocateDynamicMemory(charCount); /* allocateDynamicMemory, assigns memory, 
                                                    which returns value to point to dynamicArray*/
    /**************************************/ 
    assignDataToArray (fPtrdynamicArray);    
    /**************************************/ 

    /* openfile again x2 */

    fclose(fPtr);  /*close previously opened file */

    /*open file once again */
    fPtrfopen(infilename"r");
     
    if ((
    fPtr=fopen(infilename"r")) == NULL)
     
     { 
      
    printf("\n\n*** Cannot REOPEN file ***\a\n");
      };
    /**************************************/    
    *printArray assignDataToArray (fPtrprintArray);
    /**************************************/  
        /* write file */
        
    writefile (FILE *fPtr2int *printArray); /*this is where the problem occurs */

        
    printf ("\nTotal charaters %i\n"charCount);          
        
    printf ("\n***end of main***\n");      
        return 
    0;
    }    
    /**************************************/ 
    int assignDataToArray(FILE *fPtrint *anArray)
    {
        
    int ch;
        
    int i =0;
        
    printf("\n* Array Function successfully activated\n");
        
    ch getc(fPtr); /* get character fromfile */
        
    while(ch!=EOF)
        {
            
    anArray[i] = ch;
            
    i++;
            
    ch getc(fPtr);
            }
    }        

    /**************************************/
     
    int countfile(FILE *fPtr)

        
    int count 0;
        
    int ch;
        
    ch getc(fPtr); /* get character from file */
        
    while(ch!=EOF)
        {
        
    printf ("%d,"count);
        
    count++; 
        
    ch getc(fPtr);};
        
    printf (" characters in file\n");
        return 
    count;
     }   
    /**************************************/ 
    int *allocateDynamicMemory(int memoryAmount)
    int *dynamicMemory =NULL;
    dynamicMemory malloc(memoryAmount sizeof(int));
    if (
    dynamicMemory == NULL) {
        
    printf("\n!!!out of memory!!!\a\n");
        exit(
    1);
        }
        else 
    printf("\n* Memory Allocated successfully\n");
        return 
    dynamicMemory;
        }
    /**************************************/    

    int writefile(FILE *fPtr2,int *printArray)
    {
    int ch ;
     
    int i 0;
        { 
        
    char outputfile[20]; /* assign OUTPUT name */
        
    puts ("\n* Enter OUTPUT file name\n");
        
    gets (outputfile);
        
        if ((
    fPtr2=fopen(outputfile"w")) ==NULL)
                    {
                    
    printf("Can NOT write OUTPUT file! \a\n");
                    exit(
    1);
                    }
            }
    /* actual writing to file occurs here */

        
    {
        
    printArray[i] = ch;
        
    i--;
        
    putc(chfPtr2);
           }
    }
    /**************************************/ 
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Colorful Colorado
    Posts
    743
    Rep Power
    0
    i tried to copy and paste your code onto my computer and it gave me a stray \312 error. you need to enclose your code in [code] tags not [php] tags.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,960
    Rep Power
    194
    Originally posted by jubitzu
    i tried to copy and paste your code onto my computer and it gave me a stray \312 error. you need to enclose your code in [ code ] tags not [ php ] tags.
    Pardon? The "code" or "php" tags won't alter the code itself, only the formatting.

    The code compiles without problems besides the line with the invalid code. Corrected:
    Code:
    writefile ( fPtr2, (int*)printArray);

    hth,
    M.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    UK
    Posts
    10
    Rep Power
    0
    Thanks for the correction in the code, but when i compiled it with the correction the program kept crashing

    LAKI
  8. #5
  9. Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jan 2004
    Location
    near St. Louis Illinois
    Posts
    3,283
    Rep Power
    27
    What compiler are you using? Do you clean up all compiler warnings?After fixing the line that M.Hirsch mentioned, VC++ 6.0 says that the first argument, fPtr2, is being used without being initialized. And that is correct. There is a couple ways to fix that:

    (1) remove the parameter and make it local to the function writefile(), then writefile() should close the file before exiting.

    (2) Pass a pointer to the FILE object to writefile() so that writefile() can set it like it is attempting to do now. But, writefile() does not set the caller's handle because it is passed by value and not by reference.
    Code:
    int writefile (FILE **fPtr2,int *printArray);'
    
    int main()
    {
       FILE* fPtr2 = NULL;
    
       ...
        writefile (&fPtr2, printArray); /*this is where the problem occurs */
        if(fPtr2 != NULL)   
            fclose(fPtr2); 
     
        ...
    }
    
    int writefile(FILE **fPtr2,int *printArray)
    {int ch ;
       ...
    // use fgets() instead of gets()  because gets() can crash your program.
     fgets (outputfile, sizeof(outputfile), stdin);
    
    // Note the star in front of fPtr2
        if (( *fPtr2 = fopen(outputfile, "w")) ==NULL)
       ...
    }
    In addition to the above there are several other errors and warnings you will have to fix before your program will run without crashing.

    You should not attempt to run a program with errors and warnings. Its guarenteed to crash.
    Last edited by Ancient Dragon; February 11th, 2004 at 07:09 AM.

IMN logo majestic logo threadwatch logo seochat tools logo