+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 18

Thread: Reading and Writing Files in C

  1. #1
    Code Warrior Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest's Avatar
    Join Date
    Sep 2009
    Location
    United States
    Age
    16
    Posts
    2,639
    Blog Entries
    3

    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 08:45 AM.
    Code:
     / ___|_   _  ___  ___| |_ 
    | |  _| | | |/ _ \/ __| __|
    | |_| | |_| |  __/\__ \ |_ 
     \____|\__,_|\___||___/\__|

  2. #2
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,885
    Blog Entries
    25

    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?

  3. #3
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Reading and Writing Files in C

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

  4. #4
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,689
    Blog Entries
    57

    Re: Reading and Writing Files in C

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

  5. #5
    Programming Professional so1i is an unknown quantity at this point so1i's Avatar
    Join Date
    Sep 2009
    Posts
    229

    Re: Reading and Writing Files in C

    This is good, well structured. +rep

  6. #6
    Code Warrior Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest's Avatar
    Join Date
    Sep 2009
    Location
    United States
    Age
    16
    Posts
    2,639
    Blog Entries
    3

    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 09:47 PM.
    Code:
     / ___|_   _  ___  ___| |_ 
    | |  _| | | |/ _ \/ __| __|
    | |_| | |_| |  __/\__ \ |_ 
     \____|\__,_|\___||___/\__|

  7. #7
    Code Warrior marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89's Avatar
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,164
    Blog Entries
    2

    Re: Reading and Writing Files in C

    +rep for the tut, you beggar

  8. #8
    Guru debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy's Avatar
    Join Date
    Aug 2009
    Location
    I'm in the... Black Lodge
    Posts
    908

    Re: Reading and Writing Files in C

    Great tutorial +rep
    Last edited by debtboy; 11-10-2009 at 04:14 PM.
    The owls are not what they seem...

  9. #9
    Code Warrior Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest's Avatar
    Join Date
    Sep 2009
    Location
    United States
    Age
    16
    Posts
    2,639
    Blog Entries
    3

    Re: Reading and Writing Files in C

    Thanks everyone
    Code:
     / ___|_   _  ___  ___| |_ 
    | |  _| | | |/ _ \/ __| __|
    | |_| | |_| |  __/\__ \ |_ 
     \____|\__,_|\___||___/\__|

  10. #10
    Newbie tonymorrison39 is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    22

    Re: Reading and Writing Files in C

    very nice i learned something

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Reading and writing files using File methods
    By ArekBulski in forum CSharp Tutorials
    Replies: 15
    Last Post: 10-10-2009, 12:02 PM
  2. Reading and writing files using FileStream class
    By ArekBulski in forum CSharp Tutorials
    Replies: 4
    Last Post: 09-06-2009, 05:53 AM
  3. Replies: 4
    Last Post: 09-01-2009, 05:32 AM
  4. C# Tutorial: Writing Text Files
    By Xav in forum CSharp Tutorials
    Replies: 46
    Last Post: 07-28-2009, 08:18 AM
  5. Replies: 1
    Last Post: 12-05-2008, 03:42 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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