Read File,How to get rid of '/' seperator

Hi,

How do I read a date from a file to determine different of days

For example my date.txt contains:

1 23/01/2012 05/02/2012

Currently my code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<fstream>

using namespace std;

class Date
{
public:

	int day;
	int month;
	int year;
	int total_day;
	Date();
	friend  std::ostream& operator << (std::ostream&, Date);

};

Date::Date()
{
	day = 00;
	month = 0;
	year = 0000;
	total_day = 0;
}

inline Date operator-(Date lhs, Date rhs)
{
	Date result;
	int total_day;
	result.year = lhs.year - rhs.year;
	result.month = lhs.month - rhs.month;
	result.day = lhs.day - rhs.day;
	result.total_day = ((result.year * 365) + (result.month * 12) + (result.day));
	return result;
}

int main()
{
	int num;
	Date dt;
	fstream inFile;
	inFile.open("date.txt");
	if (!inFile)
	{
		cout << "Unable to locate file...\n";
	}
	while (!inFile.eof())
	{
		inFile >> num;
		//inFile >> //how do i read  that format?
	}
	inFile.close();

	cout << "Number of days differ is:" << dt.day << endl;

	system("pause");
	return 0;
}
Last edited on
1
2
char skip;
inFile >> dt.day >> skip >> dt.month >> skip >> dt.year;
how do i use strtok ?
it still looks a bit complicated....so i think better to stick to ur 1st method the skip ;-) thx alot for helping me out
Can you check my codes anything wrong?

Becoz i cant see the last cout of "Total Days:"

Text contains:

20-12-2012 13-12-2012

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

class Date
{
public:

	int day, day1;
	int month, month1;
	int year, year1;
	int total_day;
	Date();
	friend  std::ostream& operator << (std::ostream&, Date);

};


Date::Date()
{
	day = 00;
	month = 00;
	year = 0000;
	day1 = 00;
	month1 = 00;
	year1 = 0000;
	total_day = 0;
}


inline Date operator-(Date start, Date due)
{
	Date result;
	int total_day;
	result.year = due.year - start.year1;
	result.month = due.month - start.month1;
	result.day = due.day - start.day1;
	result.total_day = ((result.year * 365) + (result.month * 12) + (result.day));
	return result;
}

std::ostream& operator << (std::ostream& out, Date d)
{
	out << "Day = " << d.day << endl;
	out << "Month = " << d.month << endl;
	out << "Year = " << d.year << endl;

	return out;
}

int main()
{
	fstream inF;
	char skip;
	Date result;
	int day, month, year,day1,month1,year1;
	inF.open("lol.txt");
	if (!inF)
	{
		cout << "File not found...\n";
	}
	while (!inF.eof())
	{
		inF >> day >> skip >> month >> skip >> year;
		inF >> day1 >> skip >> month1 >> skip >> year1;
	}
	inF.close();

	cout << day1 << endl;
	cout << month1 << endl;
	cout << year1 << endl;
	cout << day << endl;
	cout << month << endl;
	cout << year << endl;
	//
	cout << "Total days:" << result.total_day << endl;



	system("pause");
	return 0;
}
Last edited on
while (!inF.eof())<--danger of excess read

Becoz i cant see the last cout of "Total Days:"
Can't see at all? Or some nonsensual value is output?

Because you never initialize your result variable.
while (!inF.eof())<--danger of excess read
-I dont understand here


Becoz i cant see the last cout of "Total Days:"
Can't see at all? Or some nonsensual value is output?
-Can't see at all

Because you never initialize your result variable.
-result.total_day = 0;
Last edited on
last cout of "Total Days:"
I assume you see other output (day, month year)

Show file you are trying to read.
Here's the input operation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include<iostream>
#include<fstream>

using namespace std;

class Date
{
public:

	int day;
	int month;
	int year;
	int total_day;
	Date();
	friend  std::ostream& operator << (std::ostream&, const Date);
	friend  std::istream& operator >> (std::istream&, Date&);
};

Date::Date()
{
	day = 00;
	month = 0;
	year = 0000;
	total_day = 0;
}

inline Date operator-(Date lhs, Date rhs)
{
	Date result;
	int total_day;
	result.year = lhs.year - rhs.year;
	result.month = lhs.month - rhs.month;
	result.day = lhs.day - rhs.day;
	result.total_day = ((result.year * 365) + (result.month * 12) + (result.day));
	return result;
}

std::ostream& operator << (std::ostream& os, const Date d)
{
	os << d.day << '/' << d.month << '/' << d.year;
	return os;
}

std::istream& operator >> (std::istream& is, Date& d)
{
	const char seperator = '/';
	int temp_date = 0;
	int temp_month = 0;
	int temp_year = 0;
	char temp_char = 0;
	is >> temp_date;
	if(!is)
	{
		is.setstate(ios_base::failbit);
		return is;
	}
	is >> temp_char;
	if(temp_char != seperator)
	{
		is.setstate(ios_base::failbit);
		return is;
	}
	is >> temp_month;
	if(!is)
	{
		is.setstate(ios_base::failbit);
		return is;
	}
	is >> temp_char;
	if(temp_char != seperator)
	{
		is.setstate(ios_base::failbit);
		return is;
	}
	is >> temp_year;
	if(!is)
	{
		is.setstate(ios_base::failbit);
		return is;
	}
	d.day = temp_date;
	d.month = temp_month;
	d.year = temp_year;
	return is;
}

int main()
{
	int num;
	Date dt;
	ifstream inFile("date.txt");
	if (!inFile)
	{
		cout << "Unable to locate file...\n";
	}
	while(inFile >> dt)
	{
		cout << dt << '\n';
	}
	inFile.close();

	cout << "Number of days differ is:" << dt.day << endl;

	//system("pause");
	return 0;
}
{
		is.setstate(ios_base::failbit);
		return is;
	}
	is >> temp_year;
	d.day = temp_date;
	d.month = temp_month;
	d.year = temp_year;
	return is;
}

int main()
{
	int num;
	Date dt;
	ifstream inFile("date.txt");
	if (!inFile)
	{
		cout << "Unable to locate file...\n";
	}
	while(inFile >> dt)
	{
		cout << dt << '\n';
	}
	inFile.close();

	//system("pause");
	return 0;
}


Hope it helps! By the way when overloading the >> operator you have to pass by reference.
Last edited on
Dear benbalach, thks for ur reply.

I have tried to modify my code earlier and appeared to be working.


Not sure if can use same thread for a new question.

My new question is how to read name.txt.
Content: Robin/Hood

I would like to read first and last name.I tried with the same code used for date.txt but not working though

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
	fstream in;
	in.open("name.txt");
	char skip;
	string name, name1;
	if (!in)
	{
		cout << "File not valid...\n";
	}
	while (!in.eof())
	{
		in >> name >> skip >> name1;
	}

	cout << name << endl;
	cout << name1 << endl;

	system("pause");
	return 0;
}


1
2
3
4
The output was:
Robin/Hood instead of the one i wanted which is
Robin
Hood
Last edited on
in >> name >> skip >> name1; Where do you think string ends?

Use something like:
1
2
std::getline(std::cin, name, '/');
std::getline(std::cin, name1);


If you have more similar questions I suggest you to look into boost::tokenizer and boost::split
in >> name >> skip >> name1;

May i know why this code doesnt work?


Whats the differences between (boost::tokenizer and boost::split) vs strtok?

name is a string. in >> name will read all characters which are valid for strings (that will be all characters) until it hits invalid character for string (none exist) or whitespace character, so it reads all your line and does not leave anything for other input operations.

differences between (boost::tokenizer and boost::split) vs strtok?
tokenizer is a powerful tool when you need to iterate over string parts delimeted by some tokens (may be more than one). Split works similar to tokenizer, but it copies resulting tokens into container of your choice.

strtok is C-style dangerous, parameter modifying non-reentrant function. If you do not have todeal with C API there is almost always is better ways to do something.
Thks for enlighten me ;-)
Topic archived. No new replies allowed.