C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

where does open open the file?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Gunnar G
Guest





PostPosted: Sat Jan 21, 2006 8:13 pm    Post subject: where does open open the file? Reply with quote



I'm having problem reading from the beginning of a file.
Here is the code (more or less)




ifstream codefin;
ofstream codefout;

while (not_annoyed)
{
codefout.open("bar");
// write to bar, works fine
codefout.bar();
system("command to change bar to foo");

codefin.open("foo");
if (!codefin.is_open())
cout<<"ERROR: couldn't open foo for reading"<
cout< codefin.seekg(0,ios::beg);
cout<
if (codefin.eof())
cout<<"FRACK!n";
while (!codefin.eof())
getline(codefin,text);
codefin.close();
}


The files foo and bar are there and they look alright, but when I run the
program, at the second run I get the FRACK output. The file foo is not
opened in the beginning. I add the seekg/tellg commands to see, but there
is nothing but -1 and -1 printed.

What have I done wrong?

Back to top
Victor Bazarov
Guest





PostPosted: Sat Jan 21, 2006 8:28 pm    Post subject: Re: where does open open the file? Reply with quote



Gunnar G wrote:
Quote:
I'm having problem reading from the beginning of a file.
Here is the code (more or less)




ifstream codefin;
ofstream codefout;

while (not_annoyed)
{
codefout.open("bar");
// write to bar, works fine
codefout.bar();

You mean,

coutfout.close();

Quote:
system("command to change bar to foo");

codefin.open("foo");
if (!codefin.is_open())
cout<<"ERROR: couldn't open foo for reading"<
cout< codefin.seekg(0,ios::beg);
cout<
if (codefin.eof())
cout<<"FRACK!n";
while (!codefin.eof())
getline(codefin,text);
codefin.close();
}


The files foo and bar are there and they look alright, but when I run
the program, at the second run I get the FRACK output. The file foo
is not opened in the beginning. I add the seekg/tellg commands to
see, but there is nothing but -1 and -1 printed.

What have I done wrong?

I am not sure. Post _real_ code, *complete* and *compilable*. Even
though 'system' is OS-specific, you could simply explain what you mean
by "change bar to foo". Rename? Why not use 'std::rename'? Anyway...

V



Back to top
Gunnar G
Guest





PostPosted: Sat Jan 21, 2006 8:39 pm    Post subject: Re: where does open open the file? Reply with quote



Quote:
I am not sure. Post _real_ code, *complete* and *compilable*.

Here it is then.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;

int main(int argc, char* argv[])
{
if (argc!=2) {
cout<<"Usage: "< return 0;
}

bool codemode=false; // found code?
string line;
string texline;
string tmp="/tmp/.Big_secret_I_love_emacs_and_I_hate_vim.cpp";
string tmptex=tmp+".tex";
string command="highlight -A -L -t 4 -I -f -r -q -i "+tmp+" -o "+tmptex;

vector vector<string> latex_out; // the resulting latex file
string codefile;
ofstream codefout;
ifstream codefin;
ifstream fin(argv[1]);
if (!fin.is_open())
{
cout<<"Could not open the file "< return -1;
}
cout<<" Opening "< while (!fin.eof())
{
getline(fin,line);
if (line!="\begin{KODcpp}" && line!= "\end{KODcpp}")
{
if (!codemode)
latex_out.push_back(line);
else
{
// write to the file
if (line[line.size()-1]=='\' && line[line.size()-2]=='\')
line=line.substr(0,line.size()-2);
codefout< }
}
else
{
if (!codemode)
{ // open file and write
cout<<" Creating code in "< codefout.open(tmp.c_str());
if (!codefout.is_open())
{
cout<<"ERROR: couldn't open "< }
latex_out.push_back("\begin{KODcpp}");
codemode=true;
}
else
{ // close file and run highlight and then insert it
codefout.close();
codemode=false;
system(command.c_str());
system("cat /tmp/.Big_secret_I_love_emacs_and_I_hate_vim.cpp");
cout< system("cat /tmp/.Big_secret_I_love_emacs_and_I_hate_vim.cpp.tex");
codefin.open(tmptex.c_str());
if (!codefin.is_open())
{
cout<<"ERROR: couldn't open "< }
cout< codefin.seekg(0,ios::beg);
cout< if (codefin.eof()) cout<<"SHIT! What is this crap Im doing?n";
while (!codefin.eof())
{
getline(codefin,texline);
cout<<"Read :"< latex_out.push_back(texline);
}
codefin.close();
latex_out.push_back("\end{KODcpp}");
}
}

}
fin.close();

ofstream fout(argv[1]);
if (!fout.is_open())
{
cout<<"Could not open the file "< return -1;
}
cout<<" Writing "< for (unsigned int i=0;i fout< fout.close();
return 0;
}

-------------------------------------------------------------------------
The input file is a latex file that contains

begin{KODcpp}
.... INSERT c++ code HERE...
end{KODcpp}

at different places, not nested.


Back to top
red floyd
Guest





PostPosted: Sat Jan 21, 2006 8:58 pm    Post subject: Re: where does open open the file? Reply with quote

Gunnar G wrote:
Quote:
I am not sure. Post _real_ code, *complete* and *compilable*.

Here it is then.
int main(){

[redacted]
vector<string> source;
vector<string> latex_out; // the resulting latex file
string codefile;
ofstream codefout;
ifstream codefin;
ifstream fin(argv[1]);
if (!fin.is_open())
{
cout<<"Could not open the file "< return -1;
}
cout<<" Opening "< while (!fin.eof())
{
getline(fin,line);
[loop body redacted]
}
fin.close();

[remainder of function redacted]
}


Your loop is incorrect. Please see FAQ 15.5
([url]http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5)[/url].

fin.eof() only returns true *AFTER* an attempt to read past EOF. So you
will attempt to read an extra line. Your while loop should read:

while (getline(fin,line))
{
// body of loop.
}

This will exit on EOF.

Back to top
Gunnar G
Guest





PostPosted: Sat Jan 21, 2006 9:13 pm    Post subject: Re: where does open open the file? Reply with quote

Quote:
Your loop is incorrect. Please see FAQ 15.5
([url]http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5)[/url].
Thanks, I'll look right away,


But the problem seems to be that eof() is still true after closing the file
and opening it again.
So open() just openes the file, nothing else.

A call to clear() solved things.

Back to top
David Harmon
Guest





PostPosted: Sat Jan 21, 2006 9:37 pm    Post subject: Re: where does open open the file? Reply with quote

On Sat, 21 Jan 2006 21:13:05 GMT in comp.lang.c++, Gunnar G
<debian (AT) comhem (DOT) se> wrote,
Quote:
But the problem seems to be that eof() is still true after closing the file
and opening it again.

After an operation fails, whether because of eof or some other
reason, the stream is in a failed state and will do nothing more for
you until you .clear() it.


Back to top
red floyd
Guest





PostPosted: Sun Jan 22, 2006 2:38 am    Post subject: Re: where does open open the file? Reply with quote

David Harmon wrote:
Quote:
On Sat, 21 Jan 2006 21:13:05 GMT in comp.lang.c++, Gunnar G
[email]debian (AT) comhem (DOT) se[/email]> wrote,
But the problem seems to be that eof() is still true after closing the file
and opening it again.

After an operation fails, whether because of eof or some other
reason, the stream is in a failed state and will do nothing more for
you until you .clear() it.


Yes, but I believe that this is a deficiency in the standard. .close()
followed by .open() should clear any previous state.

Back to top
Mike Wahler
Guest





PostPosted: Sun Jan 22, 2006 6:06 am    Post subject: Re: where does open open the file? Reply with quote


"red floyd" <no.spam (AT) here (DOT) dude> wrote

Quote:
David Harmon wrote:
On Sat, 21 Jan 2006 21:13:05 GMT in comp.lang.c++, Gunnar G
[email]debian (AT) comhem (DOT) se[/email]> wrote,
But the problem seems to be that eof() is still true after closing the
file
and opening it again.

After an operation fails, whether because of eof or some other
reason, the stream is in a failed state and will do nothing more for
you until you .clear() it.


Yes, but I believe that this is a deficiency in the standard. .close()
followed by .open() should clear any previous state.

If a stream is in a 'fail' state, I don't believe
'close()' will succeed.

-Mike



Back to top
red floyd
Guest





PostPosted: Sun Jan 22, 2006 10:26 pm    Post subject: Re: where does open open the file? Reply with quote

Mike Wahler wrote:
Quote:
"red floyd" <no.spam (AT) here (DOT) dude> wrote in message


Yes, but I believe that this is a deficiency in the standard. .close()
followed by .open() should clear any previous state.

If a stream is in a 'fail' state, I don't believe
'close()' will succeed.

-Mike



EOF is a fail state. Why should it fail to close when you've reached EOF?

Back to top
Mike Wahler
Guest





PostPosted: Sun Jan 22, 2006 11:13 pm    Post subject: Re: where does open open the file? Reply with quote


"red floyd" <no.spam (AT) here (DOT) dude> wrote

Quote:
Mike Wahler wrote:
"red floyd" <no.spam (AT) here (DOT) dude> wrote in message
Yes, but I believe that this is a deficiency in the standard. .close()
followed by .open() should clear any previous state.

If a stream is in a 'fail' state, I don't believe
'close()' will succeed.

-Mike



EOF is a fail state.

Right.

Quote:
Why should it fail to close when you've reached EOF?

Because, as I understand it, once 'good()' returns 'false',
any other operation on the stream will fail, including
'close()'.

There might be exceptions of which I'm unaware, but my
experience has been that when 'eof()' returns 'true',
so will 'fail()' (this makes sense to me because triggering
'eof()' results from an attempt to read past eof, which
would mean the read operation failed, setting 'failbit'.

-Mike



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.