Program to create file and write data in it

Program to create file and write data in it



Consider the following code in which we have created and file and written data on it:

CODE:

# include <stdio. h>

# include <conio. h>

void main ()

{

    FILE * fptr;

    char name [20];

    int age;

    float fee;

    fptr = fopen (“std.txt”, “w”);

    if (fptr == NULL)

    {

        printf (“File does not exists \n”);

        return;

  }

    printf (“Enter name \n”);

    scanf (“%s”, name);

    fprintf (fptr, “Name  = %s\n”, name);

    printf (“Enter age\n”);

    scanf (“%d”, &age);

    fprintf (fptr, “Age  = %d\n”, age);

    printf (“Enter fee\n”);

    scanf (“%f”, &fee);

    fprintf (fptr, “fee  = %.2f\n”, fee);

    fclose (fptr);

}

In the above example a student file is created named std in which we have written name, age and fee of student.