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;
}