How to handle string in fstream I/O?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: How to handle string in fstream I/O?

  1. #1
    Jon Guest

    How to handle string in fstream I/O?


    I am having a problem in getting the string to work with the fstream. Here
    is a sample of code (without detail defs) that I have been trying to make
    it work. Tried two different modes (ios::binary and ios::text) of writing
    to and reading from the file but the content of file does not appear to
    have the correct info. Any suggestions?

    <iostream>
    <string>
    using namespace std;

    enum {MAX_IP_LOC = 5};
    const string filename="c:\\test\\netcfg.txt";

    struct net_record{
    string netIPAdr[MAX_IP_LOC];
    string hostName[MAX_IP_LOC];
    unsigned short portNum;
    };
    typedef net_record NET_RECORD;

    void main(void)
    {
    NET_RECORD net1;
    char cnt = '0';
    // write some test strings.
    ofstream outFile(filename.c_str());
    for (int i=0; i< MAX_IP_LOC; ++i, ++cnt) {
    net1.netIPAdr[i] = "128.288.45." + cnt;
    ....
    }
    outFile.write((char*)(&net1), sizeof(net1));
    outFile.close();

    // read the test strings.
    ifstream inFile(filename.c_str());
    inFile.read((char*)(&net1), sizeof(net1));
    for (int i=0; i< MAX_IP_LOC; ++i) {
    cout << "Net IP addr = " << net1.netIPAdr[i] << endl;
    ....
    }
    inFile.close();

    }

  2. #2
    Danny Kalev Guest

    Re: How to handle string in fstream I/O?

    you are trying to perform a shallow copy of a non-POD ("plain old data")
    object. Your net1 object contains embedded objects that have to written
    individually. Look at the following article for detailed examples of
    writing string objects to a file and retrieving them later:
    http://gethelp.devx.com/techtips/cpp.../10min0901.asp

    Danny

    Jon wrote:
    >
    > I am having a problem in getting the string to work with the fstream. Here
    > is a sample of code (without detail defs) that I have been trying to make
    > it work. Tried two different modes (ios::binary and ios::text) of writing
    > to and reading from the file but the content of file does not appear to
    > have the correct info. Any suggestions?
    >
    > <iostream>
    > <string>
    > using namespace std;
    >
    > enum {MAX_IP_LOC = 5};
    > const string filename="c:\\test\\netcfg.txt";
    >
    > struct net_record{
    > string netIPAdr[MAX_IP_LOC];
    > string hostName[MAX_IP_LOC];
    > unsigned short portNum;
    > };
    > typedef net_record NET_RECORD;
    >
    > void main(void)
    > {
    > NET_RECORD net1;
    > char cnt = '0';
    > // write some test strings.
    > ofstream outFile(filename.c_str());
    > for (int i=0; i< MAX_IP_LOC; ++i, ++cnt) {
    > net1.netIPAdr[i] = "128.288.45." + cnt;
    > ....
    > }
    > outFile.write((char*)(&net1), sizeof(net1));
    > outFile.close();
    >
    > // read the test strings.
    > ifstream inFile(filename.c_str());
    > inFile.read((char*)(&net1), sizeof(net1));
    > for (int i=0; i< MAX_IP_LOC; ++i) {
    > cout << "Net IP addr = " << net1.netIPAdr[i] << endl;
    > ....
    > }
    > inFile.close();
    >
    > }


  3. #3
    Jon Guest

    Re: How to handle string in fstream I/O?


    Dear Danny,
    Thanks for your help. It works great!
    Best regards,
    Jonathan

    Danny Kalev <dannykk@inter.net.il> wrote:
    >you are trying to perform a shallow copy of a non-POD ("plain old data")
    >object. Your net1 object contains embedded objects that have to written
    >individually. Look at the following article for detailed examples of
    >writing string objects to a file and retrieving them later:
    >http://gethelp.devx.com/techtips/cpp.../10min0901.asp
    >
    >Danny
    >
    >Jon wrote:
    >>
    >> I am having a problem in getting the string to work with the fstream.

    Here
    >> is a sample of code (without detail defs) that I have been trying to make
    >> it work. Tried two different modes (ios::binary and ios::text) of writing
    >> to and reading from the file but the content of file does not appear

    to
    >> have the correct info. Any suggestions?
    >>
    >> <iostream>
    >> <string>
    >> using namespace std;
    >>
    >> enum {MAX_IP_LOC = 5};
    >> const string filename="c:\\test\\netcfg.txt";
    >>
    >> struct net_record{
    >> string netIPAdr[MAX_IP_LOC];
    >> string hostName[MAX_IP_LOC];
    >> unsigned short portNum;
    >> };
    >> typedef net_record NET_RECORD;
    >>
    >> void main(void)
    >> {
    >> NET_RECORD net1;
    >> char cnt = '0';
    >> // write some test strings.
    >> ofstream outFile(filename.c_str());
    >> for (int i=0; i< MAX_IP_LOC; ++i, ++cnt) {
    >> net1.netIPAdr[i] = "128.288.45." + cnt;
    >> ....
    >> }
    >> outFile.write((char*)(&net1), sizeof(net1));
    >> outFile.close();
    >>
    >> // read the test strings.
    >> ifstream inFile(filename.c_str());
    >> inFile.read((char*)(&net1), sizeof(net1));
    >> for (int i=0; i< MAX_IP_LOC; ++i) {
    >> cout << "Net IP addr = " << net1.netIPAdr[i] << endl;
    >> ....
    >> }
    >> inFile.close();
    >>
    >> }



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center