3 Replies - 924 Views - Last Post: 01 December 2010 - 10:40 PM Rate Topic: -----

#1 Guest_bedwar64*


Reputation:

Stops working when I try to print a string

Posted 01 December 2010 - 09:52 PM

I'm trying to read data from a random access file and when I try to print a string containing the tool name the program stops working. program compiles under dev-c++.
there are 3 files
lab8.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include "HardwareData.h"
using namespace std;

int main()
{
    HardwareData blankHardwareData;
    HardwareData myHardwareData;
    HardwareData inHardwareData;
    int intSentinal = 1;
    int intChoice;
    string strFileName;
    string strtmpToolName = "                                   ";
    double dbltmpCostPerTool;
    int inttmpRecordNumber;
    int inttmpToolQuantity;
    string strDump;
    char *tmpFileName;
    cout << "Enter file name: ";
    getline(cin, strFileName);
    //getline(cin, strDump);
    tmpFileName = (char*)strFileName.c_str();
    fstream inOutHardwareData(tmpFileName, ios::in | ios::out | ios::binary);
    if(!inOutHardwareData)
    {
        fstream inOutHardwareData(tmpFileName, ios::out | ios::binary);
        for(int intLoop = 0; intLoop < 100; intLoop++)
                    inOutHardwareData.write( reinterpret_cast< const char * >( &blankHardwareData ), sizeof(HardwareData));
        inOutHardwareData.close();
        inOutHardwareData.open(tmpFileName, ios::in | ios::out | ios::binary);
    };
    while(intSentinal >=0 )
    {
         fstream inOutHardwareData(tmpFileName, ios::in | ios::out | ios::binary);
         if(!inOutHardwareData)
         {
              fstream inOutHardwareData(tmpFileName, ios::out | ios::binary);
              for(int intLoop = 0; intLoop < 100; intLoop++)
                    inOutHardwareData.write( reinterpret_cast< const char * >( &blankHardwareData ), sizeof(HardwareData));
              inOutHardwareData.close();
              inOutHardwareData.open(tmpFileName, ios::in | ios::out | ios::binary);
         };
         
         cout << "\n\nPlease select an option\n1)Enter a file name\n2)Display Records\n3)Enter new data\n4)quit\nChoice: ";
         cin >> intChoice;
         switch(intChoice)
         {
              case 1:
              {
                cout << "Enter file name: ";
                getline(cin, strFileName);
                //getline(cin, strDump);
                tmpFileName = (char*)strFileName.c_str();
                inOutHardwareData.close();
                /*inOutHardwareData(tmpFileName, ios::in | ios::out | ios::binary);
                if(!inOutHardwareData)
                {
                     fstream inOutHardwareData(tmpFileName, ios::out | ios::binary);
                     for(int intLoop = 0; intLoop < 100; intLoop++)
                          inOutHardwareData.write( reinterpret_cast< const char * >( &blankHardwareData ), sizeof(HardwareData));
                     inOutHardwareData.close();
                     inOutHardwareData.open(tmpFileName, ios::in | ios::out | ios::binary);
                };*/
                break;
              }
              case 2:
              {
                   inOutHardwareData.seekg( 0 );
                   inOutHardwareData.read( reinterpret_cast< char * >( &inHardwareData ), sizeof( HardwareData ) );
                   cout << "record number    Tool Name                    #tools      cost per tool\n";
                   for(int intLoop = 0; intLoop < 100; intLoop++)
                   {
                        if(inHardwareData.getToolQuantity() != 0)
                        {
                             inttmpToolQuantity = inHardwareData.getToolQuantity();
                             cout << "a";
                             dbltmpCostPerTool = inHardwareData.getCostperUnit();
                             cout << "b";
                             //cout << inHardwareData.getToolName();
                             cout << "c";
                             cout << "\n" << left << setw(15) << (intLoop+1) << "  " ;
                             inHardwareData.printToolName();
                             cout <<"  " << setw(5) << inttmpToolQuantity << "  $" << setprecision(2) << setw(10) << dbltmpCostPerTool;
                        };
                        inOutHardwareData.read( reinterpret_cast< char * >( &inHardwareData ), sizeof( HardwareData ) );
                   };
                   inOutHardwareData.close();
                   break;
              }
              case 3:
              {
                   cout << "\nEnter record number to edit: ";
                   cin >> inttmpRecordNumber;
                   cout << "Enter tool name: ";
                   getline(cin, strDump);
                   getline(cin, strtmpToolName);
                   cout << "Enter number of tools: ";
                   //getline(cin, strDump);
                   cin >> inttmpToolQuantity;
                   cout << "Enter cost per tool: ";
                   cin >> dbltmpCostPerTool;
                   cout << "starting";
                   //inOutHardwareData.close();
                   myHardwareData.setData( inttmpToolQuantity, dbltmpCostPerTool, strtmpToolName );
                   cout << "data set";
                   //fstream inOutHardwareData(tmpFileName, ios::in | ios::out | ios::binary);
                   inOutHardwareData.seekp((inttmpRecordNumber - 1) * sizeof(HardwareData));
                   cout << "location sought";
                   inOutHardwareData.write( reinterpret_cast< const char * >(&myHardwareData), sizeof(HardwareData));
                   cout << "data written";
                   inOutHardwareData.close();
                   cout << "file closed";
                   break;
              }
              case 4:
              {
                   intSentinal = -1;
                   inOutHardwareData.close();
                   break;
              };
         };
    };
    myHardwareData.~HardwareData();
};



HardwareData.h

//#ifndef HARDWAREDATA_H
//#define HARDWAREDATA_H
#include <string>
using namespace std;

class HardwareData
{
      public:
             HardwareData();
             ~HardwareData();
             string getToolName();
             double getCostperUnit();
             int getToolQuantity();
             void setData( int, double, string );
             void setCurrentRecord( int );
             void printToolName();
      //private:
              //int intRecordNumber
              int intToolQuantity;
              double dblCostPerTool;
              string strToolName;
              //string strFileName;
};



and HardwareData.cpp

#include <string>
#include <iostream>
//#include <fstream>
#include <iomanip>
//#include <cstdlib>
#include "HardwareData.h"
using namespace std;

HardwareData::HardwareData(/*strint strEnteredFileName*/)
{
     //strFileName = strEnteredFilename;
     //intRecordNumber = 1;
     strToolName = "                                 ";
     dblCostPerTool = 0;
     intToolQuantity = 0;
};

HardwareData::~HardwareData()
{
}

void HardwareData::printToolName()
{
     cout << strToolName;
};

void HardwareData::setData( int intEnteredToolQuantity, double dblEnteredCostPerTool, string strEnteredToolName)
{
     //intRecordNumber = intEnteredRecordNumber;
     cout << "a";
     intToolQuantity = intEnteredToolQuantity;
     cout << "b";
     dblCostPerTool = dblEnteredCostPerTool;
     cout << "c";
     strToolName.swap( strEnteredToolName );
     cout << "d";
};

double HardwareData::getCostperUnit()
{
     return(dblCostPerTool);
};

string HardwareData::getToolName()
{
     return(strToolName);
};

int HardwareData::getToolQuantity()
{
     return(intToolQuantity);
};



I've isolated it to the string but I don't know why it's not working. I'm guessing I'm either reading the data wrong or when the data's read it's format is different somehow or I'm writing the string to the file wrong. any help would be appreciated.

Is This A Good Question/Topic? 0

Replies To: Stops working when I try to print a string

#2 Guest_bedwar64*


Reputation:

Re: Stops working when I try to print a string

Posted 01 December 2010 - 10:06 PM

I should've been more specific. I'm trying to print the string from in the switch statement in main under case 2: where it reads the data and prints if it's valid.
Was This Post Helpful? 0

#3 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2555
  • View blog
  • Posts: 4,739
  • Joined: 30-May 10

Re: Stops working when I try to print a string

Posted 01 December 2010 - 10:12 PM

http://www.parashift...ialization.html

> int intToolQuantity;
> double dblCostPerTool;
> string strToolName;
When you just .write() the structure, you're just writing the bits of the base elements of a std::string.
This almost certainly contains POINTERS to where the actual string is stored.

So when you read back in said pointers, you're pointing at memory that no longer exists and you see garbage.

Raw reading/writing only works for POD(Plain Old Data) types like int and char (and arrays of them).
For STL objects like std::string, you need a more refined approach.
Was This Post Helpful? 1
  • +
  • -

#4 Guest_bedwar64*


Reputation:

Re: Stops working when I try to print a string

Posted 01 December 2010 - 10:40 PM

Thank you. somehow I missed that. for the short term I'm converting the stored data to an array of characters because of time constraints but I'll read into the link you gave me as soon as time permits. again thank you.
Was This Post Helpful? 0

Page 1 of 1