Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Jeremy Bishop
Guest
 
Posts: n/a
#1: Jul 19 '05
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?




Karl Heinz Buchegger
Guest
 
Posts: n/a
#2: Jul 19 '05

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




Jeremy Bishop wrote:[color=blue]
>
> 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:
>[/color]

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.
[color=blue]
> 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,[/color]

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 ?
[color=blue]
> i get the error.[/color]

which error?
Where is the error detected? I can't see anythting in you 'code' above.
[color=blue]
> Does anyone have any ideas what
> the trouble might be?[/color]


--
Karl Heinz Buchegger
kbuchegg@gascad.at
amit gulati
Guest
 
Posts: n/a
#3: Jul 19 '05

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


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:[color=blue]
>
> Jeremy Bishop wrote:
>[color=green]
>>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:
>>[/color]
>
>
> 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.
>
>[color=green]
>>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,[/color]
>
>
> 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 ?
>
>[color=green]
>>i get the error.[/color]
>
>
> which error?
> Where is the error detected? I can't see anythting in you 'code' above.
>
>[color=green]
>> Does anyone have any ideas what
>>the trouble might be?[/color]
>
>
>[/color]

Closed Thread