Any tutorials on how to use CFile or any other, to open a text file, get what's inside into memory, e.x Link List, and then put it back into text file when done? Please link me or guide me through this problem. Trying to get a Payroll system going.

Recommended Answers

All 5 Replies

Like abhimanipal said go check out that website because it is really useful

Here is something I made to show how to read in, sort, then output some data from a text file to a text file.

//#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

struct PERSON
{
	string name;
	int number;
};

bool cmp(PERSON a, PERSON b) //used for sorting
{
	if( a.number > b.number )
		return false;
	return true;
}

int main()
{
	vector<PERSON> people; //holds the data read in

	ifstream in; //creates an in file stream
	in.open("input.txt");

	PERSON tempGuy; //holds data being read in
	while(in >> tempGuy.name)
	{
		in >> tempGuy.number;
		people.push_back(tempGuy);
	}
	in.close(); //closes the stream when done with it

	ofstream out; //creates an out file stream
	out.open("output.txt");

	sort(people.begin(), people.end(), cmp); //sorts the people based on number

	vector<PERSON>::iterator it;
	for( it = people.begin(); it != people.end(); it++ )
		out << (*it).name << " " << (*it).number << endl; //outputs people to text file

	out.close(); //close when done

    return 0;
}

"input.txt"

jim 10
john 32
bob 22
sean 54
mark 49
steve 27
justin 35
alan 31
scott 19
stew 42

I know how to fstream, I am using MFC and it's not letting me hard code fstream... Anyway I can make fstream work?

I had never used MFC before and was trying to play around with it to get it to use ifstream and couldn't work it out.

However I came across this and it looks similar to what you want. here. Towards the end it shows reading in aswell.

Oh I get an error with my ios::in and ios::app, ios is not recognized.

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.