Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Reading and writing files in C

In C you can use either open or fopen 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 and fprintf.

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".

fgets 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 articles
C210 - C and C based languages - File Handling
  [2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)
  [2572] The what and why of C pointers - (2010-01-13)
  [3122] When is a program complete? - (2011-01-06)
  [3386] Adding the pieces together to make a complete language - C - (2011-08-11)
  [4339] Command line and file handling in C - (2014-12-03)
  [4340] Simple C structs - building up to full, dynamic example - (2014-12-03)

Back to
Function Prototypes in C
Previous and next
Horse's mouth home
Forward to
The what and why of C pointers
Some other Articles
Sharing variables between files of code in C - extern
Summary of Wiltshire Core Strategy responses
C Structs - what, how and why
Reading and writing files in C
Function Prototypes in C
How to run a successful online poll / petition / survey / consultation
Forums for your Melksham and open source discussions
Extra MySQL course dates (2 day course, UK)
Excellent staff make for excellent hotel
4759 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, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - 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).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2571_Rea ... -in-C.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb