-
Handling with files
Hi!
I can't understand what is wrong to my code:
#include <fstream>
using namespace std;
int main() {
char c, inputString[10000];
int i=0;
ifstream infile("in.txt");
while (infile.get(c)) inputString[i++]=c;
infile.close();
infile.open("out.txt");
if (!infile.good()) return 0;
system("Pause");
return 0;
}
The problem is that this program can not open file "out.txt" (such file exists on disc) and so returns at line `if... return 0`. If I remove line `while...` from the code, everything works fine (file "out.txt" can be opened). Why is it so??
-
I think the proper way for checking for opening errors is:
Code:
infile.open("c:\\2.txt");
if(! infile.is_open()) return 0;
I hope this helps.
Last edited by Viorel; 12-29-2006 at 10:11 AM.
-
OK, this helps in this situation which I made as small as possible, but not in my real situation.. Now I have such code:
#include <fstream>
#include <iostream>
using namespace std;
int main() {
char c, inputString[10000];
int i=0;
ifstream infile("in.txt");
ofstream outfile("out.txt");
for (int j=0;j<10000;j++) inputString[j] = 0;
while (infile.get(c)) inputString[i++]=c;
outfile << inputString;
outfile.close();
infile.close();
infile.open("out.txt");
if (!infile.is_open()) return 0;
outfile.open("res.txt");
for (int j=0;j<10000;j++) inputString[j] = 0;
while (infile.get(c)) {
cout <<c;
inputString[i++]=c;
}
outfile <<inputString;
infile.close();
outfile.close();
system("Pause");
return 0;
}
And now the problem is as follows - when cheching if infile is open, everything is ok, infile is open and we go further. But we never go into this second while loop.. althouht there are some simbols written in file "out.txt". I think maybe it thinks we have already reached end of infile.. but how can that be, we have just opened this file.. The correct action would be going into this second while loop and printing out one by one all characters in file "out.txt".
-
OK, I found a solution - I had to write "infile.clear()" before reopening it.
Similar Threads
-
Replies: 5
Last Post: 08-01-2006, 06:42 PM
-
By Mansoor in forum Enterprise
Replies: 2
Last Post: 09-10-2002, 10:38 PM
-
Replies: 8
Last Post: 08-23-2002, 04:35 PM
-
By Pat Meaney in forum Java
Replies: 0
Last Post: 03-13-2001, 01:54 PM
-
By René Whitworth in forum vb.announcements
Replies: 1
Last Post: 04-29-2000, 02:54 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks