-
Read/open file
I know I'm a little slow but what is the difference between a readfile and infile.open. I think I know the answer but Here is what I'm trying to do: I want to open a file that has data in it then I need take a hunk of the data and do a cout.
I think I want to do a readfile(text.tx) then infile.open(text.txt) or something......please help me cuz this book is not helping me!:confused:
-
I'm not clear about what exactly you mean by a "readfile" and "infile.open", but I'll just tell you a little bit about opening a file with an ifstream object.
You can open the file either of two ways:
Code:
//1:
ifstream fin("text.txt");
//2:
ifstream fin;
fin.open("text.txt");
Hth.
-
What is the difference?
Why would I do one vs the other?
-
Well the answer is quite simple. Consider if you were to write an app that accesses more than a single file at different times.
Code:
// good
ifstream fin("fileA.txt");
//...
fin.close(;
fin.open("fileB.txt");
//...
fin.close();
// not so good
ifstream fin("fileA.txt");
//...
fin.close();
// do stuff
ifstream finAgain("fileB.txt");
//...
finAgain.close();
This is a very simple example that you may not encounter, but consider the case where the ifstream object is a data member of your class. You could explicity open the file with the constructor of your parent class, but what if you want to ask the user for the name of the file? Then it's only possible to call open().
In short, there is no difference between using the constructor and using open(). It's just a matter of convenience in your app.
(Side Note: Keep in mind, however, that if you open a file after closing a different one with the same object, you should insure that it is .good() first.)
-
I think what Lucky is explaining might be a bit to complicated for the level at which the question is at. This is what I do to open a file:
Code:
ifstream inStream;
inStream.open(textfile.txt); //opens file in inStream
//check if file opened correctly:
if ( inStream.fail() ){
cout << "error opening file....";
exit(-1);
}
hope this helps...also look at the FAQ it has a section about I/O with files,
axon
-
Greetings.
I'm also a beginner too, but I found some handy functions in C++.
These are FileOpen, FileCreate, etc. I think if you only want to read from a text file into a string you should use something like this.
Code:
int iFileHandle;
try { iFileHandle = FileOpen(filename, fmOpenRead);
}
catch(...)
{ Showmessage("Error");
goto end; //weird huh ? :)
}
iFileLength=FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0); //go to the beginning of the file
buffer=new char[iFileLength];
FileRead(iFileHandle,buffer,iFileLength); //reads the file onto the buffer
It is like this. The buffer has to be a specific type (I don't remember it just now), I think void *char, or something like this, but from the buffer you can put your stream into a String one by one in a for cycle.
I do it this way, and it works fine. However in the recent past I have the problem that it reads an extra " ' " character to the end of the buffer. I don't know why it does this, but I'm on solving this little piece of problem.
Good luck!
Han
-
I am doing it like this:
Code:
char get;
ofstream file_ptr;
file_ptr.open(file, ios::out);
while(file_ptr.get(get))
{cout << get;}
file_ptr.close();
Hope that was what you was looking for :)