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

J

Jeremy Bishop

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?
 
K

Karl Heinz Buchegger

Jeremy said:
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.
 
A

amit gulati

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top