Academic Tutorials



English | French | Portugese | German | Italian
Google

Home Source Codes E-Books Downloads Contact Us About Us

C Tutorial
Introduction to C Programming
Variables in C
Opertaors in C
Branching Statement in C
Looping Statement in C
C Storage Class
Functions in C
The C Preprocessor
Input/Output Functions in C
File I/O Functions in C
Pointers in C
Arrays in C
Dynamic Memory Allocation in C
Strings in C
Structures in C

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
AJAX Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PHP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
.NET (dotnet)
Microsoft.Net
XML Web Services
ASP.Net
.Net Mobile
C# : C Sharp
ADO.NET
VB.NET
Multimedia
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills


File I/O Functions in C
Previous Next


C File Handling - File Pointers

Use a new datatype called a file pointer to with C files . This type is written as FILE *, and defined within stdio.h. A file pointer called output_file is declared in a statement is as follows:

FILE *output_file;



Function Name Operation
fopen() Creates a new file for use
Opens a new existing file for use
fclose Closes a file which has been opened for use
getc() Reads a character from
putc() Writes a character to a file
fprintf() 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 file
putw() 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 begining of the file



Opening a file pointer using fopen()

To open streams,fopen() is used.For opening files for input, this is most often used. Before it can access it,your program must open a file . This function returns the required file pointer. The value NULL will be returned,if the file cannot be opened for any reason. fopen's prototype is as follows:

FILE *fopen (const char *path, const char *mode);

fopen takes in the path to the file as well as the mode and to open the file with. Take for the following example:

iFILE *Fp; Fp = fopen("/home/johndoe/input.dat", "r");

For reading,this will open the file in /home/johndoe/input.dat . You will usually use fopen as given below:

if ((output_file = fopen("output_file", "w")) == NULL)
fprintf(stderr, "Cannot open %s\n", "output_file");



fopen takes two arguments, both are strings, the first is the name of the file to be opened, the second is an access character, which is usually one of the following:

  1. "r"-Opening the file for reading
  2. "a"-Opening the file for appending
  3. "w"-Opening the file for writing
  4. "w+"-open for the writing and reading (existing file will be overwritten)
  5. "r+"-open for the reading and updating (file must already exist)
  6. "a+"-open for the appending and reading (file may or may not exist)

The following code fragmentis used to get the text from the first <title> element:

FILE *output_file;



Closing a file pointer using fclose()

You would use fclose() to close the stream, . The prototype for fclose is given below:

int fclose( FILE *stream );



File I/O :: fgets()

The alternatives to scanf/fscanf is fgets. The prototype is as follows:

char *fgets(char *s, int size, FILE *stream);

fgets stores it into *s pointer and reads in size - 1 characters from the stream. The string is always automatically null-terminated. If it reaches an EOF or newline,fgets stops reading in characters.




File I/O :: sscanf()

The sscanf library call is handy to scan a string for a format.The prototype is as follows:

int sscanf(const char *str, const char *format, ...);

sscanf takes a character pointer instead of a file pointer and works much like fscanf. Instead of scanf/fscanf,using the combination of fgets/sscanf you can avoid the "digestion" problem (or bug, depending on who you talk to :)




File I/O :: fprintf()

  • It is sometimes useful also to output to the different streams.
  • fprintf() allows us to do exactly the same thing
  • The prototype for fprintf is as follows:
  • int fprintf(FILE *stream, const char *format, ...);

    fprintf takes in a special pointer called a file pointer, signified by the FILE *. It then accepts argument and a formatting string and. The only difference between printf and fprintf is that fprintf can redirect output to a particular stream. These streams can be stderr,stdout,or a file pointer. More on the file pointers when we get to fopen. An example is given below:

    fprintf(stderr, "ERROR: Cannot malloc enough memory.\n");

    This output the error message to standard error.




    File I/O :: fscanf()

    fscanf() is basically a streams version of the scanf. The prototype for fscanf is as follows:

    int fscanf( FILE *stream, const char *format, ...);
    File I/O :: fflush()

    If the program crashes, sometimes the stream isn't written. You can do this by using fflush() function. Sometime it is necessary to forcefully flush a buffer to its stream. The prototype for fflush is as follows:

    int fflush(FILE *stream);

    Not very difficult to use, specify the stream to fflush.

    The program which is given below displays use of a file operations. The the program writes it and data enter through the keyboard. Character by character, to file input. The end of the data is indicated by entering an EOF character, which is control-z. the file input is closed at this signal only.

    #include< stdio.h >

    main()
    {
    file *f1;
    printf("Data input output");
    f1=fopen(Input,w); /*Open the file Input*/
    while((c=getchar())!=EOF) /*get a character from key board*/
    putc(c,f1); /*write a character to input*/
    fclose(f1); /*close the file input*/
    printf("\nData output\n");
    f1=fopen(INPUT,r); /*Reopen the file input*/
    while((c=getc(f1))!=EOF)
    printf("%c",c);
    fclose(f1);
    }



    Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
    • blinkbits
    • BlinkList
    • blogmarks
    • co.mments
    • connotea
    • del.icio.us
    • De.lirio.us
    • digg
    • Fark
    • feedmelinks
    • Furl
    • LinkaGoGo
    • Ma.gnolia
    • NewsVine
    • Netvouz
    • RawSugar
    • Reddit
    • scuttle
    • Shadows
    • Simpy
    • Smarking
    • Spurl
    • TailRank
    • Wists
    • YahooMyWeb

    Previous Next

    Keywords: perl functions, c tutorial, php functions, c array, c time, c programs, c examples, c language, c library, c dll, unix c, c program, c example, standard c, array functions, file open, variable functions, unix file, functions tutorial, functions examples, file read, file write, simple c, sample c, variable c, functions example


    HTML Quizes
    HTML Quiz
    XHTML Quiz
    CSS Quiz
    TCP/IP Quiz
    XML Quizes
    XML Quiz
    XSL Quiz
    XSLT Quiz
    DTD Quiz
    Schema Quiz
    XForms Quiz
    XSL-FO Quiz
    XML DOM Quiz
    XLink Quiz
    XQuery Quiz
    XPath Quiz
    XPointer Quiz
    RDF Quiz
    SOAP Quiz
    WSDL Quiz
    RSS Quiz
    WAP Quiz
    Web Services Quiz
    Browser Scripting Quizes
    JavaScript Quiz
    VBScript Quiz
    AJAX Quiz
    DHTML Quiz
    HTML DOM Quiz
    WMLScript Quiz
    E4X Quiz
    Server Scripting Quizes
    ASP Quiz
    PHP Quiz
    PERL Quiz
    SQL Quiz
    ADO Quiz
    .NET (dotnet) Quizes
    Microsoft.Net Quiz
    XML Web Services Quiz
    ASP.Net Quiz
    .Net Mobile Quiz
    C# : C Sharp Quiz
    ADO.NET Quiz
    VB.NET Quiz
    Multimedia Quizes
    SVG Quiz
    Flash Quiz
    Media Quiz
    SMIL Quiz
    Web Building  Quizes
    Web Browsers Quiz
    Web Hosting Quiz
    W3C Quiz
    Web Building Quiz
    Web Quality Quiz
    Web Semantic Quiz
    Web Careers Quiz
    Java Quizes
    Java Quiz
    JSP Quiz
    Servlets Quiz
    Struts Quiz
    EJB Quiz
    JMS Quiz
    JMX Quiz
    Programming Langauges Quizes
    C Quiz
    C++ Quiz
    Visual Basic Quiz
    Data Structures Using C Quiz
    Soft Skills Quizes
    Communication Skills Quiz
    Time Management Quiz
    Project Management Quiz
    Team Work Quiz
    Leadership Skills Quiz
    Corporate Communication Quiz
    Negotiation Skills Quiz

    Privacy Policy
    Copyright © 2003-2008 Vyom Technosoft Pvt. Ltd., All Rights Reserved.