Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 3 of 3

Thread: read & write in C language

  1. #1

    read & write in C language

    yo guys i wrote a C program that generates random numbers, but i wanna write those numbers to a file, like a notepad file, but m kinda stuck, i can't figure it out , i've been tryin' 2 search all over d net but nah

    can somebody hook me up?

  2. #2

    Join #kirupa | Click the under my Username | CL | Jeff

  3. #3
    Templarian, he said C, not C#

    hellboy1, I'm assuming that you're using ANSI C. There is a file pointer datatype which allows us easy access to files. It is defined in stdio.h. To open a file with a file pointer, we use this code:

    Code:
    #include <stdio.h>
    int main()
    {
    /*Let's first of all define our pointer:*/
    FILE *fpointer;
    /*Next we'll open the file and check that we were able to do so*/
    fpointer = fopen("examplefile.txt", "a+");
    if ( fpointer == NULL )
    {
      printf("There has been an error opening your file");
      exit(1);
    }
    }
    The fopen function takes 2 arguments. The first is the file name. The second describes how to open the file. I'll assume you don't want to overwrite the contents each time, so we're using read/append mode (a+).

    Next we have to write to the file. I'll assume that you know how to use the printf function, as there is another function, called fprintf which is just the same, but there is another argument at the beginning, where you put your file pointer. After that we use fclose, and we're done
    The final code:

    Code:
    #include <stdio.h>
    int main()
    {
    /*Let's first of all define our pointer:*/
    FILE *fpointer;
    int mynum = 15; //You could put your random number into this variable ;)
    /*Next we'll open the file and check that we were able to do so*/
    fpointer = fopen("examplefile.txt", "a+");
    if ( fpointer == NULL )
    {
      printf("There has been an error opening your file");
      exit(1);
    }
    //Write to the file
    fprintf (fpointer, "%d\n", mynum);
    //Close the file
    fclose (fpointer);
    }
    Hope that helps you a bit

    -Ben
    Last edited by Ben H; February 5th, 2007 at 01:51 PM.
    If you notice this notice you will notice this notice was inspired by the noticing of another notice.

Thread Information

Users Browsing this Thread

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

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012