Borland C++ Builder File I/O

I need to read in a text file line by line, lines are of a variable length.
I'm using Borland C++ Builder 4.
IceyAsked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
FYI.  By using a string object to store each string read, you never need to worry about the length of a line.  The string will automatically resize to the length needed to hold the line.   And it almost always makes it easyier to process the line.

some example code:

using namespace std;

fstream Fil("C:\\SOMEFILE.TXT",ios::in);
string Str;

while (!Fil.eof()) // While not at the end of the file.
{
   getline(Fil,Str);
   // Use the line.
};
0
 
KangaRooCommented:
fgets()
istream::getline()
0
 
IceyAuthor Commented:
Alittle more detail please
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
nietodCommented:
I dissagree with both of those approaches--to some extent.

 I would recommend you use an fstream object and read the lines into a string object (STL string) using the _global_ getline()  procedures.

continues.
0
 
KangaRooCommented:
Pfoei, they did hide that function very well :(
Much better, use of string is recommended for this.
0
 
IceyAuthor Commented:
Alittle more detail please
0
 
nietodCommented:
Icey, what more do you want to know?  That is all you need to read every line from a file.  I can't tell you more because I don't know what else you want to do.

Kangaroo, you didn't know about ::getline()?
0
 
IceyAuthor Commented:
Alittle more detail please
0
 
nietodCommented:
What more do you want?
0
 
IceyAuthor Commented:
Alittle more detail please
0
 
IceyAuthor Commented:
Alittle more detail please
0
 
IceyAuthor Commented:
Nietod

You are arguing with a glitch in the sites programming, I wrote that message once it just seems to repeating itself.

I'm trying your answer right now.
0
 
IceyAuthor Commented:
Nietod

You are arguing with a glitch in the sites programming, I wrote that message once it just seems to repeating itself.

I'm trying your answer right now.
0
 
IceyAuthor Commented:
I implemented the code exactly as you have it and it's not even opening the file. Is there something you omitted from the code assuming that I knew it should be there. Warning I am very new to C++.
I keep getting :
E2027 Must take address of a memory
      location

in the EOF test.
0
 
nietodCommented:
Got it.  the problem's not in EE, however.  Your browser is resubmitting each time you come to the page.  I can't remember what causes that.

You do need to include the header files for fstream and string, like

#include <fstream>
#include <string>

If that doesn't help, post your code.
0
 
IceyAuthor Commented:
Got it working now for another 200 points for a total of 500 :

I need to add that string to a ListBox but everytime I compile I get

E2034 Cannot convert 'string' to 'AnsiString'.

E2343 Type mismatch in parameter 'S' in call to '_fastcall TStrings::Add(const AnsiString)'.

0
 
andlaCommented:
If you now what 'main' are you probebly know that you can write

int main(void)
{
}

This is the birth of the first function called in 'C' written code. If you compile this code written in you editor bounded to your compiler, you will have shortest c-program that is possible to build and it does nothing yet.

Next step is to make the function take argument from the DOS (the black window)

int main(int argc, char *argv[])
{
}

This makes you acess number of argument(argc) and a list of strings containing the arguments you wrote.

If you write
c:\myprogram arg1 arg2 arg3

You can access it in main like this


int main(int argc, char *argv[])
{
int NumberOfArguments=argc;//3
char *string1=argv[0];//arg1
char *string2=*(argv+1);//same as argv[1]
char *string3=(argv+2)[0]//same as =*(argv+2);
}

If you know about "c:\directory\>cd.." or "c:\>cd directory" you can say that cd.. is like '&' and cd directory is like '*' and that means that you use * to access the contentlevel and & to step out of the content level.

Now if you almost understod what i meant you can try to access the ability to read a file. Before you do that you have to learn about headerfiles.

What is a header file?
basically you can make the program do alot more. What you really do is simply implement code right where you include file are. It's like copy text from notepad and replace the include file in wordpad (then you don't need any include file) But to make the code look clean lets use the include file.

How does a headerfile look like?
Ha ha very simple:
#include <compilerfile.h>
or
#include <myownfile.h>

Note! these two files don't exist it's only to show what the difference of " and <> are.

Now you want your program be more powerfull than superman. And you want you program to read text in superspeed.

simple write code with an include file like this.

#include <fstream.h>
#include <iostream.h>
int main(int argc, char *argv[])
{
}

Now you probebly see that you file is bigger than before. Thats because you have the ability to read and write to file and read from keyboard and write to monitor. Now you probebly asks pleeeeasee tell me how to read a file

Ok i tell you.


#include <fstream.h>
#include <iostream.h>
int main(int argc, char *argv[])
{
fstream obj;
                        obj.open(argv[0],ios::in|ios::app);

                  
                        if(obj.fail())
                              {
return -1;//**** happends            }
                        obj.seekp(0,ios::end);//go to end of file.
                        filelen=obj.tellp();//Please tell me your position.
                        obj.seekp(0,ios::beg);//go to beginning of file.
                        char *filetext=new char[filelen+2];//give memory.
                        //obj>>filetext; //This doesn't read the whole file only to next space.
obj.get(filetext,filelen+1); //Time to read whatever;

cout<<filetext;

}

-------------------
This program is not tested but i think it works if it doesn't work you can try to fix it and if you can't find the error i can take a look if you send me a comment.

Your sincerely
andla.
0
 
andlaCommented:
Comment about>>
E2034 Cannot convert 'string' to 'AnsiString'.

E2343 Type mismatch in parameter 'S' in call to '_fastcall TStrings::Add(const AnsiString)'.

Try to convert the string with the * or & that i told you about. And look if you have any funtions that can convert a normal string to ansicoded string. I'm not very familiar with ansistring but you should have look for string manipulations function or methods. Also try to look in www.codeguru.com to get more help.
0
 
IceyAuthor Commented:
I'm sorry andla but neotid had all readt answered the question but thank you anyway
0
 
andlaCommented:
I found these functions. Hope they will be helpful.

extern "C" char FAR*
ConvertStrWtoA(OLECHAR FAR* strIn, char FAR* buf, UINT size)
{
  int badConversion = FALSE;
   
  WideCharToMultiByte(CP_ACP, NULL,  
              strIn, -1,  
      buf, size,  
      NULL, &badConversion);
  return buf;
}
 
extern "C" char FAR*
AnsiString(OLECHAR FAR* strIn)
{
  static char buf[256];
   
  return (ConvertStrWtoA(strIn, buf, 256));
}
0
 
andlaCommented:
Ooops sorry.
0
 
nietodCommented:
>> E2034 Cannot convert 'string' to >> 'AnsiString'

I'm not sure what an AnsiString is, can you find a definiton for it?

In any case, you will probably need to convert the string inside the string class to a char * string (C-type string), this is done with the c_str() member function, like

const char *CStrPtr = Str.c_str().

Then this can be converted to an AnsiString.  (There is a good chance that conversion will be automatic, like if you do

AnsiString AS;

AS = CStrPtr;

You might try that.  Otherwise, see what you can find about this AnsiString.
0
 
andlaCommented:
I was thinking of that the listbox complain because you use a normal ansi string (char[]) and it wants something like wide character or a list of strings (char[][]) ??
0
 
nietodCommented:
The error message is clear and does not agree with that.  it says he is using a type "string", which makes sense since that is what I suggested, it says it cannot convert it to type AnsiString.  He is not using an char [] and he does not need to convert it to a char[][].  
0
 
nietodCommented:
I found AnsiString in the BCB docs.  You can construct an ANSI string from a "char *" style (C) string, so you can do.

????.Add(AnsiString(Str.c_str()));

This converts the string in Str to a char * string, then creates a temporary AnsiString from that.
0
All Courses

From novice to tech pro — start learning today.