Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Programming > C-C++

Discuss / Comment  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More
 
Bookmarks Article Tools Search this Article Display Modes

File Handling in C - File Pointers


On 2nd September, 2006
Post File Handling in C - File Pointers

Show Printable Version Email this Page Subscription Add to Favorites Copy File Handling in C - File Pointers link

Author

pradeep ( Team Leader )

Yet to provide details about himself


All articles By pradeep

Recent Articles

Similar Articles

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

Code: C
FILE *output_file;

Opening a file pointer using fopen



Your program must open a file before it can access it. This is done using the fopen function, which returns the required file pointer. If the file cannot be opened for any reason then the value NULL will be returned. You will usually use fopen as follows

Code: C
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:

"r" => Open a file for reading
"w" => Create a file for writing
"a" => Open a file for appending

Closing a file using fclose



The fclose command can be used to disconnect a file pointer from a file. This is usually done so that the pointer can be used to access a different file. Systems have a limit on the number of files which can be open simultaneously, so it is a good idea to close a file when you have finished using it.

This would be done using a statement like

Code: C
fclose(output_file);
If files are still open when a program exits, the system will close them for you. However it is usually better to close the files properly.

Standard file pointers in UNIX



UNIX systems provide three file descriptors which are automatically open to all C programs. These are

stdin => The standard input.The keyboard or a redirected input file.
stdout => The standard output.The screen or a redirected output file.
stderr => The standard error.This is the screen, even when output is redirected.
This is a conventional place to put any error messages.

Since these files are already open, there is no need to use fopen on them.
The Following User Says Thank You to pradeep For This Useful Post:
vikasbhandare2000 (07-22-2010)
Old 03-06-2008, 12:58 PM   #2
Ambitious contributor
 
Join Date: Feb 2008
Posts: 104
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 3
rahul.mca2001 is on a distinguished road

Re: File Handling in C - File Pointers


thanks a lot , file handling is realyy tough forme
rahul.mca2001 is offline   Reply With Quote
Old 05-23-2008, 01:30 PM   #3
Newbie Member
 
Join Date: Oct 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
sreeramu is on a distinguished road

Re: File Handling in C - File Pointers


if you explain other file related functions in ths thread means it will be very nice....
sreeramu is offline   Reply With Quote
Discuss / Comment  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More


Currently Active Users Reading This Article: 1 (0 members and 1 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
File System Simulation In C rocky1986 C-C++ 6 05-14-2009 08:40 AM
File Splitter and Merger Jaihind C-C++ 2 03-06-2008 12:34 PM
Some Multiple choice questions in C shabbir C-C++ 13 01-30-2008 10:00 AM
File Splitter and Merger Jaihind C-C++ 0 08-26-2006 12:42 PM

 

All times are GMT +5.5. The time now is 05:00 AM.