0% found this document useful (0 votes)
24 views9 pages

Hand 7

The document contains 6 sections that provide C code examples for performing various file input/output operations using functions like fopen(), fread(), fwrite(), fgetc(), fputc(), etc. The examples cover: 1) Reading/writing characters and integers to text files 2) Reading/writing characters to binary files 3) Storing student records in a struct to a binary file and reading it back 4) Determining if two text files contain the same contents 5) Counting uppercase letters in a text file
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)
24 views9 pages

Hand 7

The document contains 6 sections that provide C code examples for performing various file input/output operations using functions like fopen(), fread(), fwrite(), fgetc(), fputc(), etc. The examples cover: 1) Reading/writing characters and integers to text files 2) Reading/writing characters to binary files 3) Storing student records in a struct to a binary file and reading it back 4) Determining if two text files contain the same contents 5) Counting uppercase letters in a text file
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/ 9

Ma. Oliva Ross T.

Fulgado
MSAE 1 - 2
Exercises_Handout 7
AE 713
EXERCISES - Text File

1. Write a program that will output the characters ‘A’ to ‘Z’ (one character per line)
onto a text file whose filename is specified by the user. Use fputc() for character
output.

#include <stdio.h>

void main(void)
{
FILE *inputf;
char filename[40];
int c;

printf("Input name of text file:");


scanf("%s", filename);

if ( (inputf = fopen(filename, "r") )== NULL)


{
printf("ERROR: %s cannot be opened.\n", filename);
exit(1);
}

while ( (c = fgetc(inputf)) != EOF )


fputc(c, stdout);

fclose(inputf);
}

2. Write a program that will read the text stored in the file generated by the program in the
previous exercise. User fgetc() for character input.

#include <stdio.h>

void main(void)
{
FILE *inputf;
int c;
char filename[40];

printf("Input name of text file:");


scanf("%s", filename);

if ( (inputf = fopen(filename, "r")) == NULL)


{
printf("ERROR: %s cannot be opened.\n", filename);
Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 7
AE 713
exit(1);
}

while ( (c = fgetc(inputf)) != EOF )


printf("%c", c);

fclose(inputf);
}

3. Write a program that will ask the user to input two integers, say n1 and n2. The second
integer should be greater than the second integer, i.e., n1 < n2. Thereafter, the program
should output to a text file the integers from n1 to n2 (i.e., n1, n1 + 1, …, n2-1, n2).
Output one integer per line.

#include<stdio.h>

void main(void)
{
FILE *inputf;
char inputfilename[40];
int num1 = 0, num2 = 0;
char ch, c;
int i;
printf("Enter first number: ");
scanf("%d",&num1);
printf("\nEnter Second number: ");
scanf("%d",&num2);
printf("\nEnter Filename: ");
scanf("%s", inputfilename);
inputf = fopen(inputfilename, "w");
for(i = num1; i <= num2; i++){
fprintf(inputf,"%d \n", i);
}
fclose(inputf);
}

4. Write a program that will read the integers from the file generated in the previous
exercise. The program should also print the integers onto the standard output device.

#include<stdio.h>

void main(void)
{
FILE *inputf;
char inputfilename[40];
char ch, c;
Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 7
AE 713
int i;
// ask for filename, open text file for input
printf("Input name of output text file: ");
scanf("%s", inputfilename);
inputf = fopen(inputfilename, "r");
int d;
if (inputf) {
while ((d = getc(inputf)) != EOF)
putchar(d);
fclose(inputf);
}
}

5. Write a program that will count the number of upper case letters in a text file.

#include<stdio.h>

int main(void)
{
FILE *inputf;
char inputfilename[40];
char ch, c;
int i, upper = 0;
// ask for filename, open text file for input
printf("Input name of output text file: ");
scanf("%s", inputfilename);
inputf = fopen(inputfilename, "r");
int d;
if (inputf) {
while ((d = getc(inputf)) != EOF)
{
if (d >= 'A' && d <= 'Z')
upper++;
}
}
fclose(inputf);

printf("\nuppercase letter(s): %d", upper);


return 0;
}

6. Assume that there are two text files. Write a program that will determine whether these
two text files are identical or not (i.e., the contents are the same).

#include<stdio.h>

int main(void)
Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 7
AE 713
{
FILE *inputf, *f1, *f2;
char inputfilename[40], file1[40], file2[40];
char ch, c;
char c1[1000], c2[1000];
int i, upper = 0,ctr = 0;
// ask for filename, open text file for input
printf("Input name of first file: ");
scanf("%s", file1);
f1 = fopen(file1, "r");
printf("Input name of second file: ");
scanf("%s", file2);
f2 = fopen(file2, "r");

while((fgets(c1,1000,f1)!= NULL) && (fgets(c2,1000,f2)!= NULL))

if(strcmp(c1, c2) != 0){


ctr++;
}

}
if(ctr>0) printf("Not the same content");
else printf("The same content");
fclose(f1);
fclose(f2);
}

EXERCISES - Binary File

1. Write a program that will output the characters ‘A’ to ‘Z’ (one character per line)
onto a binary file whose filename is specified by the user.

#include<stdio.h>

void main(void)
{
FILE *outputf;
char i;
char outputfilename[40];

printf("Input filename: ");


scanf("%s", outputfilename);
Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 7
AE 713
outputf = fopen(outputfilename, "wb");

for (i = 'A'; i <= 'Z'; i++)


fwrite(&i, sizeof(char), 1, outputf);

fclose(outputf);
}

2. Write a program that will read the characters stored in the binary file generated by the
program in the previous exercise.

#include <stdio.h>
void main(void)
{
FILE *inputf;
int i;
char n;
char inputfilename[40];

printf("Input filename: ");


scanf("%s", inputfilename);

if ( (inputf = fopen(inputfilename, "rb")) == NULL)


{
fprintf(stderr, "ERROR: %s does not exists.\n",
inputfilename);

}
printf("the file contains:\n");
while ( 1 )
{
if ( fread(&n, sizeof(char), 1, inputf) < 1 )
{
break;
}
printf("%c", n);
}

fclose(inputf);
}

3. Write a program that will ask the user to input two floating points, say f1 and f2. The
second value should be greater than the second integer, i.e., n1 < n2. Thereafter, the
program should output to a binary file the integers from n1 to n2 (i.e., n1, n1 + 1, …,
n2-1, n2).
Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 7
AE 713
#include<stdio.h>

void main(void)
{
FILE *outputf;
int i;
float f1,f2;

outputf = fopen("ex3file.txt", "wb");

printf("enter first value:");


scanf("%f", &f1);
printf("enter second value:");
scanf("%f", &f2);
if (f1>f2)
printf("The second value shoud be greater than the first value");
else{
for (i = (int) f1; i <= (int) f2; i++)
fwrite(&i, sizeof(int), 1, outputf);
}
fclose(outputf);
}

4. Write a program that will read the integers from the binary file generated in the previous
exercise. The program should also print the integers onto the standard output device.

#include <stdio.h>
void main(void)
{
FILE *inputf;
int n;
int A[1000];
int count;

if ( (inputf = fopen("ex3file.txt", "rb")) == NULL)


{
fprintf(stderr, "ERROR: %s does not exists.\n",
inputf);

}
printf("the file contains:\n");
printf("%d", n);

count = fread(A, sizeof(int), 1000, inputf);

for (n = 0; n < count; n++)


Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 7
AE 713
{
printf("%d ", A[n]);
}

fclose(inputf);
}

5. Write a program that will input 10 student records onto an array of struct studType.
Thereafter, output these student records onto a binary file named by the user.

#include <stdio.h>

struct studentType
{
char name[40];
int idnumber;
char course[10];
};

void main(void)
{
FILE *outputf;
int i;
char outputfilename[40];

struct studentType student[10];

printf("Input 10 student record\n");


for (i = 0; i < 10; i++)
{
printf("student no.%d\n", i+1);
printf("Input name: ");
scanf("%s", &student[i].name);

printf("Input ID number: ");


scanf("%d", &student[i].idnumber);

printf("Input course: ");


scanf("%s", &student[i].course);

fflush(stdin);
}

printf("Input filename: ");


scanf("%s", outputfilename);
Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 7
AE 713
outputf = fopen(outputfilename, "wb");

fwrite(student, sizeof(struct studentType), 10, outputf);


fclose(outputf);
}

6. Write a program that will read the student records from a binary file created in the
previous exercise.

#include <stdio.h>

struct studentType // student structure template


{
char name[40];
int idnumber;
char course[10];
};

void main(void)
{
FILE *inputf;
char inputfilename[40];
int count, i;

struct studentType student[10];

printf("Input filename: ");


scanf("%s", inputfilename);

inputf = fopen(inputfilename, "rb");

count = fread(&student, sizeof(struct studentType), 10, inputf);

for (i = 0; i < count; ++i)


{
printf("student %d:\n", i+1);
printf("Name: %s\n", student[i].name);
printf("ID number: %d\n", student[i].idnumber);
printf("Course: %s\n\n", student[i].course);
}

fclose(inputf);
}
Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 7
AE 713

You might also like