what's the wrong with my code C++?

hello every one,
my code is to read a file and inserting it to a list and type it to a nother file my problem is with files when opening it it already be opened but i reads no thing from the file and i don't know why?
i want to know the reason.
Thanks.
my code:
#include"ordering.h"

using namespace std;

int main()
{
ordering A;
ifstream in;
bool flag;
string line;
in.open("C:\\infile.txt");
while(! in.eof());
{getline(in,line);
A.inorder_insertion(line);
}
in.close();
ofstream out;
out.open("C:\\outfile.txt");
flag=A.inorder_first(line);
while(flag){
out<<line<<endl;
flag=A.inorder_next(line);}
out.close();

return 0;
}
Last edited on
"C:\\infile.txt"
It's not infile.txt located on disc C, but file named "C:\\infile.txt".
Next thing is, that getline function have construction like this: getline(char * string, size_of_stream, char a). So you should change it: getline(line, (for example) 50); where characters are extracted until 50-1 characters have been extracted or, if you character a is found (if it's not defined, character a='\n').
Last edited on
mtweeman wrote:
It's not infile.txt located on disc C, but file named "C:\\infile.txt".

What?
I don't understand your question, where is the problem in your piece of code? If you use the same std::ifstream object to open/read/close multiple files, you shouldn't forget to call std::ifstream::clear() to reset the EOF flag each time. Did you mean that? Btw. you can use the std::getline() function directly in the loop:

1
2
3
4
while (std::getline(in, line))
{
    A.inorder_insertion(line);
}


@mtweeman There is also a std::getline() function, which is NOT the std::istream::getline() method. So the code is correct.
@mtrenkmann, to be honest - i haven't know about it :D

@chrisname, your wrote C:\\infile.txt. It's not a path of this file, but it's treated by comp. as a name of .txt file.
mtweeman wrote:
your wrote C:\\infile.txt.

I didn't write C:\\infile.txt.
It's not a path of this file, but it's treated by comp. as a name of .txt file.

That's wrong. Windows will see C:\infile.txt. It will try to find infile.txt on path C:\
Topic archived. No new replies allowed.