473,786 Members | 2,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I read 2 characters from file into a std::string

How can I read in two characters from a file into a std::string?

the long way:

fstream f("myfile.bin ", ios::binary | ios:in); // Opened for read
binary...
unsigned char buffer[3];
buffer[2] = '\0';
f.read(buffer, 2);
std::string x(buffer);

Got a better way?

Jun 6 '07 #1
6 2351
SpreadTooThin wrote:
How can I read in two characters from a file into a std::string?

the long way:

fstream f("myfile.bin ", ios::binary | ios:in); // Opened for read binary...
unsigned char buffer[3];
buffer[2] = '\0';
f.read(buffer, 2);
std::string x(buffer);

Got a better way?
fstream f("myfile.bin ", ios::binary | ios:in);
char buffer[2];
f.read(buffer, 2);
std::string x(buffer, buffer+2);

They do behave differently.
Jun 6 '07 #2
On Jun 6, 10:11 pm, SpreadTooThin <bjobrie...@gma il.comwrote:
How can I read in two characters from a file into a std::string?
the long way:
fstream f("myfile.bin ", ios::binary | ios:in); // Opened for read
binary...
unsigned char buffer[3];
buffer[2] = '\0';
f.read(buffer, 2);
std::string x(buffer);
Got a better way?
std::string x ;
x += f.get() ;
x += f.get() ;

(Of course, you'll want to check that you actually did get two
characters.)

Something that will work with all current implementations , and
will be guaranteed by the next version of the standard:

std::string x( 2, ' ' ) ;
f.read( &x[ 0 ], 2 ) ;

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 7 '07 #3
James Kanze wrote:
....
Something that will work with all current implementations , and
will be guaranteed by the next version of the standard:
Do you have a reference to the this proposal (contiguous strings)?
Jun 7 '07 #4
On 2007-06-07 01:59:03 -0700, Gianni Mariani <gi*******@mari ani.wssaid:
James Kanze wrote:
...
>Something that will work with all current implementations , and
will be guaranteed by the next version of the standard:

Do you have a reference to the this proposal (contiguous strings)?
See: http://www.open-std.org/jtc1/sc22/wg...fects.html#530

--
Clark S. Cox III
cl*******@gmail .com

Jun 7 '07 #5
On Jun 7, 10:59 am, Gianni Mariani <gi3nos...@mari ani.wswrote:
James Kanze wrote:
...
Something that will work with all current implementations , and
will be guaranteed by the next version of the standard:
Do you have a reference to the this proposal (contiguous strings)?
It's in the current draft (N2134): "The char-like objects in a
basic_string object shall be stored contiguously. That is, for
any basic_string object s, the identity &*(s.begin() + n) ==
&*s.begin() + n shall hold for all values of n such that 0 <= n
< s.size()." From what I understand, a non-const version of
data() will also be added (as well as const and non-const data()
functions to std::vector), in order to obtain the address in a
way somewhat more transparently than &s[0].

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 8 '07 #6
James Kanze wrote:
>
It's in the current draft (N2134)
Just so everyone's up to date, the current draft (reflecting changes
made at the April, 2007 meeting) is N2284.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Jun 8 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
22739
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give the std::string a pointer and a length and then give the std::string control over the pointer and its content? I'm basically trying to avoid copying large text between an std::string and a char pointer, and vice versa. Is there anyhing in the...
10
8184
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
11
3662
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while( (idx=str.find_first_of(',')) >= 0 ) { str.replace( idx, 1, "" ); str.insert( idx, " or " ); }
22
13334
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; char c; std::string str;
19
6165
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
6
6645
by: Khuong Dinh Pham | last post by:
How do i read the contents of a xml file and output it to a std::string. Thx in advance
84
15905
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
11
2454
by: Peter Olcott | last post by:
Does C++ have anything like this?
11
4291
by: Christopher Pisz | last post by:
Is std::string::npos always going to be less than any std::string 's size()? I am trying to handle a replacement of all occurances of a substr, in which the replacement also contains the substr. Yick. All I could come up with is: #include <string> int main() { std::string text;
0
9492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10360
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.