Thread: io stream and string class problems

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    5

    io stream and string class problems

    Hi
    My program is eventually supposed to read in a .cpp file, ask the user if they want to get rid of the white space lines and/or single line comments, show the changed file to the screen and then ask if they want to save the file or not. I cannot get the program to get rid of the whitespace or comments. It will complie but I will get a run time error that will crash the program. Any help would be appreciated.

    Thanks

    header
    Code:
    #ifndef IOSTR_H_
    #define IOSTR_H_
    
    
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    
    
    class ioStr
    {
       private:
          char * str;
          int len;
    
          ifstream fins;
          ofstream fout;
    
          int numLines;
          
    
    
    
       public:
          ioStr(const char * s); // constructor
          ioStr();               // default constructor
          ioStr(const ioStr & ); // copy constructor
          ~ioStr();     
          ioStr & operator=(const ioStr &);
          ioStr & operator=(const char *);
    
          void OpenOutFile();
          void OpenInFile();
          void SetNumLines();
    
          int NumLine(){return numLines;}
    
          void Display();
          void DisplayNoCom();
          void DisplayNoWht();
    
          void CloseInFile(){fins.close(); fins.clear();}
          void CloseOuFile(){fout.close(); fout.clear();}
    
    
    
    
    
    };
    #endif
    implementation
    Code:
    
    #include "ioStr.h"
    
    
    
    ioStr::ioStr(const char * s)   // construct String from C string
    {
        len = strlen(s);            // set size
        str = new char[len + 1];      // allot storage
        strcpy(str, s);               // initialize pointer
    }
    
    ioStr::ioStr()      // default constructor
    {
        len = 0;
       numLines = 0;
        str = new char[1];
        str[0] = '\0';                 // default string
    
    }
                                           
    ioStr::ioStr(const ioStr & st) // Copy Constructor
    {
        len = st.len;              // same length
        str = new char [len + 1];  // allot space
        strcpy(str, st.str);       // copy string to new location
    }
    
    ioStr::~ioStr()         // necessary destructor
    {
        delete [] str;                    // required
    }   
    
        // assign a String to a String
    ioStr & ioStr::operator=(const ioStr & st)
    {
       if (this == &st)
            return *this;
        delete [] str;
        len = st.len;
        str = new char[len + 1];
        strcpy(str, st.str);
        return *this;
    }
    
        // assign a C string to a String
    ioStr & ioStr::operator=(const char * s)
    {
       delete [] str;
        len = strlen(s);
        str = new char[len + 1];
        strcpy(str, s);
        return *this;
    }
    
    
    void ioStr :: OpenInFile()
    {
       fins.open("c:\\temp\\source.cpp");
        if ( ! fins.is_open())
        {
            cout << "Input file opening failed.\n";
    //      fail = true;
        }
    }
    
    void ioStr :: SetNumLines()
    {
       string temp;
    
       while(! fins.eof())
       {
          getline(fins, temp, '\n');
          numLines++;
       }
    }
    void ioStr :: OpenOutFile()
    {
        fout.open("c:\\temp\\newsource.cpp");
        if (! fout.is_open())
        {
            cout << "Output file opening failed.\n";
       //   fail = true;
        }
    }
    
    void ioStr :: Display()
    {
       char ch;
    
       while((ch = fins.get()) != EOF )
             cout << ch;
    }
    
    void ioStr :: DisplayNoCom()
    {
       for (int i = 0;i<=numLines;i++)
       {
          //fins.get(str, 120,' ');
          //fins.get();
          
          
    //      fins.get(str);
          len = strlen(str);
          if((str[1] != '/') && (str[2] !='/'))
          cout<<str<<endl;      
       }
    }
    
    void ioStr :: DisplayNoWht()
    {
       for (int i = 0;i<=numLines;i++)
       {      
          fins.getline(str,1500);   
          for(int j =0; str[j]== '\n';j++)   
          if(isspace(str[j]) == false)
                cout<<str<<endl;
    
       }
    }

    test file
    Code:
    # include "ioStr.h"
    
    
    
    int main()
    {
       ioStr source;
       int num;
    
       source.OpenInFile();
       source.Display();
       source.CloseInFile();
       source.OpenInFile();
       source.SetNumLines();
       num= source.NumLine();
       
    
       cout<<endl<<num<<endl<<endl;
    
       source.CloseInFile();
       source.OpenInFile();
    
       //source.DisplayNoCom();
       source.DisplayNoWht();
    
    
    
    
    
    
    
    
    
    
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    5
    I've changed part of the implementation file. It will now get rid of the whitespace and comments but i still get a run time error. Any help would be great!
    Code:
    #include "ioStr.h"
    
    
    
    ioStr::ioStr(const char * s)   // construct String from C string
    {
        len = strlen(s);            // set size
        str = new char[len + 1];      // allot storage
        strcpy(str, s);               // initialize pointer
    }
    
    ioStr::ioStr()      // default constructor
    {
        len = 0;
       numLines = 0;
        str = new char[1];
        str[0] = '\0';                 // default string
    
    }
                                           
    ioStr::ioStr(const ioStr & st) // Copy Constructor
    {
        len = st.len;              // same length
        str = new char [len + 1];  // allot space
        strcpy(str, st.str);       // copy string to new location
    }
    
    ioStr::~ioStr()         // necessary destructor
    {
        delete [] str;                    // required
    }   
    
        // assign a String to a String
    ioStr & ioStr::operator=(const ioStr & st)
    {
       if (this == &st)
            return *this;
        delete [] str;
        len = st.len;
        str = new char[len + 1];
        strcpy(str, st.str);
        return *this;
    }
    
        // assign a C string to a String
    ioStr & ioStr::operator=(const char * s)
    {
       delete [] str;
        len = strlen(s);
        str = new char[len + 1];
        strcpy(str, s);
        return *this;
    }
    
    
    void ioStr :: OpenInFile()
    {
       fins.open("c:\\temp\\source.cpp");
        if ( ! fins.is_open())
        {
            cout << "Input file opening failed.\n";
    //      fail = true;
        }
    }
    
    void ioStr :: SetNumLines()
    {
       string temp;
    
       while(! fins.eof())
       {
          getline(fins, temp, '\n');
          numLines++;
       }
    }
    void ioStr :: OpenOutFile()
    {
        fout.open("c:\\temp\\newsource.cpp");
        if (! fout.is_open())
        {
            cout << "Output file opening failed.\n";
       //   fail = true;
        }
    }
    
    void ioStr :: Display()
    {
       char ch;
    
       while((ch = fins.get()) != EOF )
             cout << ch;
    }
    
    void ioStr :: DisplayNoCom()
    {
       int count;
       for (int i = 0;i<=numLines;i++)
       {
          fins.getline(str, 1000, '\n');
          if((str[0] != '/') && (str[1] !='/'))
          {
             cout<<str<<endl;
             count++;
          }
          
       }
       commentLines = numLines - count;
    }
    
    void ioStr :: DisplayNoWht()
    {
       for (int i = 0;i<=numLines;i++)
       {      
          fins.getline(str, 1000, '\n');
          fins.ignore(1000,' ');
          cout<<str<<endl;;
       }
    
    }

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    Above the for() in

    Code:
    void ioStr :: DisplayNoWht()
    {
       for (int i = 0;i<=numLines;i++)
       {      
          fins.getline(str, 1000, '\n');
          fins.ignore(1000,' ');
          cout<<str<<endl;;
       }
    }
    Add

    Code:
    str = new char [1000];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipe class.
    By eXeCuTeR in forum Linux Programming
    Replies: 8
    Last Post: 08-21-2008, 03:44 AM
  2. Can someone help with my seg fault?
    By John_L in forum C++ Programming
    Replies: 23
    Last Post: 03-01-2008, 04:04 PM
  3. Replies: 3
    Last Post: 12-28-2006, 04:18 PM
  4. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  5. IRC, reading the stream
    By Iyouboushi in forum C# Programming
    Replies: 6
    Last Post: 08-03-2006, 05:34 PM