Hello,

I've been Google'ing(?) like crazy for the last hour or so trying to find an answer to my problem, however the problem is simply that I'm not too sure what to search for :S

My basic problem is that I want to change the default way in which the ifstream's operator>> reads in data. Alas, I can't form a coherent sentence to adequately describe *what* (exactly) I want, so I'm going to give a little example:

Current behavior:
inFile >> var1>> var2>> var3 will read in the first three words in the text file separated by a blank space (" "). So, if the file is like this:

Example.txt: How are you ... Then I'd end up with var1="How", var2="are", var3="you". That's fine.. But, what I want instead is for my file to look like this:

Example.txt: How,are you,today Where inFile >> var1>> var2>> var3 would read in the variables and use the delim ',' instead of a blank space.. So I'd end up with var1="How", var2="are you", var3="today".

So, my question is, is this possible? Is it possible to change the ifstream to look for comma's instead of blank spaces? Or, am I going to have to just suck it up and create a new function to read in a line, then use string streams to split that line up into the segments that I want?

I was just kinda hoping to avoid having to do something like that, and I thought it would be a pretty simple thing to do, however now I'm beginning to realize that it's not so simple.

I'm going to include the relevant code from my project, although I don't think it's really necessary; but hey, it can't hurt.

#include "customer.h"
#include <iostream>
#include <fstream>

using namespace std;

void readFile(ifstream& inFile, CustomerList &list);

int main()
{
	CustomerList custList;

	ifstream inFile;
	
	inFile.open("Customers.txt");
	readFile(inFile, custList);
	inFile.close();
	
	cin.get();
}

void readFile(ifstream &inFile, CustomerList &list)
{
	if(inFile)
	{
		string phone, address, name, expires;
		char* number;

		while(inFile >> phone >> address >> name >> number >> expires)
		{
			cout << list.newCustomer(phone, address, name, number, expires);

		}
	}
}[

Customers.txt:

55223344,8 Smith Street,MR T Jones,343523532532,07/08
55123456,27 Joe Place,MRS C Joe,84328483284,12/09
55452342,19 Bad street,MR B Fake,432423432423,03/09

Any input on this would be greatly appreciated.
Thanks,
Dan.

you could use getline() instead of the >> operator. Getline can take in a delimiter, so just use the comma

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.