Practical 11 Files
Practical 11 Files
1. Open notepad, type the following content and save it as “inputstring.txt” to the same folder where your
source file is placed.
Copy and paste the following code as the source file and run it.
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
char line[51];
ifstream inFile("inputstring.txt");
ofstream outFile("outputstring.txt");
if (!inFile || !outFile)
{
if(!inFile)
cout << "Error opening input file\n";
if (!outFile)
cout << "Error opening output file\n";
}
else
{
inFile.getline(line, 51);
while(inFile)
{
int len = strlen(line);
for (int i = 0; i < len; i++)
line[i] = toupper(line[i]);
inFile.getline(line, 51);
}
inFile.close();
outFile.close();
}
return 0;
}
1
UECS1643 Fundamentals Of Programming
(a) Where is the output file “outputstring.txt” It is stored to the same folder where the source file is
stored to? placed.
(b) Modify the highlighted code so that the output ofstream outFile(“e:outputstring.txt”);
file is saved to your thumb drive instead.
Assuming the thumb drive at drive e.
(d) Modify the code inside the while loop so that while (inFile)
{
you will get the following output file content (the int len = strlen(line);
case of the letters is inverted and the punctuation for (int i = 0; i < len; i++)
marks are discarded and replaced by whitespaces): if (islower(line[i]))
line[i] = toupper(line[i]);
else if (isupper(line[i]))
aN APPLE A DAY KEEPS THE DOCTOR AWAY line[i] = tolower(line[i]);
a STITCH IN TIME SAVES NINE else if (ispunct(line[i]))
line[i] = ' ';
mORE HASTE LESS SPEED
outFile << line << endl;
inFile.getline(line, 51);
}
2. Given the following input and output files together with the declarations:
ifstream inFile;
ofstream outFile;
int number1, number2, result;
2
UECS1643 Fundamentals Of Programming
exit(100);
}
If(!outFile)
{
cout << “Error opening input file\n”;
exit(100);
}
(b) Write statements to read the two numbers inFile>> number1 >> number2;
from “mydata.txt” into 2 variables called result = number1 + number2;
number1 and number2, compute their sum outFile << “Result is” << result;
and store into a variable called result,
and write the result to the file
“myreport.txt”.
int main(void)
{
int number, product= 1;
if (!inFile)
cout << "Error opening inout file\n";
else
{
inFile >> number;
while (inFile)
{
product *= number;
inFile >> number;
}
cout << "Product of the numbers is " << product << endl;
inFile.close();
}
return 0;
}
3
UECS1643 Fundamentals Of Programming
2. Write a program to read a set of numbers from a file, store them in an array, and display the numbers in reverse
order. Assume there are no more than 30 numbers in the file.
#include<iostream>
#include<fstream>
using namespace std;
int main(void)
{
int numbers[30], count = 0;
ifstream inFile("numbers.txt");
if (!inFile)
cout << "Error opening input file\n";
else
{
inFile >> numbers[count];
while (inFile)
{
inFile >> numbers[++count];
}
cout << "The numbers in reverse are: \n";
for (int i = count - 1; i >= 0;i--)
{
cout << numbers[i] << " ";
}
cout << endl;
inFile.close();
}
return 0;
}
4
UECS1643 Fundamentals Of Programming
3. Write a program to input a set of words from the user and write the words to a file with spaces added in
between the letters. Assume the longest word is 20 characters long. A sample input and output is as follows:
Input/Output using keyboard/screen Output file contents
Enter a word: Apple Apple
Enter a word: Ball Ball
Enter a word: Chair Chair
Enter a word: Dog Dog
Enter a word: Elephant Elephant
Enter a word: ^Z
#include<iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main(void)
{
char word[21];
ofstream outFile("PartBQ3.txt");
if (!outFile)
cout << "Error opening output file\n";
else
{
do
{
cout << "Enter a word: ";
cin >> word;
for (int i = 0; i < strlen(word);i++)
outFile << word[i] << " ";
outFile << endl;
} while (cin);
outFile.close();
}
return 0;
}
5
UECS1643 Fundamentals Of Programming
4. Write a program to read an input file containing student data. Each line of text in the file contains a student’s
registration number and 3 test scores. The program computes the average score.
Use a structure to represent a student. The structure has 5 members to store the registration number (string), 3
test scores (array of floats), and average score (float). The program should be able to handle a maximum of 50
students using an array of structures. The program produces an output file with a report showing, for each
student, the registration number, 3 tests scores, and the average score.
#include<iostream>
#include<fstream>
#include<cstring>
#include <iomanip>
using namespace std;
typedef struct
{
char reg_no[7];
float scores[3];
float average;
}STUDENT_TYPE;
int main(void)
{
STUDENT_TYPE students[50];
int index = 0;
float total;
ifstream inFile("inputPartBQ4.txt");
ofstream outFile("outputPartBQ4.txt");
if (!inFile || !outFile)
{
if (!inFile)
cout << "Error opening input file\n";
if (!outFile)
cout << "Error opening input file\n";
}
else
{
outFile << "Reg. No. \t Test1\t Test2\t Test3\t Average\n";
inFile >> students[index].reg_no;
while (inFile)
{
//read record and calculate average
total = 0.0;
for (int i = 0;i < 3; i++)
{
inFile >> students[index].scores[i];
total += students[index].scores[i];
}
students[index].average = total / 3;
//write report
6
UECS1643 Fundamentals Of Programming
1. Write and test a program that reads product details from a file and stores the information in an array of
structures. The program then prints a menu to allow the user to do any of the following:
1. List all products
2. Search the price of a product
3. Update the price of a product
4. Exit
If the user chooses to exit, the program will write the updated data in the array of structures to the file.
typedef struct
{
char number[6];
char desc[31];
double price;
} PRODUCT_TYPE;
int main()
{
PRODUCT_TYPE products[50];
int index = 0, choice;
ifstream inFile("product.txt");
if (!inFile)
cout << "Error opening input file\n";
else
7
UECS1643 Fundamentals Of Programming
{
inFile >> products[index].number;
while (inFile)
{
inFile.ignore();
inFile.getline(products[index].desc, 31);
inFile >> products[index].price;
// menu starts
do
{
cout << "\nDo you want to:\n";
cout << "1. List all products\n";
cout << "2. Display the price for a product\n";
cout << "3. Update the price for a product\n";
cout << "4. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice)
{
case 1: list(products, index);
break;
case 2: showPrice(products, index);
break;
case 3: updatePrice(products, index);
break;
case 4: break;
default: cout << "Invalid choice\n";
}
} while (choice != 4);
ofstream outFile("product.txt");
if (!outFile)
cout << "Error opening output file, records are not updated.\n";
else
{
for (int i = 0; i< index; i++)
{
outFile << products[i].number << endl;
outFile << products[i].desc << endl;
outFile << products[i].price << endl;
}
outFile.close();
}
}
return 0;
}
8
UECS1643 Fundamentals Of Programming
return;
}
return;
}