Thread: Writing to a file

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    5

    Writing to a file

    Hi, I'm relatively new to C, so this will probably seem really easy to most of you. Here's what I have to do:

    Write a C program to read an integer and a floating point number from the console and write them to a text file called "exercise3.txt".

    I can do the first part of this no problem, it's writing to the file that I can't work out. I would really appreciate some help with this guys, thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Use fopen() to open the file
    Use fprintf() just like you would printf()

    In fact,
    fprintf( stdout, "hello world\n" );
    printf( "hello world\n" );
    are the same thing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    Use fgets() to read from the keyboard, remove the '\n' character from the end of the string.
    use atoi() to convert a string to an integer
    use atof() to convert a string to a float.
    then do what Salem said.

  4. #4
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Here is a sample of code that does what I think you want it to:

    Code:
    #include <stdio.h>
    
    void main( void )
    {
        // Your integer
        int Integer;
        // Your floating point number
        float FloatingPoint;
        // your file
        FILE* File;
    
        printf("Please enter an integer: ");
        // read the integer;
        scanf("%d", &Integer);
    
        printf("Please enter a floating point number: ");
        // read the floating point number
        scanf("%f", &FloatingPoint);
    
        // Open the file. The "w" opens the file for writing
        File = fopen("exercise.txt", "w");
    
        // Write the integer
        fprintf(File, "%d ", Integer);
    
        // Write the floating point number
        fprintf(File, "%f", FloatingPoint);
    
        // Close the file
        fclose(File);
    }

    I hope that the code works. I don't have C on this computer so I didn't test it. lol. You might need another header file or something. Good Luck!
    Don't quote me on that... ...seriously

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > void main( void )
    Nooooo!!!!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM