| View previous topic :: View next topic |
| Author |
Message |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Tue Apr 01, 2008 10:36 pm Post subject: FI/FO(file input/file output) problem/question |
|
|
I decided to try to learn file input/file output(FI/FO) and so I was reading about it and made a test program:
| Code: |
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char filename[80];
char buffer[255];
cout<<"File name: ";
cin>>filename;
ifstream fin(filename);
if (fin) {
cout<<"Current file contents: \n";
char ch;
while(fin.get(ch)) {
cout<<ch;
}
}
fin.close();
cout<<"Opening file...\n";
ofstream fout(filename, ios::ate);
if (!fout) {
cout<<"Could not open "<< filename <<" for writing.\n";
return(1);
}
cout<<"Text to be written: \n";
cin.ignore(1, '\n');
cin.getline(buffer, 255);
fout<< buffer << "\n";
fout.close();
fin.open(filename);
if (!fin) {
cout<<"Could not open "<< filename <<" for reading.\n";
return(1);
}
cout<<"Contents of file: \n";
char ch;
while(fin.get(ch)) {
cout<<ch;
}
fin.close();
return 0;
}
|
But when I compile and run this, as soon as I type it what I want to write and click enter, my program shuts down. And my compiler isn't giving me any warnings or errors or anything. I can't really find the problem. Can you guys help? _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
WafflesFTW Expert Cheater
Reputation: 0
Joined: 21 Mar 2008 Posts: 131
|
Posted: Wed Apr 02, 2008 8:18 pm Post subject: |
|
|
| Most likely because your program executes very fast. If you have MSVS 2k5 go to debug-> start without debugging. Tell me if it still doesn't work. If you don't have MSVS 2k5 add cin or system("pause") at the end. |
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Wed Apr 02, 2008 8:27 pm Post subject: |
|
|
no system pause, use cin:
cin.sync();
cin.ignore();
before return 0; _________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Wed Apr 02, 2008 9:22 pm Post subject: |
|
|
I've never had that happen before. I remember before, on short like 8 line programs when I was beginning to learn C++, like hello world and stuff it would happen. But I never experienced with anything that was somewhat lengthy (in terms of a simple program).
edit:
It still isn't working. It will write to the file fine, and save the file, just not, display its contents. Any other reasons why it's closing before it displays the contents? _________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
HalfPrime Grandmaster Cheater
Reputation: 0
Joined: 12 Mar 2008 Posts: 532 Location: Right there...On your monitor
|
Posted: Wed Apr 02, 2008 9:40 pm Post subject: |
|
|
try
| Code: |
while(!fin.eof()) {
cout<<fin.get();
}
|
Also, try setting up messageboxes or random couts to see where your program is and isn't executing. |
|
| Back to top |
|
 |
|