Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file i/o problem (not about vc++)

Status
Not open for further replies.

airswit

Programmer
Apr 19, 2003
23
0
0
US
hi, i was having trouble with this code opening the file:
void loadstats()
{
fstream input;
char name[]="save.txt";
input.open(name, ios::in|ios::eek:ut|ios::noreplace);
if (input.fail())
{
input.open(name, ios::in|ios::eek:ut);
if (input.fail())
cout<<&quot;this shows up at runtime&quot;;
}
else
{
cout<<&quot;Enter Player 1 name: &quot;;
cin>>players[1];
cout<<&quot;Enter Player 2 name: &quot;;
cin>>players[2];
input<<players[1]<<endl<<players[2];
}
input>>players[1]>>players[2];
cout<<players[1]<<' '<<players[2]<<endl;
system(&quot;pause&quot;);
}


and i was wondering if anyone could help me out. the file doesn't open if it was created, so when i try to load to the 'players' variables, nothing happens and the default values remain. ps, players is declared as:
char players[3][20]={&quot;&quot;,&quot;Player 1&quot;,&quot;Player 2&quot;};
 
>> input.open(name, ios::in|ios::eek:ut);

that should work unless maybe some other process has the file open exclusively?

-pete

 
for some reason, and i don't know why, that code just doesn't want to work. i put it in its own program, compiled it, and it just doesn't want to open the file after it is created the first time through!
 
Are you saying you see this cout<<&quot;this shows up at runtime&quot;; the second time you execute?

-pete


 
that's exactly what i mean. like once the file 'save.txt' is created, that cout is executed instead of the file opening for reading/writing
 
Is this what you are trying to achieve?

Code:
fstream fs;
char name[] = &quot;save.txt&quot;;
fs.open(name, ios::in)
if(fs.fail())
{
 cout << &quot;this shows at runtime&quot; << endl;
 fs.clear();
 fs.open(name, ios::out);
}
 
you have to use fstream::clear() after a failure before using the stream object again.

-pete



 
okay, this is what i want to do. if the file doesn't exist, i want to create it and input from user. if the file already exists, i want to read from the file, that's why i had the open(name, ios::in|ios::eek:ut|ios::nocreate) then the open(name, ios::in|ios::eek:ut)
 
airswit:

The problem here is that you are not clearing the failed bit
after the first failure with 3 flags. This needs to be reset manually for the stream in concern. After you do that, an existing file can be opened with just in &/or out mode. So here is the corrected code:
-----------------------------------
void loadstats()
{
fstream input;
char name[]=&quot;save.txt&quot;;
input.open(name, ios::in|ios::eek:ut|ios::noreplace);
if (input.fail())
{
input.clear();
input.open(name, ios::in|ios::eek:ut);
if (input.fail())
cout<<&quot;this shows up at runtime&quot;;
}
else
...
...
-----------------------------------------
Hope this helps.

Rahul
 
thanks rahulc, that worked nice. thanks a bunch again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top