Code:
/*
Write a program that reads an input text file,
identifying and auto-correcting the following common typing errors:
‘Space before comma or a full-stop ( period ) ’, ‘Multiple spaces as opposed to
a single space’, and ‘Missing spaces’.
* */
#include <iostream>
#include <fstream>
#include <cstring>
#include <stdlib.h>
using std::cout;
using std::endl;
int main (int argc, const char **argv)
{
if( argc < 2)
{
printf("error: no file [ %s ]\n", argv[argc]);
exit(1);
}
std::ifstream infile(argv[1]);
std::ofstream temp;
temp.open("temp.txt");
std::string str;
if (infile.is_open()) {
char c;
int position = 0;
// get length of file:
infile.seekg (0, infile.end);
int length = infile.tellg();
cout<< "length is = "<<length<<endl;
infile.seekg (0, infile.beg);
while (infile.get(c))
{
if ((int)c == 32)
{
// get cur pos
position = infile.tellg();
//move back 2 to get c again to see
// what was before (curr c) it
infile.seekg(position-2, std::ios::beg);
infile.get(c);
if ( (int)c == 32 )
{
cout<<"Got extra space"<<endl;
//put it back where it was before hand
infile.seekg(position,std::ios::beg);
}
else // if no extra space, still needs to be put back
infile.seekg(position, std::ios::beg);
}
if ((int)c == 44)
{
position = infile.tellg();
infile.seekg(position-2, std::ios::beg);
infile.get(c);
if ( (int)c == 32 )
{
cout<<"Got extra space before comma"<<endl;
infile.seekg(position,std::ios::beg);
}
else
infile.seekg(position, std::ios::beg);
}
if ((int)c == 46)
{
position = infile.tellg();
infile.seekg(position-2, std::ios::beg);
infile.get(c);
if ( (int)c == 32 )
{
cout<<"Got extra space before period"<<endl;
infile.seekg(position,std::ios::beg);
}
else
infile.seekg(position, std::ios::beg);
}
// and it does not work yet.
if ( (int)c == 10) {
// cout<<"END LINE ";
temp << '\n'; }
else if ( (int)c == 32 )
temp << " ";
else
temp << c;
} // end while
} // end if
infile.close();
temp.close();
//remove(argv[1]);
//rename("temp.txt", argv[1]);
//std::ifstream tempfile(argv[1]);
std::ifstream tempfile("temp.txt");
if (tempfile.is_open()) {
while (true)
{
tempfile >> str;
if (tempfile.eof()) break;
cout << str ;
}
}
tempfile.close();
return 0;
}
I am lacking in knowing how to get it back in another file the way it came out, and it is getting extra letters put into that temp file, and as I have discovered this method removes white space and end line. and trying to put it back like I am does not work.
Code:
$ ./auto_correct auto_correct_test_file
length is = 53
Got extra space before comma
Got extra space
Got extra space before period
Got extra space
Got extra space
Got extra space
Got extra space
Thiss,wassmyytesttfileeanddhereeweegooagainn
test file
Code:
This , was my test file .
and here we go again.