0% found this document useful (0 votes)
5 views

Experiment - 12

Uploaded by

Taaha Baig
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Experiment - 12

Uploaded by

Taaha Baig
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

12 Files

a. Create a file to hold the data of employees input and output data from it.
b. Write a sentence in a file and convert all lower case alphabets to uppercase and vice
versa

12.a. Create a file to hold the data of employees input


and output data from it.

#include <stdio.h>

// Structure to hold employee data


struct Employee {
char name[50];
int eno;
float salary;
};

int main() {
FILE *file;
struct Employee emp;

// Writing employee data to a file


file = fopen("emp.txt", "w");

if (file == NULL) {
printf("Error opening the file.\n");
return -1;
}

printf("Enter employee name: ");


scanf("%s", emp.name);
printf("Enter employee no: ");
scanf("%d", &emp.employeeID);

printf("Enter employee salary: ");


scanf("%f", &emp.salary);

fprintf(file, "%s %d %.2f\n", emp.name, emp.eno, emp.salary);


fclose(file);

// Reading employee data from the file


file = fopen("emp.txt", "r");

if (file == NULL) {
printf("Error opening the file.\n");
return -1;
}

printf("\nEmployee Data:\n");
fscanf(file, "%s %d %f\n", emp.name, &emp.eno, &emp.salary);
printf("Name: %s\n", emp.name);
printf("Employee No: %d\n", emp.eno);
printf("Salary: %.2f\n", emp.salary);

fclose(file);

return 0;
}
12.b. Write a sentence in a file and convert all lower
case alphabets to uppercase and vice versa.

#include <stdio.h>

int main() {
FILE *file1, *file2;
file1 = fopen("file1.txt", "r"); // Open the file1 for reading
file2 = fopen("file2.txt", "w"); // Open the file2 for writing

if (file1 == NULL) {
printf("Error opening the file1.\n");
return -1;
}

char c;

while ((c = fgetc(file1)) != EOF) {


if (c >= 'a' && c <= 'z') {
c = c - 32; // Convert lowercase to uppercase
} else if (c >= 'A' && c <= 'Z') {
c = c + 32; // Convert uppercase to lowercase
}
putchar(c);
fputc(c, file2); // Write the modified character to the file2
}

fclose(file1); // Close the file1


fclose(file2); // Close the file2

printf("conversion done successfully...\n");


return 0;
}

You might also like