load.eof() = 1 at beginning of file

I have some code that is supposed to transfer all the code from one text document to another, but replaces certain strings of characters with another. I've checked my code several times, and I figured out that my problem was .eof() andmy file. I know how to use fstream and .eof(), but the program won't read my file at all. I don't even know where to start. Does anyone know why it might not read my file?
Can you show the code where you attempt to load the file?
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
	load.open("pokeMoves.txt");
	if(!load.is_open())
	{
		cout << "Could not open pokeMoves.txt\n";
		return 0;
	}
	change.open("pokemon moves.txt");
	getline(load, compare);
	if(compare != number)
		change << compare;
	else if(compare == number)
		change << changing;
	else
		cout << "What?\n";
	cout << compare << endl;
	cout << load.eof() << endl;
	while(!load.eof())
	{
		getline(load, compare);
		if(compare != number)
		{
			change << compare;
			cout << compare << endl;
		}
		else if(compare == number)
		{
			change << changing;
			cout << compare << endl;
		}
		else
			cout << "What?\n";
	}
	load.close();
	change.close();


There are some unnecessary lines that are there to make sure that the code works, but none of them are used because the program won't read the file.
the unnecessary lines aren't my concern. It seems to be the logic layout of the code.

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
	load.open("pokeMoves.txt");
	if(!load.is_open())
	{
		cout << "Could not open pokeMoves.txt\n";
		return 0;
	}
        else
        {
	       change.open("pokemon moves.txt");
               // we are not checking to see if this opening cleanly... the file name seems suspect.
               while(!load.eof())
	       {
         		getline(load, compare);
	        	if(compare != number)
		        {
			      change << compare;
			      cout << compare << endl;
		        }
		        else if(compare == number)
		        {
			      change << changing;
			      cout << compare << endl;
		        }
		       else
                       {
			     cout << "What?\n";
                       }
                 } //while
	         load.close();
	         change.close();
          } // else on open. 


this assumes you have one number per line or a string or string of information because I don't see the definition of things. My thought is something didn't open cleanly and is causing errors.
Last edited on
Change is an ofstream variable, load is an ifstream variable, and all the rest of the variables are strings. I don't worry about the newline character, but I do worry about why it's reading that I'm at the end of a full file right after opening it.

EDIT: I used an if statement to make sure that change opened "pokemon moves.txt" and close if it didn't. When I ran it again, I still got an .eof() = 1 even though it was opening and I know that it has data in it.
Last edited on
Try using while(load.good()) and see if there is any difference.
Edit: Would you be able to upload the input file somewhere? I am not able to reproduce the error.
Last edited on
I am starting to suspect it is compiler and development platform stuff. I am not sure how getline is coded but I have never had an issue getting a line from a file and parsing from there in a string. I am not sure what would invalidate the stream, unless you are reading a wide char or unichar file and it fills something incorrectly. Other issues like something that screw it the file pointer in the os may doing something.
I can't figure out what the problem is then because it's an ordinary text file and it has a ton of information. The only thing I can think of that might make this not work is if it's too big, but I highly doubt that because I don't think file size is a factor on whether or not the program can access it unless I make file size a factor or the file is too big for the computer itself. I don't know how to make file size a factor and the file itself is probably less than a megabyte.

Here's the code
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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;

int main()
{
	string moves [351];
	ifstream load;
	ofstream change;
	string compare;
	string number;
	int find = 0;
	string changing;

	load.open("moves.txt");
	if(!load.is_open())
	{
		cout << "Could not open moves.txt\n";
		return 0;
	}

	for(int move = 0; move < 351; move++)
	{
		getline(load, moves[move]);
		transform(moves[move].begin(), moves[move].end(), moves[move].begin(), tolower);
	}
	load.close();

	while(moves[find] != "mediate")
	{
		find++;
		if(find == 352)
		{
			cout << "Could not find mediate\n";
			return 0;
		}
	}
	stringstream(find) >> number;

	find = 0;
	while(moves[find] != "meditate")
	{
		find++;
		if(find == 352)
		{
			cout << "Could not find meditate\n";
			return 0;
		}
	}
	stringstream(find) >> changing;

	load.open("pokeMoves.txt");
	if(!load.is_open())
	{
		cout << "Could not open pokeMoves.txt\n";
		return 0;
	}
	change.open("pokemon moves.txt");
	if(!change.is_open())
	{
		cout << "Could not open pokemon moves.txt\n";
		return 0;
	}
	getline(load, compare);
	if(compare != number)
		change << compare;
	else if(compare == number)
		change << changing;
	else
		cout << "What?\n";
	cout << compare << endl;
	cout << load.eof() << endl;
	while(!load.eof())
	{
		getline(load, compare);
		if(compare != number)
		{
			change << compare;
			cout << compare << endl;
		}
		else if(compare == number)
		{
			change << changing;
			cout << compare  << " >> " << change << endl;
		}
		else
			cout << "What?\n";
	}
	load.close();
	change.close();

return 0;
}


And here's the text file. This is less than a quarter of the file due to the size of it, but this is essentially what the file looks like.
Bulbasaur
307
115
152
332
205
221
303
116
306
276
39
55
114
155
162
201
241
260
26
67
176
294
305
Ivysaur
39
55
114
155
162
201
241
260
26
67
176
294
305
307
115
152
332
266
221
303
116
306
276
Venusaur
39
55
114
155
162
201
241
260
26
67
176
294
305
307
115
152
332
266
221
303
116
306
276
106
Charmander
246
115
78
171
272
245
97
265
72
94
7
17
19
22
71
195
233
305
26
49
67
168
167
176
233
249
294
305
Charmeleon
7
17
19
22
71
195
233
305
26
49
67
168
167
176
233
249
294
305
246
115
78
272
245
97
265
72
94
Charizard
7
17
19
22
71
195
233
305
26
49
67
168
167
176
233
249
294
305
125
246
115
78
171
272
245
97
343
265
72
94
18
Squirtle
307
309
32
345
335
22
220
211
219
260
132
96
105
122
179
181
186
226
347
26
49
67
168
167
176
249
294
Wartortle
96
105
122
179
181
186
226
347
26
49
67
168
167
176
249
294
307
309
32
345
335
22
220
211
219
260
132
Blastoise
96
105
122
179
181
186
226
347
26
49
67
168
167
176
249
294
307
309
32
345
335
22
220
211
219
260
132
349


And here's the moves file (the first file called in my code).

Absorb
Acid
Acid Armor
Aerial Ace
Agility
Air Cutter
Amnesia
Ancientpower
Arm Thrust
Aromatherapy
Assist
Astonish
Attract
Aurora Beam
Barrage
Barrier
Baton Pass
Beat Up
Blast Burn
Belly Drum
Bide
Bind
Bite
Blaze Kick
Blizzard
Block
Body Slam
Bone Club
Bone Rush
Bonemerang
Bounce
Brick Break
Bubble
Bubblebeam
Bulk Up
Bullet Seed
Calm Mind
Camouflage
Charge
Charm
Clamp
Comet Punch
Confuse Ray
Confusion
Constrict
Converstion
Conversion 2
Cosmic Power
Cotton Spore
Counter
Covet
Crabhammer
Cross Chop
Crunch
Crush Claw
Curse
Cut
Defense Curl
Destiny Bond
Detect
Dig
Disable
Dive
Dizzy Punch
Doom Desire
Double Kick
Double Team
Double-Edge
Doubleslap
Dragonbreath
Dragon Claw
Dragon Dance
Dragon Rage
Dream Eater
Drill Peck
Dynamicpunch
Earthquake
Egg Bomb
Ember
Encore
Endeavor
Endure
Eruption
Explosion
Extrasensory
Extremespeed
Facade
Faint Attack
Fake Out
Fake Tears
False Swipe
Featherdance
Fire Blast
Fire Punch
Fire Spin
Fissure
Flail
Flamethrower
Flame Wheel
Flash
Flatter
Fly
Focus Energy
Focus Punch
Follow Me
Foresight
Frenzy Plant
Frustration
Fury Attack
Fury Cutter
Fury Swipes
Future Sight
Giga Drain
Glare
Grasswhistle
Growl
Growth
Grudge
Guillotine
Gust
Hail
Harden
Haze
Headbutt
Heal Bell
Heat Wave
Helping Hand
Hidden Power
Hi Jump Kick
Horn Attack
Horn Drill
Howl
Hydro Pump
Hyper Beam
Hyper Fang
Hyper Voice
Hypnosis
Ice Ball
Ice Beam
Ice Punch
Icicle Spear
Icy Wind
Imprison
Ingrain
Iron Defense
Iron Tail
Jump Kick
Karate Chop
Kinesis
Knock Off
Leaf Blade
Leech Life
Leech Seed
Leer
Lick
Light Screen
Lock-On
Lovely Kiss
Low Kick
Luster Purge
Mach Punch
Magic Coat
Magical Leaf
Magnitude
Mean Look
Meditate
Mega Drain
Mega Kick
Mega Punch
Megahorn
Memento
Metal Claw
Metal Sound
Meteor Mash
Metronome
Milk Drink
Mimic
Mind Reader
Minimize
Mirror Coat
Mirror Move
Mist
Mist Ball
Moonlight
Morning Sun
Mud Shot
Mud Sport
Muddy Water
Mud-Slap
Nature Power
Needle Arm
Night Shade
Nightmare
Odor Sleuth
Octazooka
Outrage
Overheat
Pain Split
Pay Day
Peck
Perish Song
Petal Dance
Pin Missile
Poison Fang
Poison Gas
Poisonpowder
Poison Sting
Poison Tail
Pound
Powder Snow
Present
Protect
Psybeam
Psychic
Psych Up
Psywave
Pursuit
Quick Attack
Rage
Rain Dance
Rapid Spin
Razor Leaf
Razor Wind
Recover
Recycle
Reflect
Refresh
Rest
Return
Revenge
Reversal
Roar
Rock Blast
Rock Slide
Rock Smash
Rock Throw
Rock Tomb
Role Play
Rolling Kick
Rollout
Sacred Fire
Safeguard
Sand-Attack
Sandstorm
Sand Tomb
Scary Face
Scratch
Screech
Secret Power
Seismic Toss
Selfdestruct
Shadow Ball
Shadow Punch
Sheer Cold
Shock Wave
Signal Beam
Silver Wind
Sing
Sketch
Skill Swap
Skull Bash
Sky Attack
Sky Uppercut
Slack Off
Slam
Slash
Sleep Powder
Sleep Talk
Sludge
Sludge Bomb
Smellingsalt
Smog
Smokescreen
Snatch
Snore
Softboiled
Solarbeam
Sonicboom
Spark
Spider Web
Spike Cannon
Spikes
Spit Up
Spite
Splash
Spore
Steel Wing
Stockpile
Stomp
Strength
String Shot
Struggle
Stun Spore
Submission
Substitute
Sunny Day
Super Fang
Superpower
Supersonic
Surf
Swagger
Swallow
Sweet Kiss
Sweet Scent
Swift
Swords Dance
Synthesis
Tackle
Tail Glow
Tail Whip
Take Down
Taunt
Teeter Dance
Teleport
Thief
Thrash
Thunder
Thunderbolt
Thunderpunch
Thundershock
Thunder Wave
Tickle
Torment
Toxic
Transform
Tri Attack
Trick
Triple kick
Twineedle
Twister
Uproar
Vicegrip
Vine Whip
Vital Throw
Waterfall
Water Gun
Water Pulse
Water Sport
Water Spout
Weather Ball
Whirlpool
Whirlwind
Will-O-Wisp
Wing Attack
Wish
Withdraw
Wrap
Yawn
Zap Cannon
Hydro Cannon
Mediate


Sorry for the size of this thing.
I see what you did. I know why the file is at eof.

I would break up the two load files into two different file stream and not use one Load class for two different files. It appears that the Load file object isn't resetting completely on a new file open. It might require a Load.seek(std::ios::begin) or something similar. Because I am suspecting the file pointer of old file is interfering with file pointer of new file and if the sizes aren't exact the eof flag would be set in certain cases. It is easier to make independent objects for all files you open because this could be something with the OS implementation of the stdc++ lib or something that wasn't included in ios classes and may not be covered in the standard.
Thank you. I didn't think of any of that before as I have not come across it before. I will try it when next I can.
Topic archived. No new replies allowed.