Reading and writing files in C
In C you can use either open
to open a file. open
is what you would use for binary files, and you have plenty of low level controls; use fopen
for text files.
When you fopen
a file, you'll get back a pointer to a structure of type FILE
which you then pass into function such as fgets
Each call to fgets
reads the next
line from the incoming file (up to a certain number of characters) and puts it into a character array. There's no need to move any pointers on to say where in the file you're going to read from - that's automatic as fgets is what we call an "iterator".
returns the number of characters it has read; if you have an empty line in the incoming file you still get "1" back as there's a new line character (which is also included in the string you get). "0" indicates that there is no more data - i.e. you have reached the end of the file - and so that's how you test to know when to stop.
Remember that (in C) a character string has a null on the end of it, so if you're telling your program to read up to (say) 128 characters in a line, you have to dimension you char array to at least 129 ... otherwise you get undefined (and potentially nasty) results if the file you're reading has any lines longer than your maximum.
Finally, you should close your file(s) with fclose
. For a file you're reading / files in programs that run quickly, you won't notice any difference if you forget. If you're writing to files in long-running programs, you may have more of an issue, as information that you think you have output may not reach the file until the program ends.
Example showing all of this ... [HERE]
from the Learning to program in C
course just concluded!
(written 2010-01-12, updated 2010-01-13)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesC210 - C and C based languages - File Handling 
Simple C structs - building up to full, dynamic example - (2014-12-03) 
Command line and file handling in C - (2014-12-03) 
Adding the pieces together to make a complete language - C - (2011-08-11) 
When is a program complete? - (2011-01-06) 
The what and why of C pointers - (2010-01-13) 
New C Examples - pointers, realloc, structs and more - (2009-01-20)
Some other Articles
Sharing variables between files of code in C - externSummary of Wiltshire Core Strategy responsesC Structs - what, how and whyReading and writing files in CFunction Prototypes in CHow to run a successful online poll / petition / survey / consultationForums for your Melksham and open source discussionsExtra MySQL course dates (2 day course, UK)Excellent staff make for excellent hotel
4722 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95 at 50 posts per page
This is a page archived from The Horse's Mouth at
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).