4 Replies - 11475 Views - Last Post: 25 November 2009 - 09:49 AM Rate Topic: -----

#1 stefaan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-November 08

Fixed length string in C++

Posted 25 November 2009 - 07:51 AM

I would like to know how to create fixed length strings in C++ (using VC++ 2008 Express Edition for Windows)
The problem im experiencing with varying string is that when i input over 15 characters in the string and save the object(user defined) to a binary file(random access) and i try to read it the program crashes with a "assertion error". When i debug it brings me to the string and says <bad ptr>. Here is a example
#include <string>
using std::string;

class Address
{
private:
string line1;
string line2;
string city;
string parish;
public:
Address();
~Address(){};
void setLine1();
//and the others follow
void displayAddress();
};


if the user enters a very long "line1: street#+name" everything is fine until im trying to read it back from the file i saved it to. However, if the user entered for instance 10 characters, everything is fine.
How do i overcome this problem?

Is This A Good Question/Topic? 0
  • +

Replies To: Fixed length string in C++

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: Fixed length string in C++

Posted 25 November 2009 - 07:55 AM

Then the issue lies with how you save/retrieve the data then. Having a std string over 15 characters doesn't naturally crash programs. If you want to ensure you only have a certain space usage, use a C style string and trim it. You could do the same thing with a C++ string as well. (Only retrieve 'x' amount of characters from input, etc...)

This post has been edited by KYA: 25 November 2009 - 08:00 AM

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Fixed length string in C++

Posted 25 November 2009 - 07:58 AM

can you show us how your write and read the string to/from the file?

C++/STL does not really have "fixed size" strings -- that would be character arrays I suppose, though even thoughs can be dynamic since they are null terminated. You can ofcourse always create your OWN string class and have it do anything you would like.
Was This Post Helpful? 0
  • +
  • -

#4 stefaan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-November 08

Re: Fixed length string in C++

Posted 25 November 2009 - 08:19 AM

View PostNickDMax, on 25 Nov, 2009 - 06:58 AM, said:

can you show us how your write and read the string to/from the file?

C++/STL does not really have "fixed size" strings -- that would be character arrays I suppose, though even thoughs can be dynamic since they are null terminated. You can ofcourse always create your OWN string class and have it do anything you would like.


Address adr; //Create object adr of type Address
//Write object to the file
oftsream save("Address.dat", ios::in | ios::out | ios:: binary);
save.seekp((0)*sizeof(Address));
save.write(reinterpret_cast<const char*>(&adr), sizeof(Address));

//read object from file
ifstream display ("Address.dat", ios::in | ios::binary);
display.seekg((0)*sizeof(Address));
display.read(reinterpret_cast<char*>(&adr), sizeof(Address));

//seekp and seekg offset 0 are just references, i use actual numbers(0,1,2,3.....) in the program


Was This Post Helpful? 0
  • +
  • -

#5 stefaan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 22-November 08

Re: Fixed length string in C++

Posted 25 November 2009 - 09:49 AM

View Poststefaan, on 25 Nov, 2009 - 07:19 AM, said:

View PostNickDMax, on 25 Nov, 2009 - 06:58 AM, said:

can you show us how your write and read the string to/from the file?

C++/STL does not really have "fixed size" strings -- that would be character arrays I suppose, though even thoughs can be dynamic since they are null terminated. You can ofcourse always create your OWN string class and have it do anything you would like.


Address adr; //Create object adr of type Address
//Write object to the file
oftsream save("Address.dat", ios::in | ios::out | ios:: binary);
save.seekp((0)*sizeof(Address));
save.write(reinterpret_cast<const char*>(&adr), sizeof(Address));

//read object from file
ifstream display ("Address.dat", ios::in | ios::binary);
display.seekg((0)*sizeof(Address));
display.read(reinterpret_cast<char*>(&adr), sizeof(Address));

//seekp and seekg offset 0 are just references, i use actual numbers(0,1,2,3.....) in the program



How can i write/read from the file so it does not crash the program
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1