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?
Lets create an empty VCL Form app and add a single
TMemocontrol on it. The IDE will name itMemo1automatically . The Memo object is a Text editor with 2 important properties:Memo1->TextTextis aSystem::String(automatically reallocable string class from VCL holding the whole text of the memo.Stringhas aLength()function returning number of characters present, and each character is accessed using the[]operator like this (indexed from 1 !!!):There are tons of support functions inside
String, the most important for you is formatted output:You can use
Stringas your local variables, too:Also for backward compatibility, if you need a
char*for some functions, then just assign theStringto anAnsiStringand call itsc_str()method:But beware not to overwrite unallocated space, or use that pointer after any reallocation inside
as, or afterasgoes out of scope. This is used mainly as input parameter for Win32 API functions, C functions, non-VCL LIBs/DLLs, etc.Memo1->LinesThis is a
TStringsclass, which holds a dynamic array ofStrings. In a TMemo, each element represents a line in theText. You can dynamically add lines toMemo1like this:You can load/save the whole Memo contents like this:
Any change in
Memo1->Lineswill also changeMemo1->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:Or:
Or:
Unlike
std::fstream, these will work for any file ... even if holding control codes.