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

File in Simple Way

The code opens an input file called "PetList.txt" and reads the names of pets from it line by line. It prints each name to the screen and counts how many times the name "Galaxy" appears. It then opens a new output file called "test.txt", writes two lines of text to it, and closes both files. The code summarizes reading and writing to simple text files in C++.

Uploaded by

SreePrakash
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)
40 views

File in Simple Way

The code opens an input file called "PetList.txt" and reads the names of pets from it line by line. It prints each name to the screen and counts how many times the name "Galaxy" appears. It then opens a new output file called "test.txt", writes two lines of text to it, and closes both files. The code summarizes reading and writing to simple text files in C++.

Uploaded by

SreePrakash
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/ 1

File in simple way

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void main()
{
ifstream input;
input.open("PetList.txt");
if (input.fail()) //true
cerr<<" Cant find the file\n";

int counter=0;
string name;
while(!input.eof())
{
input>>name;//retrive info frm file
cout<<name<<endl;//output black screen
if (name=="Galaxy")
counter++;
}
cout<<"number of pets which are similar :\n"<<counter;

ofstream output;
output.open("test.txt");
output<<"the first line in file \n"<<"Happy Me :)"<<endl;
output.close();

input.close();

getchar();
}

You might also like