0

I want to read a whole file into a string. I am using Embarcadero C++Builder XE.

When I use the below code in my project, it is giving errors:

#include <iostream>
#include <iomanip>
#include <iterator>
#include <fstream>

std::ifstream in(Path);
std::string s((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
[ILINK32 Error] Error: Unresolved external 'std::_Mutex::_Lock()' 
[ILINK32 Error] Error: Unresolved external 'std::_Mutex::_Unlock()' 
[ILINK32 Error] Error: Unresolved external 'std::char_traits::eq_int_type(const int&, const int&)' 
[ILINK32 Error] Error: Unresolved external 'std::char_traits::not_eof(const int&)' 
[ILINK32 Error] Error: Unresolved external 'std::char_traits::to_char_type(const int&)'
[ILINK32 Error] Error: Unresolved external 'std::char_traits::eof()' 
[ILINK32 Error] Error: Unresolved external 'std::char_traits::to_int_type(const char&)' 
[ILINK32 Error] Error: Unresolved external 'std::locale::id::operator unsigned int()' 
[ILINK32 Error] Error: Unresolved external 'std::locale::name() const' 
[ILINK32 Error] Error: Unresolved external 'std::codecvt_base::codecvt_base(unsigned int)' 
[ILINK32 Error] Error: Unresolved external 'std::locale::facet::_Incref()' 
[ILINK32 Error] Error: Unresolved external 'std::ios_base::ios_base()' 
[ILINK32 Error] Error: Unresolved external 'std::ios_base::getloc() const' 
[ILINK32 Error] Error: Unresolved external 'std::ctype::_Getcat(std::locale::facet * *, std::locale *)' 
[ILINK32 Error] Error: Unresolved external 'std::ctype::widen(char) const' 
[ILINK32 Error] Error: Unresolved external 'std::ios_base::rdstate() const' 
[ILINK32 Error] Error: Unable to perform link

Any other solutions for reading a file into a string?

13
  • 1
    Check your library path. Maybe some configuration problem with your standard library. Commented Jun 6, 2017 at 11:45
  • @Devolus : Intsand of ifstream , Is there any soulution for reading file in Borland C++, RAD XE Embarcadero IDE ? Commented Jun 6, 2017 at 11:50
  • Your problem is not the standard library functions, it's your setup. As a sidenote, what you posted ist not a proper C++ code anyway, so you should post a full example anyway. Commented Jun 6, 2017 at 11:51
  • @Devolus : I have Written here code which is giving error. My goal is to create Function which read whole large file data and return file content in to string variable. Project is very large and old, So I can not do major change in configuration/setup at this moment. Commented Jun 6, 2017 at 12:10
  • 1
    Why using iostream and fstream in VCL? You got AnsiString natively in VCL ... TStrings and TMemo->Lines can load entire text file with simple LoadFromFile method. File access is also simple use VCL: FileOpen/FileCreate, FileSeek, FileRead/FileWrite, FileClose. But to your errors your project is wrongly configured so may be you got some weird settings or corruption in project file ... sometimes creating new one helps. Unresolved external means you have included header but no code nor linked obj/lib/dll where it is Commented Jun 7, 2017 at 8:25

1 Answer 1

0

Lets create an empty VCL Form app and add a single TMemo control on it. The IDE will name it Memo1 automatically . The Memo object is a Text editor with 2 important properties:

  1. Memo1->Text

    Text is a System::String (automatically reallocable string class from VCL holding the whole text of the memo. String has a Length() function returning number of characters present, and each character is accessed using the [] operator like this (indexed from 1 !!!):

    String s = _D("123456"); // set some string to s
    int l = s.Length(); // get its length
    for (int i = 1; i <= l; i++) s[i] = '0'; // change all the chars to zeros
    Memo1->Text = s; // feed it to Memo
    

    There are tons of support functions inside String, the most important for you is formatted output:

    Memo1->Text = String().sprintf(_D("float number: %7.3f"), float(123.456));
    

    You can use String as your local variables, too:

    String s = _D("some text");
    Memo1->Text = s;
    

    Also for backward compatibility, if you need a char* for some functions, then just assign the String to an AnsiString and call its c_str() method:

    String s = _D("some text");
    AnsiString as = s;
    char *txt = as.c_str();
    

    But beware not to overwrite unallocated space, or use that pointer after any reallocation inside as, or after as goes out of scope. This is used mainly as input parameter for Win32 API functions, C functions, non-VCL LIBs/DLLs, etc.

  2. Memo1->Lines

    This is a TStrings class, which holds a dynamic array of Strings. In a TMemo, each element represents a line in the Text. You can dynamically add lines to Memo1 like this:

    Memo1->Lines->Add(_D("text 1"));
    Memo1->Lines->Add(_D("text 2"));
    Memo1->Lines->Add(_D("text 3"));
    

    You can load/save the whole Memo contents like this:

    Memo1->Lines->LoadFromFile("file1.txt");
    Memo1->Lines->SaveToFile("file1.txt");
    

    Any change in Memo1->Lines will also change Memo1->Text, and vice versa, as they both represent the same thing. You can have your Memo hidden (invisible) and still use it in case you do not want to show what are you doing ...

You can also load an entire file into a char[] buffer using file access functions without any VCL component, like this:

int hnd = FileOpen("file1.txt", fmOpenRead); // open file hnd>=0 if all OK
int siz = FileSeek(hnd, 2, 0); // point to end of file and return position = file size
FileSeek(hnd, 0, 0); // point back to start of file
char *txt = new char[siz+1] // allocate space for text and null terminator
FileRead(hnd, txt, siz); // load the file into memory at once
FileClose(hnd); // close file as we do not need it anymore

txt[siz] = 0; // add null termination just to be safe (text files do not contains zeros usually)
// do your thing with txt[siz]

delete[] txt;

Or:

TFileStream *strm = new TFileStream("file1.txt", fmOpenRead); // open file
int siz = strm->Size; // file size
char *txt = new char[siz+1] // allocate space for text and null terminator
strm->ReadBuffer(txt, siz); // load the file into memory at once
delete strm; // close file as we do not need it anymore

// do your thing with txt[siz]

delete[] txt;

Or:

TMemoryStream *strm = new TMemoryStream;
strm->LoadFromFile("file1.txt"); // open file

// do your thing with strm->Memory up to strm->Size bytes...

delete strm;

Unlike std::fstream, these will work for any file ... even if holding control codes.

Sign up to request clarification or add additional context in comments.

5 Comments

"Text is AnsiString" - no, it is not. It is a System::String, and in C++Builder 2009 and later, which includes XE, System::String is an alias for System::UnicodeString, not for System::AnsiString. As such, char *txt = Memo1->Text.c.str(); will not compile (and it is dangerous anyway, as txt will be left pointing to invalid memory when the statement is complete). Also, Memo1->Text[i]='0'; has no effect on the Memo, since it is modifying a temp String object returned by the Text getter.
@RemyLebeau it should be c_str() of coarse nice catch but yore right Text property is not the same as AnsiString I am biased by my use of BCB and BDS and no experience with XE. will repair my mistake shortly
"You can have your Memo hidden (invisible) and still use it in case you do not want to show what are you doing" - use the TStringList class for that instead. Don't use visual controls for non-visual work.
@RemyLebeau what does _D("") ? it is some kind of support macro for System::String constants ? or it is to preserve unicode ?
it is a support macro, yes. It ensures the literal uses the same charset encoding that System::String and System::Char use (currently UTF-16 in 2009+). It is similar to the _T() macro in the C standard lib for _TCHAR literals, and the TEXT() macro in the Win32 API for TCHAR literals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.