Having trouble reading/writing to text file with my class object

I have a project for my data structure class, and on of the things I have to add to my class is a read and write function. I need to be able to read from a text file supplied by my instructor and be able to write out to a text file the strings sorted. The input file is structured so that there are two strings on each line, with the first being the "key" and the item I must sort, and the second being the data which the unique key corresponds to.

Something like:

(key) (data)
12345678 I go to the store

The strings ASCII characters are read in from a file and my STRING class object is set to be equal to these read in characters. My STRING class is more or less the same (at least conceptually) as the built in string type.

STRING.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef _MYSTRING_H_ 
      #define _MYSTRING_H_ 

      #include <iostream>
      #include <fstream>
      
      using namespace std;

 class STRING
{
  private:
              
  char* _s; 
  unsigned _len; 
        
  public:    
//bunch of member functions that all have been tested well enough so that 
//I am confident they work...

  friend ostream& operator<< ( ostream& os, const STRING& s);
  //PRE:
  //POST: Inputs all values pointed to by s._s into the ostream object os.
        
                                      //in          //out 
  friend istream& operator>> ( istream& is, STRING& s);
  //PRE:
  //POST: Inputs up to 1025 values from is >> s. 
        
        
  void write ( ostream& os ) const;

  void read ( istream& is );
};

#endi 


STRING.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//lots of code for lots of member functions...

ostream& operator<< ( ostream& os, const STRING& s)
{
    for (int i = 0; i < s._len; i++)
    {
        os << s._s[i];
    }   
    
    os << '\0';  
    
    return os;  
} 

istream& operator>> ( istream& is, STRING& s)
{
    char c[1025];
    int i = 0;
    is >> c;
    
    s = STRING(c);
    return is;
}

void STRING::write ( ostream& os ) const
{
    os << '\t' << *this << '\t';
    
}

void STRING::read ( istream& is )
{
    char letter;
    is >> letter;
    while (is.good() &&  letter != '\t')
    {
        *this += letter;
    }
   
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include "BST.h"


int main()
{
    STRING s = "abcd";
    ofstream myfile;
    myfile.open ("CISP430records2.dat", ios::in | ios::out);
    s.write(myfile);
    
    cout << s << endl;
    
    cin.get();
    return 0;
}


output

abcd


CISP430records2.dat is not in my program directory.

The file I am supposed to be reading from I have uploaded here, as well as an example of a sorted keys file provided by my instructor. Its pretty big as it has a lot of data in it.

CISP430.dat <- input file
http://www.mediafire.com/?jeu646d03jv461d

DEFSortedKeys.txt <-output file
http://www.mediafire.com/view/?bddvgxz1b5widdd

The input file is titled "CISP430.dat". Its first four entries copied from word pad:

   Voters have prepared my ball.
   P1kAlMiG2K
   People will bet my closet.
   b7FzP5tM1Q   Voters have made a hallway?
   BI6DSS92c3   Men have washed my breakfast?
   1ApgJk9lVK]   You will paint my nest.


Example output file called "sorted keys". I need to be able to write a file that looks like this one:


001bwXTrhK
001KBQE1FJ
001TLZNZ60
002T1LfEWI
002tHOpOER
003Nb3hKLW
003OJ3LBOp
004COC1FnK
004Kg98cnE


I know that operator << for ostream is the way it is supposed to be, and that operator >> for istream is good enough. These I have confirmed by my instructor. I don't know how to write the read and write functions. Its really a syntax issues, because I don't know which commands to issue and I don't know how to "catch" the passed file name for both reading and writing. What I have in my cod e compiles but doesn't work.

This is a small part of a larger project I need to complete. I really don't know how to go about doing this one part.

I appreciate any help at all, in the form of instruction or links to some pages on the topic. Thank you so much.

My full STRING class I have uploaded here:
http://www.mediafire.com/?oj5dbkn3ajqbedp
http://www.mediafire.com/?javc9ku2kumu5te
http://www.mediafire.com/?o6c38dhbqoqrcmd
Last edited on
Anybody? Need help reading and writing from ostream/istream.
Topic archived. No new replies allowed.