File Input Output Practice Question in C

File Input Output:

Q: Make a program to read 5 integers from a file.
#include <stdio.h>

int main() {
    FILE *fptr;
    fptr = fopen("temp.txt", "r");
    int n;
    fscanf(fptr, "%d", &n);
    printf("Number = %d\n", n);
    fscanf(fptr, "%d", &n);
    printf("Number = %d\n", n);
    fscanf(fptr, "%d", &n);
    printf("Number = %d\n", n);
    fscanf(fptr, "%d", &n);
    printf("Number = %d\n", n);
    fscanf(fptr, "%d", &n);
    printf("Number = %d\n", n);
    fclose(fptr);
    return 0;
}
OR
#include <stdio.h>

int main() {
    FILE *ptr;
    ptr = fopen("temp.txt", "r");

    int num;
    for(int i=0; i<5; i++) {
        fscanf(ptr, "%d", &num);
        printf("%d \n", num);
    }

    fclose(ptr);
    return 0;
}
Q: Write a program to generate multiplication table of a given number in text format. Make sure that the file is readable and well formatted.
#include <stdio.h>

int main() {
    FILE *ptr;
    ptr = fopen("temp.txt", "w");

    int table;
    printf("Enter number: ");
    scanf("%d", &table);

    for(int i=1; i<=10; i++) {
        fprintf(ptr, "%d x %d = %d\n", table, i, table*i);
    }

    fclose(ptr);
    return 0;
}
Q: Make a program to input student information from a user and enter it to a file.
#include <stdio.h>

int main() {
    FILE *fptr;
    fptr = fopen("test.txt", "a");
    printf("Enter student details\n");

    char name[50];
    int phone;
    int age;
    float cgpa;
    
    printf("Enter name: ");
    scanf("%s", name);
    printf("Enter phone number: ");
    scanf("%d", &phone);
    printf("Enter your age: ");
    scanf("%d", &age);
    printf("Enter your cgpa: ");
    scanf("%f", &cgpa);

    fprintf(fptr, "\nName: %s\n", name);
    fprintf(fptr, "Phone number: %d\n", phone);
    fprintf(fptr, "Age: %d\n", age);
    fprintf(fptr, "CGPA: %f\n", cgpa);
    
    fclose(fptr);
    return 0;
}

Q: Write a program to write all the odd numbers from 1 to n in a file.

#include <stdio.h>

int main() {
    FILE *fptr;
    fptr = fopen("test.txt", "w");

    int i = 1, n;
    printf("Enter number: ");
    scanf("%d",&n);

    while(i != n){
        if(i % 2 != 0){
            fprintf(fptr, "%d, ", i);
        }
        i++;
    }

    fclose(fptr);
    return 0;
}

Q: Two numbers - a & b, are written in a file. Write a program to replace them with their sum.

#include <stdio.h>

int calcSum(int a, int b);

int main() {
    FILE *fptr;
    fptr = fopen("test.txt", "r");

    int num1, num2;

    // Read numbers from a file
    fscanf(fptr, "%d\n", &num1);
    fscanf(fptr, "%d\n", &num2);

    fptr = fopen("test.txt", "w");
    fprintf(fptr, "%d", calcSum(num1, num2)); // Rewrite the file with their sum
    
    fclose(fptr);
    return 0;
}

int calcSum(int a, int b){
    return a + b;
    return 0;
}
Q: Write a program to read a string from a file and output to the user.
#include <stdio.h>

int main() {
    FILE *fptr;
    fptr = fopen("test.txt", "r");

    char str[100];
    fscanf(fptr, "%s", &str);
    printf("%s ", str);
    fscanf(fptr, "%s", &str);
    printf("%s", str);
    return 0;
}
OR
#include <stdio.h>

int main() {
    FILE *fptr;
    fptr = fopen("test.txt", "r");

    char ch = fgetc(fptr);
    while(ch != EOF){
        printf("%c", ch);
        ch = fgetc(fptr);
    }

    fclose(fptr);
    return 0;
}
Q: Write a program to read a text file character by character and write its content twice in separate file.
#include <stdio.h>

int main() {
    FILE *ptr1, *ptr2;
    ptr1 = fopen("temp.txt", "r");
    ptr2 = fopen("temp2.txt", "w");

    char ch;
    for(int i=0; ; i++) {
        ch = fgetc(ptr1);

        if(ch == EOF) {
            break;
        }

        fprintf(ptr2, "%c", ch);
        fprintf(ptr2, "%c", ch);
        printf("%c", ch);
    }

    fclose(ptr1);
    fclose(ptr2);
    return 0;
}

Q: Take name and salary of two employees as input from the user and write them to a text file in the following format:

  • Name1, 3300
  • Name2, 7700

#include <stdio.h>

int main() {
    FILE *ptr;
    ptr = fopen("temp.txt", "w");
    char name[30];
    int salary;

    for (int i=1; i<=2; i++) {
        printf("Enter Empluyee %d info.\n", i);
        printf("Enter name: ");
        scanf("%s", name);
        printf("Enter salary: ");
        scanf("%d", &salary);
        fprintf(ptr, "Name: %s and salary: %d\n", name, salary);
    }

    fclose(ptr);
    return 0;
}

Q: Write a program to modify a file containing an integer to double its value.

#include <stdio.h>

int main() {
    FILE *ptr;
    ptr = fopen("temp.txt", "r");

    int num;
    fscanf(ptr, "%d", &num);

    ptr = fopen("temp.txt", "w");

    fprintf(ptr, "%d", num*2);

    fclose(ptr);
    return 0;
}
Q: Replace the string in a file with the number of vowels in the string.
#include <stdio.h>

int main() {
    FILE *fptr;
    fptr = fopen("test.txt", "r");

    char ch = fgetc(fptr);
    int count = 0;
    while(ch != EOF){
        printf("%c", ch);
        ch = fgetc(fptr);

        if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'){
            count++;
        }
    }

    printf("\nVowels are: %d", count);

    fclose(fptr);
    return 0;
}
Q: Format the info. of 5 students (name, marks, age course) in a table like structure in a file.
#include <stdio.h>
struct student {
    char name[50];
    int marks;
    int age;
    char course[200];
};

int main() {
    FILE *fptr;
    fptr = fopen("test.txt", "w");

    struct student s[5] = {
        {"hello1", 1, 01, "abc"},
        {"hello2", 2, 02, "def"},
        {"hello3", 3, 03, "ghi"},
        {"hello4", 4, 04, "jkl"},
        {"hello5", 5, 05, "mno"},
    };

    for(int i=0; i<5; i++) {
        fprintf(fptr, "Student %d info:\n", i+1);
        fprintf(fptr, "%s, %d, %d, %s\n\n", s[i].name, s[i].marks, s[i].age, s[i].course);
    }

    fclose(fptr);
    return 0;
}


Post a Comment

0 Comments