How to read word by word from a file using getline

Hi. I'm doing an assignment. And I'm trying to read input from a file word by word using getline and then assigning each word to a variable, using a function called 'void ReadShares()'. Each line in the input file consists of "date time, price, volume, value, condition". Date is in DD/MM/YYYY format and time is in HH:MM:SS PM format. I'm supposed to display the time in 24 hours clock instead of 12 hours too.

void ReadShares()
{

string str, d1, t, tPrice, vol, val, cond;
ifstream inFile;
ofstream outFile;
inFile.open("Course_of_sales.txt", ifstream::in);
outFile.open("ReadingShares.txt");

if (!inFile.is_open())
{
return;
}
while (!inFile.eof())
{
std::getline(inFile, d1, ' ');
std::getline(inFile, t, ',');
std::getline(inFile, tPrice, ',');
std::getline(inFile, vol, ',');
std::getline(inFile, val, ',');
std::getline(inFile, cond);

cout << d1 << endl;
cout << t << endl;
cout << tPrice << endl;
cout << vol << endl;
cout << val << endl;
cout << cond << endl;

}
}
It's not recommended to use eof() as the condition for a while loop. There are several reasons, one is that there are other error flags which may be set before reaching eof. Another is that, as in your code above, the check is made before attempting to read from the file. What is actually needed is a check on the file status after the input operation.

Typically a better way is to test the result of the input operation within the loop condition, for example like this:
1
2
3
4
5
6
    std::string line;

    while (std::getline(inFile, line))
    {
        std::cout << line << std::endl;
    }

I've not tested this, (can't really do that without seeing a sample of the actual file data) but it might work better than your current code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
while (inFile)
{
    std::getline(inFile, d1, ' ');
    std::getline(inFile, t, ',');
    std::getline(inFile, tPrice, ',');
    std::getline(inFile, vol, ',');
    std::getline(inFile, val, ',');
    std::getline(inFile, cond);
    
    if (!inFile)
        break;    

    cout << d1 << endl;
    cout << t << endl;
    cout << tPrice << endl;
    cout << vol << endl;
    cout << val << endl;
    cout << cond << endl;
}
Last edited on
Is there a reason you want to use getline() instead of just using the extraction operator>>? The extraction operator has the behavior you seem to want. Why are price, volume, and value strings? Wouldn't they be better off being numeric values instead?

I'm supposed to display the time in 24 hours clock instead of 12 hours too.

What have you tried, this is really fairly simple.


Hi, the input file includes:
Date Time, price, volume, value, condition

The file data is something like this:
7/5/2014 12:12:12 PM, 4.3, 2000, 450030, AB CD
7/5/2014 12:11:30 PM, 5.4, 32000, 65930,
7/5/2014 12:09:32 PM, 3.9, 2899, 560030, CD
.
.
.
7/5/2014 10:50:04 AM 6.4, 8750, 123789, F
Is there a reason you want to use getline() instead of just using the extraction operator>>?


Because I want to read the words both separated by commas and white space. Only the date and time is separated by just white space. The other values are separated by commas only.

Why are price, volume, and value strings?


I wanted to use the getline function which, if I'm not wrong, only works on strings.

What have you tried, this is really fairly simple.


I've tried to put this function in another class called timeImp.cpp, which is an implementation file for time.h.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int convertTo24Hrs(int hr, int min, string ap)
{
	if (ap == "AM")
	{
		if (hr == 0)
		{
			hr = 12;
		}
	}
	else
	{
		if (hr == 12)
		{
			hr = hr;
		}
		else
		{
			hr = hr - 12;
		}
	}
}
Topic archived. No new replies allowed.