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

    Join Date
    Jan 2004
    Location
    UK
    Posts
    10
    Rep Power
    0

    Question Pointer problem in opening a file


    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

    #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 *fPtr, int *anArray);
    int printArray (FILE *fPtr2, int *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 (fPtr, dynamicArray);
    /**************************************/

    /* openfile again x2 */

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

    /*open file once again */
    fPtr= fopen(infilename, "r");

    if ((fPtr=fopen(infilename, "r")) == NULL)

    {
    printf("\n\n*** Cannot REOPEN file ***\a\n");
    };
    /**************************************/
    *printArray = assignDataToArray (fPtr, printArray);
    /**************************************/
    /* write file */
    writefile (FILE *fPtr2, int *printArray); /*PROBLEM OCCURS HERE */

    printf ("\nTotal charaters %i\n", charCount);
    printf ("\n***end of main***\n");
    return 0;
    }
    /**************************************/
    int assignDataToArray(FILE *fPtr, int *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(ch, fPtr2);
    }
    }
    /**************************************/
  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
    first of all. please enclose code int code tags. second, you are getting the filename using gets() but you are nit stripping the \n off the end of that string. that is as far as i care to read without your code being formatted with code tags. to find out how to use code tags, please read the "new users" post at the top of the forum.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    485
    Rep Power
    106
    Code:
    /*open file once again */
    fPtr= fopen(infilename, "r");
    
    if ((fPtr=fopen(infilename, "r")) == NULL)
    
    { 
    printf("\n\n*** Cannot REOPEN file ***\a\n");
    };
    why are you opening twice here?
    Code:
    /* write file */
    writefile (FILE *fPtr2, int *printArray); /*PROBLEM OCCURS HERE */
    this is not the way to call the function.
    call like this
    Code:
    writefile (fPtr, printArray);
  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.

    PHP Code:
    /* write file */
    writefile (FILE *fPtr2int *printArray); /*PROBLEM OCCURS HERE */ 
    the reason i am opening twice here is so that i can write to another file, is that not correct?


    LAKI
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    485
    Rep Power
    106
    Code:
    fPtr= fopen(infilename, "r"); /* opening first time*/
    
    if ((fPtr=fopen(infilename, "r")) == NULL) /* opening second time*/
    
    
    { 
    printf("\n\n*** Cannot REOPEN file ***\a\n");
    };
    that was what I am referring.

    Now,

    is it giving compile error? Tell me what error are you getting?
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    UK
    Posts
    10
    Rep Power
    0
    It complies okay with no problems, when the program runs it only gets as far as function:-

    assignDataToArray (fPtr, dynamicArray);

    My program ran okay before, It opened a file, allocated memory, allocated it an array and wrote to another user specified file.

    When i modfied it for reversing the txt, the problems started occuring
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    485
    Rep Power
    106
    post your code.
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    UK
    Posts
    10
    Rep Power
    0
    This is the modified code, that isn't working. My other code which is working is on my home computer, sorry.

    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 fPtr2, (int*)printArray); 

        
    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);
           }
    }
    /**************************************/ 
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    485
    Rep Power
    106
    what exactly do you want to do with this program? read from one file and put that content in another file.

    Am I correct?
  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    UK
    Posts
    10
    Rep Power
    0
    Yes, i want to read from one file and reverse it contents to another file i.e abcd -> dcba
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    485
    Rep Power
    106
    I have modified your program.
    It reads from the first file and reverses the contents and writes to the second file.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    int countfile(FILE *fPtr);
    int writefile (int *printArray);
    int *allocateDynamicMemory(int memoryAmount);
    int assignDataToArray(FILE *fPtr, int *anArray);
    int printArray (FILE *fPtr2, int *assignDataToArray);
    
    int charCount = 0;
    
    int main ()
    {
    int *printArray =NULL;
    char infilename[20]; /* assign input name */
    FILE *fPtr; /* set opening pointers for reading the 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 ***\n");
           exit(1);
    }
    
    charCount = countfile (fPtr); /* counts the number of characters in file */
    
    printArray = allocateDynamicMemory(charCount);
    
    rewind(fPtr);
    assignDataToArray (fPtr, printArray);
    
    fclose(fPtr);
    
    writefile (printArray);
    
    return 0;
    }
    
    /**************************************/
    int assignDataToArray(FILE *fPtr, int *anArray)
    {
        int ch;
        int i =0;
        ch = getc(fPtr); /* get character fromfile */
        while(ch!=EOF)
        {
            anArray[i++] = ch;
            ch = getc(fPtr);
        }
        printf("\n assignDataToArray is successful...");
    }
    
    /**************************************/
    int countfile(FILE *fPtr)
    {
        int count = 0;
        int ch;
        ch = getc(fPtr); /* get character from file */
        while(ch!=EOF)
        {
    
       count++;
            ch = getc(fPtr);
        }
        printf (" characters in file %d \n", count);
        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(int *printArray)
    {
     FILE *fPtr2;
     int ch ,i=0,len;
     char outputfile[20]; /* assign OUTPUT name */
    
     puts ("\n* Enter OUTPUT file name\n");
     scanf("%s",outputfile);
    
     if ((fPtr2=fopen(outputfile, "w")) ==NULL)
     {
         printf("Can NOT write OUTPUT file! a\n");
         exit(1);
     }
    
     for(i=charCount;i>=0;i--)
     {
       ch=printArray[i];
       putc(ch, fPtr2);
     }
    
    fclose(fPtr2);
    
    }
  22. #12
  23. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    UK
    Posts
    10
    Rep Power
    0
    Thanks for that it works now :)
    Just looking through the code can you explain the rewind is it a function?

    LAKI
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    485
    Rep Power
    106
    Instead of closing the file and opening it again, if you call rewind(fp), then it takes the file pointer to the begining of the file.
  26. #14
  27. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    UK
    Posts
    10
    Rep Power
    0
    Ahh, i understand now, thanks once again, your help has been much appreciated.

    LAKI

IMN logo majestic logo threadwatch logo seochat tools logo