Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2004
    Location
    kuala lumpur
    Posts
    18

    Talking Unanswered: how to write the info to text file--> C language

    I'm a university first year student, i have some question about the C programing language. My question show below

    1)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"

    my solution is

    #include<stdio.h>

    void main()
    {
    int i;
    float f;
    FILE *ofp;

    printf("Please enter an integer number\n");
    scanf("%d", &i);
    printf("Please enter a floating point number\n");
    scanf("%f", &f);

    ofp = fopen("exercise3.txt", "w");
    fprintf(ofp, "%d \n %f", i, f);

    }

    the exercise.txt file will automatic create, but the value i and f didn't write to the file.... how to solve this

    Thank you

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I'd bet you need fclose() to flush the buffer and close the file.

    -PatP

  3. #3
    Join Date
    Nov 2010
    Posts
    1

    Help

    What is the syntax for "fclose()"? Is it the same as fopen()? If so what is the syntax for inside the brackets?
    Will appreciate the help.

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  5. #5
    Join Date
    Oct 2011
    Posts
    1

    using of Fclose()

    using of fclose() function is easy.
    fclose(Fp);
    where fp is the File pointer.

Posting Permissions

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