-
July 30th, 2010, 03:24 AM
#1
reading from file using a function problem
Code:
void Lines::load(string filename)
{
string lines;
ifstream inFile;
inFile.open(filename.stx);
if (!inFile)
{
cout<<"error";
}
while (!inFile.eof())
{
while(inFile.eof() != '/0')
{
getline(inFile,lines);
}
}
i have a function : load(string filename)
which performs the read file function the "string filename" is the name of the file i input thought "cin", but the function can't seem to be working properly in inFile.open(filename.stx).
any help would be appreciated
-
July 30th, 2010, 03:52 AM
#2
Re: reading from file using a function problem
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
July 30th, 2010, 04:46 AM
#3
Re: reading from file using a function problem
thanks for reply monarch, the .stx is the extension of the file that i'm supposed to open...
i've included the .c_str() but still couldn't get it working :'(
-
July 30th, 2010, 05:28 AM
#4
Re: reading from file using a function problem
Don't confuse the string object, and the character array.
filename is a string. filename.c_str() is a character array.
filename should already contain "youfilename.ext". If it doesnt contain that, than you can do this:
Code:
filename+=".stx";
ifstream inFile;
inFile.open(filename.c_str());
Last edited by monarch_dodra; July 30th, 2010 at 05:37 AM.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
July 30th, 2010, 07:21 AM
#5
Re: reading from file using a function problem
thanks alot monarch, you've been a great help =)
i've managed to read the whole file and print it out using now, but i still have a question.
Assuming my files have the words => "Hi how are you doing."
i have the 5 string variables:
string a,b,c,d,e;
i wanna input from the file Hi to a, how to b are to c and so on, how do i do so?
-
July 30th, 2010, 07:56 AM
#6
Re: reading from file using a function problem
your file is a stream, so it works the same ways as cin and cout, so...
Code:
myFile >> a;
myFile >> b;
myFile >> c;
myFile >> d;
myFile >> e;
if ( !myFile ) {std::cout << "an error occured" << std::endl;}
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
July 30th, 2010, 07:58 AM
#7
Re: reading from file using a function problem
in >> a >> b >> c >> d >> e
Also, your eof-checking can be simplified:
Code:
while (getline(inFile,lines))
{
...
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|