+ Reply to Thread
Page 1 of 4 123 ... LastLast
Results 1 to 10 of 40

Thread: Reading and Writing Files in C

  1. #1
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Reading and Writing Files in C

    This tutorial will show you how to read and write files in C.
    All file functions need stdio.h to work properly.

    The first thing you need to know about is file pointers. File pointers are like any other pointer, but they point to a file. (Kind of obvious). You define a file pointer as follows:
    Code:
    FILE *filepointer;
    In order to make the file pointer point to a file you use the fopen function. The function works as follows:
    Code:
    filepointer=fopen("filename", "mode");
    fopen returns a file pointer. It returns NULL if the file does not exist.
    fopen takes the first argument as the filename to open. It needs to be a string.
    The second argument is the mode argument
    Mode specifies what you want to do with the file.
    Some modes are:
    "r" - read the file
    "w" - write the file
    "a" - append to the file
    "r+" - read and write to the file
    "w+" - read and write, overwrite the file
    "a+" - read and write, append
    These modes will open files in text mode. Files opened in text mode have some bytes filtered out. If you want to open binary files use binary mode by adding a "b" to the mode. For example:
    "rb" - read the file in binary mode

    To read a character from a file, you use fgetc. It is like getchar, but for files.
    It works like this:
    Code:
    character=fgetc(filepointer);
    fgetc returns the character that is read from the file as an integer.
    fgetc takes the file pointer as its only input.
    It will automatically increment the pointer to read the next character.

    fputc allows you to write a character to a file:
    Code:
    fputc(character, filepointer);
    fputc takes an unsigned char as the first argument and the file pointer as the second argument.
    fputc returns EOF on failure.

    You can also use fprintf and fscanf. They work like printf and scanf, except the file pointer is the first argument. They work like this:
    Code:
    fprintf(filepointer, "Hello, World!\n"); //write hello world to the file
    fscanf(filepointer, "%d", integer); //read an integer from the file
    In order to close the file again, you must use fclose. It looks like this:
    Code:
    fclose(filepointer);
    fclose closes the file that filepointer points to.

    If you want to print the contents of data.txt the code would look like this:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	FILE *filepointer;
    	int character;
    	filepointer=fopen("data.txt", "r"); //filepointer points to data.txt
    	if (filepointer==NULL) { //error opening file returns NULL
    		printf("Could not open data.txt!\n"); //error message
    		return 1; //exit with failure
    	}
    	/* while character is not end of file */
    	while ((character=fgetc(filepointer)) != EOF) {
    		putchar(character); //print the character
    	}
    	fclose(filepointer); //close the file
    	return 0; //success
    }
    That's it! If you have questions, comments or suggestions feel free to post! +rep is very appreciated.
    Last edited by Guest; 01-17-2010 at 06:45 AM.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Reading and Writing Files in C

    Nice tutorial. One thing I never fully took the time to understand is the other modes. Technically, aren't there 12 modes? r, w, a, r+, w+, a+ and then all those in binary mode? I never understood the difference between x, x+, and xb - could you elaborate on those?

  4. #3
    Jordan Guest

    Re: Reading and Writing Files in C

    Nicely done, I've already referred a new member to this tutorial.
    +rep

  5. #4
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: Reading and Writing Files in C

    short and sweet. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    so1i's Avatar
    so1i is offline Programming Professional
    Join Date
    Sep 2009
    Location
    Aberystwyth, United Kingdom
    Posts
    309
    Rep Power
    0

    Re: Reading and Writing Files in C

    This is good, well structured. +rep

  7. #6
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Reading and Writing Files in C

    Quote Originally Posted by John View Post
    Nice tutorial. One thing I never fully took the time to understand is the other modes. Technically, aren't there 12 modes? r, w, a, r+, w+, a+ and then all those in binary mode? I never understood the difference between x, x+, and xb - could you elaborate on those?
    I have added the x+ file modes to the list, but I am not really sure what binary mode does. I will look into it.
    Edit: I have researched it and added binary mode to the tutorial.
    Last edited by Guest; 11-04-2009 at 07:47 PM.
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  8. #7
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: Reading and Writing Files in C

    +rep for the tut, you beggar
    Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

  9. #8
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: Reading and Writing Files in C

    Great tutorial +rep
    Last edited by debtboy; 11-10-2009 at 02:14 PM.

  10. #9
    Join Date
    Sep 2009
    Location
    USA
    Posts
    3,400
    Blog Entries
    5
    Rep Power
    37

    Re: Reading and Writing Files in C

    Thanks everyone
    Root Beer == System Administrator's Beer
    Download the new operating system programming kit! (some assembly required)

  11. #10
    Join Date
    Nov 2009
    Posts
    20
    Rep Power
    0

    Re: Reading and Writing Files in C

    very nice i learned something

+ Reply to Thread
Page 1 of 4 123 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. C# Tutorial: Reading and Writing XML Files
    By rueleonheart in forum CSharp Tutorials
    Replies: 3
    Last Post: 07-19-2011, 12:02 PM
  2. First writing and then reading, reopen ?
    By denarced in forum C and C++
    Replies: 2
    Last Post: 10-26-2010, 08:46 PM
  3. Object reading and writing...
    By lintwurm in forum Java Help
    Replies: 6
    Last Post: 03-11-2010, 01:26 PM
  4. Reading and writing files using File methods
    By ArekBulski in forum CSharp Tutorials
    Replies: 15
    Last Post: 10-10-2009, 10:02 AM
  5. Reading and writing files using FileStream class
    By ArekBulski in forum CSharp Tutorials
    Replies: 4
    Last Post: 09-06-2009, 03:53 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts