Here is my code.
// Rids a .cpp file's white-space (except spaces) and comments (reduces memory)
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
cout << "This program rids a C, C Header, C++, or C++ Header file's white-space." << endl;
cout << "The file should be located in the same directory (folder) as this program." << endl;
cout << "Enter the filename: ";
string filename;
getline(cin, filename);
ifstream infile;
infile.open(filename.c_str());
if(infile.fail() == true)
{
cout << "Error in opening the file \"" << filename << "\"." << endl;
return 0;
}
string currentLine;
int line = 1;
for(; infile.eof() == false; line++)
{
cout << filename << " | Getting line " << line << endl;
getline(infile, currentLine);
cout << filename << " | Parsing line " << line << endl;
int charNumber = 0;
for(; charNumber != currentLine.length(); charNumber++)
{
// If a carriage-return is encountered, and it is not a preprocessor-directive* line, delete it
if(currentLine[charNumber] == 13 && currentLine[0] != '#')
{
// If the char before the newline is an escape-sequence
if(currentLine[charNumber-1] == '\\') currentLine[charNumber-1] == ' ';
currentLine[charNumber] = ' '; // 'X' to see if the file was visibly changed.
}
}
}
}
// *Assumes that there is no white-space before the hash-sign (#) for the preprocessor-directive line
Note: It does not remove comments yet either.
This post has been edited by hulla: 03 October 2011 - 09:39 PM

New Topic/Question
Reply




MultiQuote





|