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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read File

Status
Not open for further replies.

LoneRanger123

Programmer
Dec 19, 2000
62
GB
How do I read the contents into a file, or more to the point, what am I doing wrong? Here is my code

Code:
//HEADERS.....
...
#include <iostream.h>
#include <fstream.h>
...

//MOVING ONTO MAIN CODDE (IN LOAD SECTION OF PROGRAM)

            ifstream infile;
            String Hello;
            char Temp[255];
  infile.open (&quot;C:\\Hello.txt&quot;, ifstream::in);

        infile.read(Hello.c_str(),255);


                  MessageDlg(Hello,mtError,TMsgDlgButtons() << mbOK,0);

I have made sure the file is there, ATM it returns a very wide but Blank Message Box. Is there anything wrong there,
The File contains &quot;Hello!&quot;.

Making the file longer (lots of d's in it) and running gives an error while running
&quot;Assertion Failed: dttPtr->dttFlags & (DTCVF_PTRVAL|DTCVF_RETVAL ),file xx.cpp, line 3735&quot; In Debug Mode
 
I'm not familiar with the read function but I suspect that is the problem. Here is an example of how I like to read files:
Code:
// Set up streams
std::ifstream InFile;

// Open input file for read only
InFile.open(&quot;C:\\Hello.txt&quot;);

if (!InFile)
{
    // Something went wrong
    Application->MessageBox(&quot;Could not open file!&quot;, &quot;File Error&quot;, MB_OK);
}
else
{
    std::string ImpLine= &quot;&quot;;
    // Get input from file
    while (std::getline(InFile, ImpLine, '\n'))
    {
	// Process input string here
    }
}

The getline gets data from the input file to the next new line character and puts it into the string. The while loop keeps getting the data until the end of file. 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.
 
What is type String, and how can I use that or convert that to AnsiString ?

when using the MessageDlg Part it says cannot convert string to AnsiString.

is there a command to copy it? strcpy(AnsiSting,String) wont work.

ty m8 :)
 
Just make some corrections:

ifstream infile;
String Hello;
char Temp[255];
infile.open (&quot;C:\\Hello.txt&quot;, ifstream::in);

infile.read(Temp,255); //!!!!!!!
Hello = Temp; // !!!!!!


MessageDlg(Hello,mtError,TMsgDlgButtons() << mbOK,0);
 
BTW, String and AnsiString are the same thing. They are both implimentations of Delphi (Pascal) strings. string (lowercase s) is C++ strings. The bigest diffrence between the two is that strings are zero-based (they start at 0, i.e., string[0] is first character) while Delphi strings are one-based (i.e., string[1] is first character). To convert between the two, use c_str().
Code:
String OneString = &quot;A string&quot;;
string ZeroString = OneString.c_str();
.
.
.
ZeroString = &quot;Another string&quot;
OneString = ZeroString.c_str();

Most of Builder's components use Strings while C++ uses strings.

A big advantage that strings have over character arrays is that you, in theory, can't have a buffer overrun with strings. A buffer overrun occurs when you try to put more characters into an array than the array can handle.
Code:
char Temp[12];
.
.
.
infile.read(Temp,255); // OUCH!!!

Another advantage is the =, +, etc. are overloaded (can be used with) for strings and Strings. 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