john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

file pointers in C

/* 
john pfeiffer 19sep07 File pointers, input/output with Files in C, 
requires infile.txt, writes to outfile.txt with random text content and use 
it as a parameter when running the program, updated 31may08
*/
#include <stdio.h>
/*
http://en.wikibooks.org/wiki/C_Programming/File_IO

Related Topics: ftell(), rewind(), fopen(), fgetpos(), fsetpos() 
r           open a text file for reading
w           truncate to zero length or create a text file for writing
a           append; open or create text file for writing at end-of-file
rb          open binary file for reading
wb          truncate to zero length or create a binary file for writing
ab          append; open or create binary file for writing at end-of-file
r+          open text file for update (reading and writing)
w+          truncate to zero length or create a text file for update
a+          append; open or create text file for update
r+b or rb+  open binary file for update (reading and writing)
w+b or wb+  truncate to zero length or create a binary file for update
a+b or ab+  append; open or create binary file for update
*/

void f(FILE** fpbyref, int argc, char* argv[])
{

   if (argc < 2)
    {   printf("ERROR, only %d argument(s), correct usage: files-c.c.exe infile.txt\n",argc);
        exit(1);
    }else{  printf("trying to open %s\n",argv[1]);  }

    *fpbyref = fopen(argv[1],"r");
}
/* end function tricky pass by reference of a FILE struct (_iobuf) */

FILE* open_outfile(FILE* ofp)
{
     char buffer2[40];

    strcpy(buffer2,"outfile.txt");
    ofp = fopen( buffer2,"a" );   /* append to end of file */

    if( ofp == NULL )
    {   printf("outfile open error!\n");
        exit(1);
    }else{    printf("creating %s\n",buffer2);   }

    return ofp;
}/* end open_outfile func returns a file pointer */

int main( int argc, char* argv[] )  /* needs filename in command line parameters */
{
    char c, buffer[256];
    FILE* ifp;
    FILE* ofp;
    FILE* fpbyref = NULL;   /* file pointer pass by reference  */

/* ***** File pointer opened in main *********** */
    ifp = fopen("infile.txt", "r");
    if( ifp==NULL )
    {   printf("infile open error!");
        exit(1);
    }else{    printf("opening infile\n");   }

/* ***** File pointer passed and returned by value *********** */
    ofp = open_outfile(ofp);

    printf("fpbyref holds (points to NULL memory address) %d\n",fpbyref);
    printf("&fpbyref is at (the address of the pointer in memory) %d\n",&fpbyref);

    f( &fpbyref, argc, argv );    /* pass the filename to open!!! */
    printf("\nafter passing pointer by reference and opening file in a sub function\n\n");
    printf("fpbyref holds (pointed to memory address) %d\n",fpbyref);
    printf("&fpbyref holds (pointer memory address) %d\n",&fpbyref);

    fgets(buffer,32,fpbyref);
    printf("%s",buffer);

    fclose(fpbyref);
/* ***** finish pass by reference *********** */

    c = getc(ifp);                       /*don't forget int ungetc(c, ifp) */
    while( c != EOF )                       
    {
        printf("%c",c);
        putc(c, ofp);
        c = getc(ifp);
    };
                            /* SEEK_CUR,SEEK_END, long int ftell(FILE *stream); */
    fseek(ifp,0,SEEK_SET);          
    fgets(buffer, 20, ifp);
    printf("\nStart at the beginning of the file again:\n%s",buffer);

    /* fgetpos() gets the file position indicator, NOTE  fsetpos */
    /* freopen() opens an existing stream with a different name */
    /* ftell() returns the current file position indicator */

/* feof() returns 0 if at eof, otherwise some non 0 value 
    similarly we could use while( fgets(buffer,1023,fptr) != NULL ) */
    while( !feof(ifp) )
    {   
        fgets(buffer, 255, ifp);
        puts( buffer );               
    };
/*
 ferror() checks for file error 
int fprintf(FILE *fp, char *control-string, ...);
int fscanf(FILE *fp, char *control-string ...);
int fscanf( ifp, "%lf", &temp); d=int, c=char, i=signed int, f=float, s=string, lf=double
        fprintf("%f\n",temp);
 */

/* write_size = fwrite( (char*) &unpacked, 1 , sizeof( struct char_and_flags_unpacked ), fptru );  
    write_size = how much written, data ptr must be cast to a char*, 1 is the number of
    objects to be read/written (for arrays of objects), size of the bytes written, 
    file pointer
    fread is exactly the same...


    output may not be directly followed by input without an intervening call to the 
    fflush function or to a file positioning function (fseek, fsetpos, or rewind), 
    and input may not be directly followed by output without an intervening call to 
    a file positioning function, unless the input operation encounters end-of-file. 
    Opening (or creating) a text file with update mode may instead open (or create) 
    a binary stream in some implementations.
*/

    fclose(ofp);
    fclose(ifp);

    return 0;
}

  • « commandline parameters pass arguments
  • pass program safe parameters »

Published

Sep 19, 2007

Category

c

~554 words

Tags

  • c 95
  • file 92
  • pointers 2