在文件处理中使用C ++字符串 [英] Use C++ strings in file handling

查看:42
本文介绍了在文件处理中使用C ++字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在文件处理中使用C ++字符串?我创建了一个类,该类具有C ++字符串作为其私有数据成员之一,但是即使我暂时不使用它,也从文件中读取时出现错误,并使用构造函数中的默认值进行了初始化.写入文件时没有问题.如果我改用C字符串,但我不想这么做,则效果很好.有办法解决吗?

How to use C++ strings in file handling? I created a class that had C++ string as one of its private data members but that gave an error while reading from the file even if I am not manipulating with it at the moment and was initialised with default value in constructor. There is no problem while writing to the file. It works fine if I use C string instead but I don't want to. Is there a way to solve this?

class budget 
{      
     float balance;      
     string due_name,loan_name;              //string objects
     int  year,month;     
     float due_pay,loan_given;    

     public:                 
     budget()     
     {      
          balance=0;
          month=1;        
          due_name="NO BODY";              //default values
          loan_name="SAFE";   
          year=0;             
          balance = 0;      
          due_pay=0;        
          loan_given=0;      
     }
      .
      .
      .
 };

void read_balance()                //PROBLEM AFTER ENTERING THIS FUNCTION      
{          
     system("cls");        
     budget b;     
     ifstream f1;     
     f1.open("balance.dat",ios::in|ios::binary);     
     while(f1.read((char*)&b,sizeof(b)))     
     { b.show_data();       
     }     
     system("cls");        
     cout<<"No More Records To Display!!";     
     getch();     
     f1.close();       
} 

推荐答案

字符串是非POD数据类型.您不能通过读/写功能从字符串中读/写.

String is non-POD data-type. You cannot read/write from/in string by read/write functions.

basic_istream<charT,traits>& read(char_type* s, streamsize n);

30效果:表现为无格式输入功能(如27.7.2.3,第1段).构造哨兵对象后,如果!good()调用setstate(failbit)可能会引发异常,然后返回.否则,将提取字符并将其存储到连续的字符中第一个元素由s.323指定的数组的位置提取并存储字符,直到出现以下任何一种情况发生:—存储n个字符; —输入结束序列(在这种情况下,该函数将调用setstate(failbit | eofbit),这可能会引发ios_base :: failure(27.5.5.4)).31返回:* this.

30 Effects: Behaves as an unformatted input function (as described in 27.7.2.3, paragraph 1). After constructing a sentry object, if !good() calls setstate(failbit) which may throw an exception, and return. Otherwise extracts characters and stores them into successive locations of an array whose first element is designated by s.323 Characters are extracted and stored until either of the following occurs: — n characters are stored; — end-of-file occurs on the input sequence (in which case the function calls setstate(failbit | eofbit), which may throw ios_base::failure (27.5.5.4)). 31 Returns: *this.

关于如何放置 std :: string 的成员,没有任何关系.查看或使用 boost :: serialiation . http://www.boost.org/doc/libs/1_50_0/libs/serialization/doc/index.html 当然,您可以写入字符串的大小,然后写入数据;读取时-读取大小,分配该大小的数组,在此数组中读取数据,然后创建细绳.但是使用boost效果更好.

There is nothing about, how members of std::string placed. Look at, or use boost::serialiation. http://www.boost.org/doc/libs/1_50_0/libs/serialization/doc/index.html And of course you can write size of string and then write data and when read - read size, allocate array of this size, read data in this array and then create string. But use boost is better.

这篇关于在文件处理中使用C ++字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆