Thread: Storing Objects in txt files?

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Storing Objects in txt files?

    Hi,

    I'm creating a program which stores information, lets say about cars in a text file.

    Up until now, I've only read and wrote to text files using variables, for example:

    cars.txt:

    Code:
    Ford Mondeo 5 25.25
    Vauxhall Vectra 5 10.10
    And I would open a stream to write and read to the file.

    But now I'm using an OO approach and am unsure on how to write objects to the text file, eg:


    Function to create a new object:

    Code:
    void createAcar()
    {
    cars car1;
    car1.manufacturer = Ford;
    car1.model = Mondeo;
    car1.door = 5;
    car1.mpg = 25.25
    }
    How would I store the object in the text file? At the moment I'm trying:


    Code:
    carTextFile >> car1; //carTextFile is the name of the stream
    But Visual Studio says " 'carTextFile' : undeclared identifier ".

    I'm thinking this is due to scope, but am wondering if my method of storing the objects in the text file is way off.

    Thanks for any advice

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    << and >> are OK for storing objects in text form. Your problem has nothing to do with that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    'carTextFile' : undeclared identifier
    Check the spelling of carTextFile, and where it is declared.
    Can you show the operator << you wrote for cars?

    btw, an object that holds the information about a car should not be plural. Use the plural form for the name of the container that holds more than one of them.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Quote Originally Posted by CornedBee View Post
    << and >> are OK for storing objects in text form. Your problem has nothing to do with that.

    Oh that's good to know, at least I'm familiar with how that works.

    I'm unsure where my mistake maybe above, maybe my using strings? That is something I have to look at.

    Many thanks for your advice, it's a 'weight of my mind'.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Quote Originally Posted by iMalc View Post
    Check the spelling of carTextFile, and where it is declared.
    A scope issue?


    Can you show the operator << you wrote for cars?
    I cannot right now, just putting the basic code together at the moment and wanted to be sure my methods would at least work, even if in a bad implementation.

    btw, an object that holds the information about a car should not be plural. Use the plural form for the name of the container that holds more than one of them.
    Noted, many thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading txt files
    By Tbrez in forum C Programming
    Replies: 6
    Last Post: 03-10-2009, 03:40 PM
  2. Replies: 12
    Last Post: 02-12-2009, 02:39 PM
  3. using c++ to edit txt files
    By trescenzi in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2006, 08:54 PM
  4. txt files
    By Kitu in forum C++ Programming
    Replies: 8
    Last Post: 08-06-2004, 03:55 PM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM