Professional Documents
Culture Documents
Hand 7
Hand 7
Hand 7
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;
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];
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);
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");
}
if(ctr>0) printf("Not the same content");
else printf("The same content");
fclose(f1);
fclose(f2);
}
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];
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("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;
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;
}
printf("the file contains:\n");
printf("%d", 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];
fflush(stdin);
}
6. Write a program that will read the student records from a binary file created in the
previous exercise.
#include <stdio.h>
void main(void)
{
FILE *inputf;
char inputfilename[40];
int count, i;
fclose(inputf);
}
Ma. Oliva Ross T. Fulgado
MSAE 1 - 2
Exercises_Handout 7
AE 713