473,503 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
2 2415


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
8720
by: Jennifer Smith | last post by:
Currently we have a site that allows users to listen to mp3 files. It is creating bandwidth issues. So we want to move the mp3 files to an ISP that caps bandwidth usage. Ours is currently...
33
2789
by: Jason Heyes | last post by:
I would like to modify the contents of a file, replacing all occurances of one string with another. I wrote these functions: bool read_file(std::string name, std::string &s); bool...
9
16181
by: W. Van Hooste | last post by:
Just starting with C, can somebody explain why this does not work or point me in the right direction? I wrote some tools and did some coding but cant seem to get this one. I DID declare my FILE...
8
4040
by: noleander | last post by:
I've got two threads reading from one disk file (I'm using the Pthread library). I need each thread to have its own file seek pointer, so they can read from independent parts of the file. I...
7
3685
by: theyas | last post by:
How can I get my code to NOT display two "Open/Save/Cancel/More Info" dialog boxes when using the "Response.WriteFile" method to download a file to IE I've asked about this before and didn't get a...
9
2207
by: AFN | last post by:
I was just dropped into someone else's code (isn't that always so fun?). I can't figure out why a custom validation control's server event function is executing. There is nothing (that I see)...
2
5025
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified...
0
2610
by: Mark Ingram | last post by:
Hi, i want to ensure that an xml file has been created properly, but i dont want to rely on the creator of the xml file to correctly enter the required namespaces (without the correct namespaces...
15
3717
by: Amir Michail | last post by:
Hi, Trying to open a file for writing that is already open for writing should result in an exception. It's all too easy to accidentally open a shelve for writing twice and this can lead to...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.