Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > reading / writing a string

Reply
Thread Tools

reading / writing a string

 
 
Per
Guest
Posts: n/a
 
      12-03-2008
On 2008-12-03, hamishd <(E-Mail Removed)> wrote:
> Hi, I have a structure like this:
>
> #define MAX_LENGTH 20
>
> struct InformationClass
> {
> char StringA[MAX_LENGTH];
> char StringB[MAX_LENGTH];
> };
>
> I want to be able to write it to a file, and also read it back in
> again? I am using fprintf, and fscanf.
>
> Write like this:
> fprintf(stream, "%s%s", IC.StringA, IC.StringB);
>
> Read like this:
> fscanf(stream, "%s%s", &IC.StringA, &IC.StringB);
>
> It doesn't seem to work. Is there an easy way to do this?
>


To me this looks like C-code. In C++ I would have overloaded the <<
and >> operators for istream and ostream respectively. If its just
serialization you are after there is also the boost serialization library.

Further in C++ use std::string instead of char[].

/Per
 
Reply With Quote
 
 
 
 
hamishd
Guest
Posts: n/a
 
      12-03-2008
Hi, I have a structure like this:

#define MAX_LENGTH 20

struct InformationClass
{
char StringA[MAX_LENGTH];
char StringB[MAX_LENGTH];
};

I want to be able to write it to a file, and also read it back in
again? I am using fprintf, and fscanf.

Write like this:
fprintf(stream, "%s%s", IC.StringA, IC.StringB);

Read like this:
fscanf(stream, "%s%s", &IC.StringA, &IC.StringB);

It doesn't seem to work. Is there an easy way to do this?

 
Reply With Quote
 
 
 
 
Fred
Guest
Posts: n/a
 
      12-03-2008
On Dec 3, 7:54*am, Jeff Schwab <(E-Mail Removed)> wrote:
> hamishd wrote:
> > Hi, I have a structure like this:

>
> > #define MAX_LENGTH 20

>
> > struct InformationClass
> > {
> > * *char StringA[MAX_LENGTH];
> > * *char StringB[MAX_LENGTH];
> > };

>
> > I want to be able to write it to a file, and also read it back in
> > again? I am using fprintf, and fscanf.

>
> > Write like this:
> > fprintf(stream, "%s%s", IC.StringA, IC.StringB);

>
> Here, you write the strings back-to-back, without any intervening
> whitespace. *When you try to read the strings back into your program,
> the whole sequence will be mistaken for one string. *You could try
> inserting a space, so that the format string is "%s %s".
>
> > Read like this:
> > fscanf(stream, "%s%s", &IC.StringA, &IC.StringB);

>
> > It doesn't seem to work. Is there an easy way to do this?

>
> The "easy" way is to use the C++ standard library's stream and string
> types, and to follow some basic conventions with regard to operator
> overloading. *This may seem cryptic to you at first, but it is worth
> taking the time to understand; the effort pays off quickly.
>
> #include <fstream>
> #include <iostream>
> #include <string>
>
> struct information {
> * * *std::string string_a;
> * * *std::string string_b;
>
> };
>
> std:stream& operator<<(
> * * * * *std:stream& stream,
> * * * * *information const& info) {
> * * *return stream << info.string_a << ' ' << info.string_b;
>
> }
>
> std::istream& operator>>(
> * * * * *std::istream& stream,
> * * * * *information& info) {
> * * *return stream >> info.string_a >> info.string_b;
>
> }
>
> void write_file(char const* name, information const& info) {
> * * *std:fstream stream( name );
> * * *stream << info;
>
> }
>
> information read_file(char const* name) {
> * * *std::ifstream stream( name );
> * * *information info;
> * * *stream >> info;
> * * *return info;
>
> }
>
> int main() {
> * * *information const info = { "hello", "world" };
> * * *char const file_name[] = "some_file";
> * * *write_file(file_name, info);
> * * *std::cout << read_file(file_name) << '\n';
> * * *return 0;
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -


This still will not work if either of the strings contains embedded
whitespace.
--
Fred K
 
Reply With Quote
 
KK
Guest
Posts: n/a
 
      12-03-2008
On Dec 3, 11:14*am, Fred <(E-Mail Removed)> wrote:
> On Dec 3, 7:54*am, Jeff Schwab <(E-Mail Removed)> wrote:
>
>
>
> > hamishd wrote:
> > > Hi, I have a structure like this:

>
> > > #define MAX_LENGTH 20

>
> > > struct InformationClass
> > > {
> > > * *char StringA[MAX_LENGTH];
> > > * *char StringB[MAX_LENGTH];
> > > };

>
> > > I want to be able to write it to a file, and also read it back in
> > > again? I am using fprintf, and fscanf.

>
> > > Write like this:
> > > fprintf(stream, "%s%s", IC.StringA, IC.StringB);

>
> > Here, you write the strings back-to-back, without any intervening
> > whitespace. *When you try to read the strings back into your program,
> > the whole sequence will be mistaken for one string. *You could try
> > inserting a space, so that the format string is "%s %s".

>
> > > Read like this:
> > > fscanf(stream, "%s%s", &IC.StringA, &IC.StringB);

>
> > > It doesn't seem to work. Is there an easy way to do this?

>
> > The "easy" way is to use the C++ standard library's stream and string
> > types, and to follow some basic conventions with regard to operator
> > overloading. *This may seem cryptic to you at first, but it is worth
> > taking the time to understand; the effort pays off quickly.

>
> > #include <fstream>
> > #include <iostream>
> > #include <string>

>
> > struct information {
> > * * *std::string string_a;
> > * * *std::string string_b;

>
> > };

>
> > std:stream& operator<<(
> > * * * * *std:stream& stream,
> > * * * * *information const& info) {
> > * * *return stream << info.string_a << ' ' << info.string_b;

>
> > }

>
> > std::istream& operator>>(
> > * * * * *std::istream& stream,
> > * * * * *information& info) {
> > * * *return stream >> info.string_a >> info.string_b;

>
> > }

>
> > void write_file(char const* name, information const& info) {
> > * * *std:fstream stream( name );
> > * * *stream << info;

>
> > }

>
> > information read_file(char const* name) {
> > * * *std::ifstream stream( name );
> > * * *information info;
> > * * *stream >> info;
> > * * *return info;

>
> > }

>
> > int main() {
> > * * *information const info = { "hello", "world" };
> > * * *char const file_name[] = "some_file";
> > * * *write_file(file_name, info);
> > * * *std::cout << read_file(file_name) << '\n';
> > * * *return 0;

>
> > }- Hide quoted text -

>
> > - Show quoted text -- Hide quoted text -

>
> > - Show quoted text -

>
> This still will not work if either of the strings contains embedded
> whitespace.
> --
> Fred K


check getline() where you can specify a delimiter. You could use '\n'
as delimiter while writing a strings in different lines
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-03-2008
On Dec 3, 8:44*pm, Jeff Schwab <(E-Mail Removed)> wrote:
> Fred wrote:
> > On Dec 3, 7:54 am, Jeff Schwab <(E-Mail Removed)> wrote:
> >>> fprintf(stream, "%s%s", IC.StringA, IC.StringB);
> >> Here, you write the strings back-to-back, without any
> >> intervening whitespace. *When you try to read the strings
> >> back into your program, the whole sequence will be mistaken
> >> for one string. *You could try inserting a space, so that
> >> the format string is "%s %s".

> > This still will not work if either of the strings contains
> > embedded whitespace.


> Good point. *That did not occur to me.


It's a general problem when serializing in text format.
Choosing a separator for most types is fairly trivial, however;
there are lots of characters which can't show up in the string
representation an int or a double. (But it still requires some
thought---the C++ standard for formatting a complex regretfully
uses a separator which can occur in a double, which means that a
standard conforming implementation is totally useless.) For
strings in general, it's a bit more complicated. Most strings,
however, have a semantic, e.g. family name, or something like
that, which will forbid some characters, and thus allow a
separator; if not, I've written a ParsableString class which
ensures round trip serialization, roughly by using the
conventions for C++ string literals: in quotes, with quotes and
unprintable characters escaped.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binary vs. formatted std::string reading/writing to streams Leslaw Bieniasz C++ 2 01-15-2010 11:53 PM
Reading/writing a string problem TyPR124@gmail.com C++ 5 12-07-2007 01:37 PM
Reading text file String by String Migrators Java 7 09-26-2007 04:12 AM
Writing a "String#jindex" method to do the same for "index" as "String#jlength" does for "length" Greg Hurrell Ruby 1 02-26-2007 10:07 AM
read a string..writing a string happy C Programming 3 02-12-2005 04:17 PM



Advertisments