Monday, December 7, 2009

File Handling in c

/* Program to create a file and write some data the file */
#include
#include
main( )
{
FILE *fp;
char stuff[25];
int index;
fp = fopen("TENLINES.TXT","w"); /* open for writing */
strcpy(stuff,"This is an example line.");
for (index = 1; index <= 10; index++)
fprintf(fp,"%s Line number %d\n", stuff, index);
fclose(fp); /* close the file before ending program */
}






FILE * fopen(char * filename, char * mode)

The mode value in the above example is set to 'r', indicating that we want to read from the file. Other possible values are:

  • r - open a file in read-mode, set the pointer to the beginning of the file.

  • w - open a file in write-mode, set the pointer to the beginning of the file.

  • a - open a file in write-mode, set the pointer to the end of the file.

  • rb - open a binary-file in read-mode, set the pointer to the beginning of the file.

  • wb - open a binary-file in write-mode, set the pointer to the beginning of the file.

  • ab - open a binary-file in write-mode, set the pointer to the end of the file.

  • r+ - open a file in read/write-mode, if the file does not exist, it will not be created.

  • w+ - open a file in read/write-mode, set the pointer to the beginning of the file.

  • a+ - open a file in read/append mode.

  • r+b - open a binary-file in read/write-mode, if the file does not exist, it will not be created.

  • w+b - open a binary-file in read/write-mode, set the pointer to the beginning of the file.

  • a+b - open a binary-file in read/append mode. 



  • /* Program to display the contents of a file on screen */
    fp = fopen("prog.c","r");
    c = getc(fp) ;    //reading
    while (c!= EOF)
    {
    putchar(c);
    c = getc(fp);
    }



    Deleting Files
    The command to delete a file is:
    remove (char * szFileName);


    Writing to files can be done in various ways:

    • putc() - like fputc()
    • fputc() - int fputc (int character, FILE * stream); - write a character to a file
    • fputs() - int fputs (const char * string , FILE * stream); - write a string to a file
    • int fprintf(
      FILE *stream, const char *format, ... )

    The fprintf() function sends information (the arguments) according to the specified format to the file indicated by stream. fprintf() works just like |printf()| as far as the format goes. The return value of fprintf() is the number of characters outputted, or a negative number if an error occurs.
    Egs.
    char name[20] = "Mary";
    
    ......
    fprintf( out, "Hello %s\n", name );
    
    
    The fputs() function writes an array of characters pointed to by str to
    the given output stream. The return value is non-negative on success,
    and EOF on failure.
    just like reading from files:
    (Note: All functions have 1 argument as file pointer, just note the position of file pointer argument in them)
    • getc(FILE * stream) - like fgetc()
    • fgetc(FILE * stream) - int fgetc (FILE * stream); - write a character to a file 
    • fgets() - char * fgets (char * string , int num , FILE * stream); - write a string to a file
    • int fscanf() - int fscanf ( FILE * stream , const char * format
    • [ , argument , ...] ); - works like scanf() except that it reads from a file instead of STDIN
    The getc() function returns the next character from stream, or EOF if the end of file is reached. getc() is identical to |fgetc()|.  

    The function fgets() reads up to num - 1 characters from the given file stream and dumps them into str. fgets() will stop when it reaches the end of a line, in which case str will be terminated with a newline. If fgets() reaches num - 1 characters or encounters the EOF, str will be null-terminated. fgets() returns str on success, and NULL on an error.
    The function fscanf() reads data from the given file stream in a manner exactly like |scanf()|. The return value of fscanf() is the number of variables that are actually assigned values, or EOF if no assignments could be made.

    Reading and writing in binary format
    int fread( void *buffer, size_t size, size_t num, FILE *stream );
    Description:
    
    The function fread() reads num number of objects (where each object is size bytes) and places them into the array pointed to by buffer.
    The data comes from the given input stream. The return value of the
    function is the number of things read...use |feof()| or |ferror()| to
    figure out if an error occurs.
    
    
    int fwrite( const void *buffer, size_t size, size_t count, FILE *stream );
    Description: The fwrite() function writes, from the array buffer, count objects of size size to stream. The return value is the number of objects written.
    Returns EOF on failure:
    fscanf() , fclose(); fputs() , 
    Returns 0 on success
    fclose() 
    Returns NULL on failure:
    fopen(), freopen() , 
    Returns non-0 on success
    feof() 
    Returns negative no. of failure:
    fprintf(); fputs()


    0 comments:

    Post a Comment