Lab 10 (C Programming) Solution

Mar 7, 2019

 programs written here are compiled and run in gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0 

1)Write characters into a file “file.txt”. The set of characters are read from the keyboard until an enter key is pressed (use putc() and getc() function).

Source Code: 

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char ch;
    FILE *file_pointer;
    file_pointer = fopen("file.txt", "w");
    if(file_pointer == NULL){
        printf("failed to open file.");
        exit(1);
    }
    do{
        ch = getchar();
        putc(ch, file_pointer);
    }while(ch != '\n');
    fclose(file_pointer);
    return 0;
}

 

 

Output:

hello this is the string written in file file.txt

 

 

2)Read characters from file “file.txt” created in question 1. Also count the number of characters in the file.

 

Source Code: 

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char ch;
    FILE *file_pointer;
    file_pointer = fopen("file.txt", "r");
    if(file_pointer == NULL){
        printf("failed to open file.");
        exit(1);
    }
    while(1){
        ch = getc(file_pointer);
        if(ch == EOF){
            break;
        }
        printf("%c", ch);
    }
    fclose(file_pointer);
    return 0;
}

 

 

Output:

hello this is the string written in file file.txt

3)Write name, age and height of a person into a data file “person.txt” and read it (use fprintf() and fscanf() function).  

 

Source Code: 

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char name[20];
    int age;
    float height;
    FILE *file_pointer;
    file_pointer = fopen("person.txt", "w");
    if(file_pointer == NULL){
        printf("failed to open file.");
        exit(1);
    }
    printf("Enter name:");
    scanf("%s", name);
    printf("Enter Age:");
    scanf("%d", &age);
    printf("Enter height:");
    scanf("%f", &height);
    fprintf(file_pointer, " %s %d %f", name, age, height);
    fclose(file_pointer);
    file_pointer = fopen("student.txt", "r");
    if(file_pointer == NULL){
        printf("failed to open file.");
        exit(1);
    }
    printf("Information read from file are :\n\n");
    fscanf(file_pointer, " %s %d %f", name, &age, &height);
    printf("Name:%s\n", name);
    printf("Age:%d\n", age);
    printf("Height:%f\n", height);
    fclose(file_pointer);
    return 0;
}

 

 

Output:

Enter name:hari
Enter Age:23
Enter height:23.234
Information read from file are :

Name:hari
Age:23
Height:23.233999

 

4)Write a program to read the file “file.txt” (already existed), convert all the lowercase alphabets to uppercase and write to the same file.

 

Source Code: 

#include <stdio.h>
#include <stdlib.h>

int main(){
    char file_name[] = "file.txt";
    char ch;
    FILE *fptr;
    fptr = fopen(file_name, "r+");
    if(fptr == NULL){
        printf("Failed to open file.");
        exit(1);
    }
    while(1){
        ch = getc(fptr);
        if(ch == EOF){
            break;
        }
        fseek(fptr, -1, 1);
        if(ch >= 97 && ch <= 122){
            ch -= 32;
        }
        putc(ch, fptr);
    }
    fclose(fptr);
    printf("See the file.txt in the directory, where your source exists.\n");
    return 0;
}

 

 

Output:

See the file.txt in the directory, where your source exists.	

 

 

 

5)Write a program to store the 8 student information (name, roll and marks) read from the user to a file called “student.txt”.

 

Source Code: 

#include <stdio.h>
#include <stdlib.h>
#define N 8
struct student{
    char name[20];
    int roll;
    float marks;
};


int main()
{
    char file_name[] = "student.txt";
    FILE *file_ptr;
    int i;
    file_ptr = fopen(file_name, "w");
    if(file_ptr == NULL){
        printf("Failed to open a file.");
        exit(1);
    }
    struct student s[N];
    printf("Enter student information:\n\n");
    for(i = 0; i < N; i++){
        printf("Student: %d\n", i+1);
        printf("Enter Name:");
        scanf(" %[^\n]", s[i].name);
        printf("Enter Roll:");
        scanf(" %d", &s[i].roll);
        printf("Enter Marks:");
        scanf(" %f", &s[i].marks);
        fwrite(&s[i], sizeof(struct student), 1, file_ptr);
    }
    fclose(file_ptr);
    return 0;
}

 

Output:

Enter student information:

Student: 1
Enter Name:hari sundar
Enter Roll:1
Enter Marks:12.345
Student: 2
Enter Name:ram karki
Enter Roll:2
Enter Marks:456
Student: 3
Enter Name:abhinav gautam
Enter Roll:3
Enter Marks:45.565
Student: 4
Enter Name:bidhan sthapit
Enter Roll:5
Enter Marks:89.567
Student: 5
Enter Name:ritu sarkini
Enter Roll:6
Enter Marks:45.555
Student: 6
Enter Name:ramesh sapkota
Enter Roll:7
Enter Marks:50.666
Student: 7
Enter Name:hari timilsina
Enter Roll:8
Enter Marks:45.4545
Student: 8
Enter Name:rita khanal
Enter Roll:9
Enter Marks:45.6666

 

 

 

6)Write a program to open the file “student.txt” created in the problem 5, implement the searching mechanism with the roll number and display the matched information.

Source Code: 

#include <stdio.h>
#include <stdlib.h>
#define N 8
struct student{
    char name[20];
    int roll;
    float marks;
};


int main()
{
    char file_name[] = "student.txt";
    FILE *file_ptr;
    int i, roll_search;
    struct student temp;
    file_ptr = fopen(file_name, "r");
    if(file_ptr == NULL){
        printf("Failed to open a file.");
        exit(1);
    }
    printf("Enter the roll number to search:");
    scanf("%d", &roll_search);
    for(i = 0; i < N; i++){
        fread(&temp, sizeof(struct student), 1, file_ptr);
        if(roll_search == temp.roll){
            printf("Record found:\n\n");
            printf("Name: %s\n", temp.name);
            printf("Roll : %d\n", temp.roll);
            printf("Marks : %f\n", temp.marks);
            exit(0);
        }
    }
    printf("Record not found:\n");
    fclose(file_ptr);
    return 0;
}

 

Output:

Enter the roll number to search:7
Record found:

Name: ramesh sapkota
Roll : 7
Marks : 50.666000

 

7)Write a program to open the file “student.txt” created in the problem 5, implement the sorting and store to the file called “sorted_student.txt”.

 

Source Code: 

#include <stdio.h>
#include <stdlib.h>
#define N 8
struct student{
    char name[20];
    int roll;
    float marks;
};


int main()
{
    char file_name[] = "student.txt", sorted_file[] = "sorted_student.txt";

    FILE *file_ptr;
    int i, j, roll_search;
    struct student s[N], temp;
    file_ptr = fopen(file_name, "r");
    if(file_ptr == NULL){
        printf("Failed to open a file.");
        exit(1);
    }
    printf("Reading information to student array from file:\n");
    for(i = 0; i < N; i++){
        fread(&s[i], sizeof(struct student), 1, file_ptr);
    }
    fclose(file_ptr);
    /* sorting the information  based on marks*/
    for(i = 0; i < N-1; i++){
        for(j = i; j < N; j++)
        {
            if(s[i].marks < s[j].marks){
                temp = s[i];
                s[i] = s[j];
                s[j] = temp;
            }
        }
    }
    file_ptr = fopen(sorted_file, "w");
    if(file_ptr == NULL){
        printf("Failed to open sorted file.");
        exit(1);
    }
    /* writing sorted list and also displaying */
    printf("Sorted Information are :\n\n\n\n");
    for(i = 0; i < N; i++){
        fwrite(&s[i], sizeof(struct student), 1, file_ptr);
        /* also displaying the information */
        printf("Name: %s\n", s[i].name);
        printf("Roll: %d\n", s[i].roll);
        printf("Marks: %f\n", s[i].marks);
    }
    fclose(file_ptr);
    return 0;
}

 

Output:

Reading information to student array from file:
Sorted Information are :



Name: ram karki
Roll: 2
Marks: 456.000000
Name: bidhan sthapit
Roll: 5
Marks: 89.567001
Name: ramesh sapkota
Roll: 7
Marks: 50.666000
Name: rita khanal
Roll: 9
Marks: 45.666599
Name: abhinav gautam
Roll: 3
Marks: 45.564999
Name: ritu sarkini
Roll: 6
Marks: 45.555000
Name: hari timilsina
Roll: 8
Marks: 45.454498
Name: hari sundar
Roll: 1
Marks: 12.345000