I am having difficulty reading the following input file

Jon 40
Chris 50
Pete 60

what is wrong with this code that I wrote to read the above file. thank you for your help.

//*****************
#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include<cassert>
using namespace std;


void main()
{
	char InputFileName[] ="in.txt";
	char OutputFileName[]="out.txt";
	ifstream InFile;
	ofstream OutFile;
	InFile.open(InputFileName);

	if(!InFile)
	{
		cout<<"Not Open"<<endl;
		exit(1);
	}

	OutFile.open(OutputFileName);

	if(!OutFile)
	{
		cout<<" Can not Open output file"<<endl;
		exit(1);
	}

	char c;
	string name;
	int age;

	while(!InFile)
	{

		 InFile>>name>>age;
	
		OutFile<<name<<"  "<<age<<endl;
		if(InFile.eof()) break;
	}
	InFile.close();
	OutFile.close();

}

<< moderator edit: added [code][/code] tags >>

Recommended Answers

All 2 Replies

You're thinking too hard. :)

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

int main()
{
  const char InputFileName[] ="in.txt";
  const char OutputFileName[]="out.txt";

  std::ifstream InFile(InputFileName);
  std::ofstream OutFile(OutputFileName);

  if (!InFile || !OutFile) {
    std::cerr << "File open failure\n";
    std::exit(EXIT_FAILURE);
  }

  std::string name;
  int age;

  while (InFile >> name >> age)
    Outfile << name << ' ' << age << '\n';
}

what is the different functions on file input output c++

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.