By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

c++ file i/o: cant use file twice

P: n/a
Hi,

When I open a file for the second time (sequentially) i get the error
error: junk after document element

tA one-time opening and use of the file works fine, it's the second time
where something goes wrong. The code basically looks like this:

ifstream in(path.c_str());

assert(in);
string line;

while (getline(in, line))

{

// do stuff

};

// in.clear();

// in.close();

I have used in.clear() and closed() with no success... If i call the loading
routine the second time, i get the error. Does anyone have any ideas what
the trouble might be?

Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a


Jeremy Bishop wrote:

Hi,

When I open a file for the second time (sequentially) i get the error
error: junk after document element

tA one-time opening and use of the file works fine, it's the second time
where something goes wrong. The code basically looks like this:

please post the exact code.
Strip all unneccessary and not related stuff. It would be ideal, if you
could come up with a small, complete, compilable program which demonstrates
your problem.
ifstream in(path.c_str());

assert(in);

string line;

while (getline(in, line))

{

// do stuff

};

// in.clear();

// in.close();

I have used in.clear() and closed() with no success... If i call the loading
routine the second time,
From this I conclude that the above is in a function, which is
called 2 times. The first time it works, the second time it doesn't?
Correct ?
i get the error.
which error?
Where is the error detected? I can't see anythting in you 'code' above.
Does anyone have any ideas what
the trouble might be?

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #2

P: n/a
try using
ifstream( path.c_str(), ios::in, filebuf::sh_read );

I have not tried it but the documentation says that this allows file
sharing.

Karl Heinz Buchegger wrote:

Jeremy Bishop wrote:
Hi,

When I open a file for the second time (sequentially) i get the error
error: junk after document element

tA one-time opening and use of the file works fine, it's the second time
where something goes wrong. The code basically looks like this:

please post the exact code.
Strip all unneccessary and not related stuff. It would be ideal, if you
could come up with a small, complete, compilable program which demonstrates
your problem.

ifstream in(path.c_str());

assert(in);

string line;

while (getline(in, line))

{

// do stuff

};

// in.clear();

// in.close();

I have used in.clear() and closed() with no success... If i call the loading
routine the second time,

From this I conclude that the above is in a function, which is
called 2 times. The first time it works, the second time it doesn't?
Correct ?

i get the error.

which error?
Where is the error detected? I can't see anythting in you 'code' above.

Does anyone have any ideas what
the trouble might be?



Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.