filing in c

Upload: khalid-habib

Post on 06-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Filing in c

    1/6

    Introduction to C Language

    introduction to c programming, c tutorial, c examples, c references, learning c.

    C Language Tutorial

    Introduction to c

    First Program in c

    Constants & Variables in c

    Loops in c language

    Control Structure in c

    Functions in c

    Pointers in c

    Arrays in c

    Strings in C LanguageStructures & Unions in c

    File Handling in c

    Graphics in c

    More Tutorials

    C Tutorial

    PHP Tutorial

    JavaScript Tutorial

    Java TutorialHTML Tutorial

    DHTML Tutorial

    Learn CSS

    Learn XHTML

    VB Script Tutorial

    Learn SQL

    About Me

    Malik

    View my complete profile

    Blogger Template by Blogcrowds

    Sunday, February 1, 2009

    File Handling in c

    http://lectures-c.blogspot.com/http://lectures-c.blogspot.com/2009/02/introduction_01.htmlhttp://lectures-c.blogspot.com/2009/02/first-program_01.htmlhttp://lectures-c.blogspot.com/2009/02/c-constants-alphabets-numbers-and.htmlhttp://lectures-c.blogspot.com/2009/02/loops.htmlhttp://lectures-c.blogspot.com/2009/02/control-structure.htmlhttp://lectures-c.blogspot.com/2009/02/functions.htmlhttp://lectures-c.blogspot.com/2009/02/pointers.htmlhttp://lectures-c.blogspot.com/2009/02/arrays.htmlhttp://lectures-c.blogspot.com/2009/02/strings-in-c.htmlhttp://lectures-c.blogspot.com/2009/02/structures-and-union.htmlhttp://lectures-c.blogspot.com/2009/02/first-program.htmlhttp://lectures-c.blogspot.com/2009/02/introduction.htmlhttp://www.studiesinn.com/learn/Programming-Languages/C-Language.htmlhttp://www.studiesinn.com/learn/Server-Side-Scripting/PHP.htmlhttp://www.studiesinn.com/learn/Client-Side-Scripting/JavaScript.htmlhttp://www.studiesinn.com/learn/Programming-Languages/Java-Language.htmlhttp://www.studiesinn.com/learn/Web-Designing/HTML.htmlhttp://www.studiesinn.com/learn/Web-Designing/DHTML-.htmlhttp://www.studiesinn.com/learn/Web-Designing/CSS.htmlhttp://www.studiesinn.com/learn/Web-Designing/XHTML.htmlhttp://www.studiesinn.com/learn/Client-Side-Scripting/VBScript.htmlhttp://www.studiesinn.com/learn/Server-Side-Scripting/SQL.htmlhttp://www.blogger.com/profile/13602544266228369990http://www.blogger.com/profile/13602544266228369990http://www.blogcrowds.com/resources/blogger_template.phphttp://www.blogcrowds.com/http://lectures-c.blogspot.com/2009/02/first-program.htmlhttp://lectures-c.blogspot.com/http://lectures-c.blogspot.com/2009/02/introduction_01.htmlhttp://lectures-c.blogspot.com/2009/02/first-program_01.htmlhttp://lectures-c.blogspot.com/2009/02/c-constants-alphabets-numbers-and.htmlhttp://lectures-c.blogspot.com/2009/02/loops.htmlhttp://lectures-c.blogspot.com/2009/02/control-structure.htmlhttp://lectures-c.blogspot.com/2009/02/functions.htmlhttp://lectures-c.blogspot.com/2009/02/pointers.htmlhttp://lectures-c.blogspot.com/2009/02/arrays.htmlhttp://lectures-c.blogspot.com/2009/02/strings-in-c.htmlhttp://lectures-c.blogspot.com/2009/02/structures-and-union.htmlhttp://lectures-c.blogspot.com/2009/02/first-program.htmlhttp://lectures-c.blogspot.com/2009/02/introduction.htmlhttp://www.studiesinn.com/learn/Programming-Languages/C-Language.htmlhttp://www.studiesinn.com/learn/Server-Side-Scripting/PHP.htmlhttp://www.studiesinn.com/learn/Client-Side-Scripting/JavaScript.htmlhttp://www.studiesinn.com/learn/Programming-Languages/Java-Language.htmlhttp://www.studiesinn.com/learn/Web-Designing/HTML.htmlhttp://www.studiesinn.com/learn/Web-Designing/DHTML-.htmlhttp://www.studiesinn.com/learn/Web-Designing/CSS.htmlhttp://www.studiesinn.com/learn/Web-Designing/XHTML.htmlhttp://www.studiesinn.com/learn/Client-Side-Scripting/VBScript.htmlhttp://www.studiesinn.com/learn/Server-Side-Scripting/SQL.htmlhttp://www.blogger.com/profile/13602544266228369990http://www.blogcrowds.com/resources/blogger_template.phphttp://www.blogcrowds.com/http://lectures-c.blogspot.com/2009/02/first-program.html
  • 8/2/2019 Filing in c

    2/6

    A file is a collection of bytes stored on a secondary storage device, which is generally a

    disk of some kind. The collection of bytes may be interpreted, for example, as characetrs,words, lines, paragraphs and pages from a textual document; fields and records belonging

    to a database; or pixels from a graphical image. There are two kinds of files that

    programmers deal with text files and binary files.

    Text Files

    A text file can be a stream of characters that a computer can process sequentially. It is not

    only processed sequentially but only in forward direction. For this reason a text file is

    usually opened for only one kind of operation (reading, writing, or appending) at any

    given time.

    Binary Files

    A binary file is no different to a text file. It is a collection of bytes. In C Programming

    Language a byte and a character are equivalent. No special processing of the data occurs

    and each byte of data is transferred to or from the disk unprocessed. C ProgrammingLanguage places no constructs on the file, and it may be read from, or written to, in any

    manner chosen by the programmer.

    Opening a file:

    The general format of the function used for opening a file is

    FILE *fp;fp=fopen(filename,mode);

    The first statement declares the variable fp as a pointer to the data type FILE. As stated

    earlier, File is a structure that is defined in the I/O Library. The second statement opens

    the file named filename and assigns an identifier to the FILE type pointer fp. fopen()contain the file name and mode (the purpose of opening the file).

    r is used to open the file for read only.w is used to open the file for writing only.

    a is used to open the file for appending data to it.

    Closing a File

    A file must be closed as soon as all operations on it have been completed. This would

    close the file associated with the file pointer. The input output library supports thefunction to close a file.

    Syntax to close file

    fclose(filepointer);

  • 8/2/2019 Filing in c

    3/6

    Example

    #include

    void main(void){

    FILE *myfile;

    char c;myfile = fopen("firstfile.txt", "r");

    if (myfile == NULL) printf("File doesn't exist\n");

    else {do {

    c = getc(myfile);

    putchar(c);

    } while (c != EOF);

    }fclose(myfile);

    }

    File operation functions in C:

    Function Name Operation

    fopen() Creates a new file. Opens an existing file.fclose Closes a file which has been opened for use

    getc() Reads a character from a file

    putc() Writes a character to a filefprintf() Writes a set of data values to a file

    fscanf() Reads a set of data values from a file

    getw() Reads a integer from a fileputw() Writes an integer to the file

    fseek() Sets the position to a desired point in the file

    ftell() Gives the current position in the file

    rewind() Sets the position to the beginning of the file

    For More Tutorials c tutorial

    Posted by Malik at 8:49 AM

    4 Comments:

    sankarsaid...

    http://www.studiesinn.com/http://lectures-c.blogspot.com/2009/02/first-program.htmlhttp://www.blogger.com/profile/11930871392159404442http://www.blogger.com/profile/11930871392159404442http://www.addthis.com/bookmark.phphttp://www.studiesinn.com/http://lectures-c.blogspot.com/2009/02/first-program.htmlhttp://www.blogger.com/profile/11930871392159404442
  • 8/2/2019 Filing in c

    4/6

  • 8/2/2019 Filing in c

    5/6

    {char buffer[5] = {0}; /* initialized to zeroes */int i, rc;FILE *fp = fopen("myfile", "rb");if (fp == NULL) {

    perror("Failed to open file \"myfile\"");return EXIT_FAILURE;

    }for (i = 0; (rc = getc(fp)) != EOF && i < 5; buffer[i++] = rc) { }

    fclose(fp);if (i == 5) {

    puts("The bytes read were...");printf("%x %x %x %x %x\n", buffer[0], buffer[1], buffer[2],

    buffer[3], buffer[4]);} else

    fputs("An error occurred while reading the file.\n", stderr);return EXIT_SUCCESS;

    }

  • 8/2/2019 Filing in c

    6/6