Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to open a file w\o memo1

Status
Not open for further replies.

cyprus106

Programmer
Apr 30, 2001
654
hey! i need to open a file, read a string from it, save it and close it... i know ABOUT using fopen, but dont really know the procedure.
what i really need to do is save. i need to save a line from edit1->Text to a file, but i cant put it into a memo and then save it, like i did before. i REALLY need help cause i dont even know where to start.

thanks in advance! Cyprus
 
[tab]There are numerous ways to do this. My favorite is to use iostreams. You will need to add
Code:
#include <fstream>
after
Code:
#pragma hdrstop
at the top of your file. Then when you need to open the file for reading by doing something like this:
Code:
ifstream MyFile; // stream set up
MyFile.open(&quot;\Mydirectory\MyFile.txt&quot;); // opening file for reading

if (!MyFile)
{
    // Something went wrong since the stream didn't open
    Application->MessageBox(&quot;Could not open file for processing!&quot;, &quot;File Error&quot;, MB_OK);
}
else
{
    string Something; // variable to hold what is in the file
    MyFile >> Something;
    // Process Something
}

MyFile.close();

You could also use getline instead of >>. A quick word of caution: There is a bug in BCB 5 and Borland 5.5. Get the patch before using getline! With getline, you could get the whole file in a while loop.
Code:
// get each line for processing
while (getline(MyFile, Something, '\n'))
{
    // Process Something here
}

[tab]For output you call ostream instead of istream. Any good book on the standard libraries can help.

James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top