File Handling in C Language
What is a buffer?
A buffer is a temporary holding area in memory which acts as an intermediary between a program and a file or other I/0 device. Information can be transferred between a buffer and a file using large chunks of data of the size most efficiently handled by devices like disc drives. Typically, devices like discs transfer information in blocks of 512 bytes or more, while program often processes information one byte at a time. The buffer helps match these two desperate rates of information transfer. On output, a program first fills the buffer and then transfers the entire block of data to a hard disc, thus clearing the buffer for the next batch of output. C++ handles input by connecting a buffered stream to a program and to its source of input. similarly, C++ handles output by connecting a buffered stream to a program and to its output target.
Using put( ) and get( ) for writing and reading characters
The put ( ) and get( ) functions are also members of ostream and istream. These are used to output and input a single character at a time. The program shown below is intended to illustrate the use of writing one character at a time in a file.
//program for writing characters
#Include <fstream.h>
#include <string.h>
void main( ){
charstr[] = "do unto others as you would be done by ;
ofstream outfile("f2.fil");
for(int i =0; i<strlen(str); i++)
outfile put(str[i]);
}
In this program, the length of the string is found by the strlen( ) function, and the characters are output using put( function in a for loop. This file is also an ASCII file.
Reading Characters
The program shown below illustrates the reading of characters from a file.
//program for reading characters of a string
#Include <fstream.h>
void main( )
{char ch;
ifstream in file("f2.fil")?
while(infile)
infile.get(ch);
cout<<ch;
}
The program uses the get( ) and continues to read until eof is reached. Each character read from the file is displayed using cout. The contents of file f2.fil created in the last program will be displayed on the screen.
Pages: [Page - 1] [Page - 2] [Page - 3] [Page - 4]
Tags: C++ Programming, File, Text File
Like What you See?
Become one of the regulars by subscribing! You'll be the first to know when we add more great posts just like this. Join up by either RSS Feeds or Email Updates today!
There are 257 Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.



































How do you write and read from a binary file using the C Language?.?
imagine we have a file that has the following text:
Two, one, 2,
One fish, two fish?!?
red_fish; BLUE FISH!!!
how can I ignore all non-alphabetic characters and print the alphabetic characters to standard output – each word on a line by itself, with all the letters converted to lower case
output should be:
one
two
one
one
fish
two
fish
red
fish
blue
fish
Kindly let me know at the earliest and email to sunainabansal2003@yahoo.com
thank u
HI,Sunaina how r u i am not under standing ur problem,
plz send ur problem on my email id kanhaiyalalsn@gmail.com
hi sunaina, i got ur prblm.
thats very easy.
You hav to just read the file byte by byte until u encounter any non-alphabetic character. Once a non alphabetic char is found, print the string up to that and then march to the next string starting with the alphabetic character. Best luck. If still hav any prblm email me at chillaraccount@gmail.com bye.
i want to show data which is store in a doc file please help me if any one have code for this.
tabishahmad1@gmail.com
please mail me.
bubble sorting using register and timer and restart the sorting again where stop
How to convert the contents of a file to upper case. file may contain bopth alphabetic and non-alphabetic characters.
Hi,could you please help me with this:
“Write program in C which it will accept from the user the name of file of text and stores the characters that are found in the odd places in the file “odd.txt†and those that find in even in “even.txtâ€.”
-create a program that will compute the running time of another program, not showing the running
time in seconds but by number of (Tfetch, Tstore, Tdelete, etc.) or any related running time axioms..
COULD SOMEONE HELP ME WITH THIS PROBLEM????…
hoping for your kind response…
just send to my email: canon_martina@yahoo.com.ph
please i need help in two or three days about this c problem; develop a simple user authentication system using C language. The system should allow you to register usernames into a file and then check if the user is valid. Remember that to check the validity of the user, you must first enter the username of that user and the system checks if this user exists in the file that stores the users. If a wrong username is supplied to the system, it should display to you an appropriate message, for example “Invalid Userâ€. When you are through with authentication, the system should display all the users stored in the file. you can kindly post your reply to aine.brian@yahoo.com
Hi, i want to delete a line in a file and replace it with another line.can i use the file pointer or help me?
hi…can anyone help me in writing a c program to delete a particular word from file thnks in advance
hi, Supriya how r u i will help u plz send your problem on my email id kanhaiyalalsn@gmail.com
hi…….. supriya i m vinay from bhopal i m mca student whats your question i dont undersdand please send me with code on my email address vinay.intech@rediffmail.com thanks………….
hi…..all
write a program i c without using main().how posssibbe? if anybody has know pls send me answer with code on my email address.vinay.intech@rediffmail.com this question
is a infosys software system
hi can anyone help me with this problem does anyone know how password is hidden and typed as a dot using file handling plz do reply at cbaitharu@yahoo.co.in
#include<stdio.h> main() { FILE *sp, *tp; char ch,nextch; int index = 0; sp = fopen("d:\hello1.txt","r"); tp = fopen("d:\remove.txt","w"); if(sp == NULL) { perror("Cannot Open Source File"); return 0; } while( (ch = fgetc(sp)) != EOF) { if(ch == 10) { index++; nextch = fgetc(sp); if(ch == 10 && nextch != 10) { fputc(ch, tp); fputc(nextch, tp); } } if(ch != 10) { fputc(ch,tp); index++; } } }Hey see there are three more modes to open a file that are w+ : use to open a file which can be both read and write
r+ : use to open a file in which we can write more with also doing alteration in before written data
a+ : this is used to read and write data but wihtout doing alteration in existing data in file if file already exists
for creating numeric data files we have to open them in binary mode like if we want to read an existing file we will use mode that is “rb”
eg: fp=fopen(“abc”,”rb”);
same for write mode
you better give the format of the file. otherwise it will be difficult
——– Original Message ——–
——– Original Message ——–
hello. please send to me a code to sort the contents of file…..thank alot
i want to do a database using file handling in cpp.its a music shoppe database..plz help