Computer Science‎ > ‎

C Program: Demonstrating File Handling Functions



 
The C++ Programming Language...
List Price: Rs.775
Our Price: Rs.736
Buy from FlipKart

The C++ Programming Language (Bjarne Stroustrup)

Beginning C++ Through Game Pr...
Our Price: Rs.2008
Buy from FlipKart

       
Beginning C++ through Game Programming 

 
C++ How to Program
List Price: Rs.695
Our Price: Rs.632
Buy from FlipKart

                               
 
The C Programming Language
List Price: Rs.175
Our Price: Rs.137
Buy from FlipKart

             
The C Programming Language (Kernighan)
 
C How To Program 6th  Edition
List Price: Rs.650
Our Price: Rs.624
            Buy from FlipKart


          
C How to Program (Deitel and Deitel)

 


C Program to demonstrate File Handling Functions


/*This program demonstrates various operation on files*/
#include<stdio.h>
int main()
{
    FILE *fp;//we declare a file pointer fp
    fp=fopen("C:\\Users\\Desktop\\ReadExample.txt","r");
    //This tries to open the file ReadExample in reading mode present at location C:\Users\Desktop\
    //(we have to specify the location of the file before reading it)
    if(fp!=NULL)//if fp==NULL then there exists no such file
    {
    char c="";//by ysing getc we read the file from charcater by character and then dipslay its contents
    while(c!=EOF)
    {
    c=getc(fp);
    printf("%c",c);
    }
    }
    else
    printf("No such file exists");
    char *str;
    fscanf(stdin,"%s",str);//The I/O devices can also be treated as files and we use file pointers stdin and stdout for them
    //So we use fscanf to read a string from the standard input device and store it in str
    fprintf(stdout,"%s",str);////Similarly we use fprintf to write a string to the standard output device
    fp=fopen("C:\\Users\\Desktop\\WriteExample.txt","w");
    //Now we open the file pointer in write mode and try to open(if exists) or create(if doesnot exist) file WriteExample
    fprintf(fp,"The Learning Poin");
    putc('t',fp);//We use fprintf and putc to write contents in the file
    close(fp);
    getch();
}
/*A sample run of the program is found to work as follows:-
The program tries to open the file ReadExample from the specified location and then displays the content of the file on standar O/P device
Otherwise NULL is stored in file pointer. After this the program reads a string from the standard I/P device and diplays it back in standard
O/P device. Then a file WriteExample is created at the specified location and The Learning Point is stored in it using fprintf and put c.
*/






 





C Program to Reverse A String

C Program: Building an Expression Evaluator
C Program: Check for Armstrong Numbers
C Program: Check whether a string is a Palindrome or not
C Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal Product
C Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansions
C Program: Computing the Area of a Circle
C Program: Computing the Upper Triangular Matrix and Lower Triangular Matrix
C Program: Demonstrating File Handling Functions
C Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding Determinants
C Program: Demonstrating the use of Bitwise Operators
C Program: Displaying a Histogram of word frequencies (unigram)
C Program: Distance Vector Routing Algorithm using Bellman Ford's Algorithm
C Program: Numerical Computing - The Gaussian Elimination Method
C Program: Numerical Computing - Implementing the Newton Raphson Method
C Program: Numerical Computing - the Bisection Method
C Program: Numerical Computing - The Gaussian Elimination Technique from Linear Algebra
C Program: Numerical Computing - the Jacobi Method
C Program: Printing the Pascal Triangle
C Program: Reversing the order of words in a sentence
C Program: Solving Simultaneous Equations in Two Variables
C Program: Source Code for computing the GCD(HFC) of two numbers
C Program: Source Code for Solving Quadratic Equations
C Program: Source code to solve the Josephus Problem
C Program: Sudoku Solver
C Program: The Usage of Command Line Arguments
C Program: Using the Sieve of Eratosthenes to print Prime Numbers