Learn how to Make Money Online | Free Tech Magazines
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  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 
 
Bookmarks Article Tools Search this Article Display Modes

File Handling in C - File Pointers

By pradeep pradeep is offline

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

ADVERTISEMENT
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.
Old 03-06-2008, 01:58 PM   #2
rahul.mca2001
Ambitious contributor
 
Join Date: Feb 2008
Posts: 104
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 2
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, 02:30 PM   #3
sreeramu
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  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 


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
Forum Jump

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

 

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