3 Replies - 3562 Views - Last Post: 04 October 2011 - 06:11 AM Rate Topic: -----

#1 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Why does this program not work properly?

Posted 03 October 2011 - 09:38 PM

The program compiles fine. It's supposed remove the white-space (currently only newlines) of a file (preferably .cpp) using file streaming. It opens the file and gets each line of the file right but then it does not work properly. It does not exchange the newlines for a space.
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


Is This A Good Question/Topic? 0
  • +

Replies To: Why does this program not work properly?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: Why does this program not work properly?

Posted 04 October 2011 - 04:10 AM

If you read the manual, I believe your problem is this:

Quote

If the delimiter is found, it is extracted and discarded, i.e. it is not stored and the next input operation will begin after it.

Was This Post Helpful? 1
  • +
  • -

#3 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Why does this program not work properly?

Posted 04 October 2011 - 04:14 AM

Oh thank you. I'll find a way to work around this problem.
Was This Post Helpful? 0
  • +
  • -

#4 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Why does this program not work properly?

Posted 04 October 2011 - 06:11 AM

The program works almost as I want it to, except it does not print a newline for preprocessor directive lines, even if I concatenate the newline escape sequence on the newline lines. I made all the lines get stored to a large string array so that I could then copy each element of the string array to the file again. 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;
	}
	unsigned int line = 1;
	string linesArray[5000];
	for(; infile.eof() == false; line++)
	{
	    // Get line
		cout << filename << " | Getting line " << line << endl;
        string currentLine;
		getline(infile, currentLine);
        // Parse line
		cout << filename << " | Parsing line " << line << endl;
		unsigned int currentCharNum = 0;
        // Checks if line is a preprocessor directive
        if(currentLine[0] == '#') currentLine += "\n";
        for(; currentLine[currentCharNum] == ' ' && currentCharNum + 1 != currentLine.length(); currentCharNum++)
        {
            if(currentLine[currentCharNum] == '#')
            {
                currentLine += "\n";
                break;
            }
        }
        // Checks if line is a comment-line
        currentCharNum = 0;
		for(; currentLine[currentCharNum] == ' ' && currentCharNum + 1 != currentLine.length(); currentCharNum++)
		{
		    if(currentLine[currentCharNum] == '/' && currentLine[currentCharNum + 1] == '/') continue;
		}
		if(currentLine[currentLine.length() - 1] == '\\') currentLine[currentLine.length() - 1] == ' ';
        // Store line in the array that holds the lines
		cout << filename << " | Storing line " << line << endl;
		linesArray[line - 1] = currentLine + ' ';

    }
    unsigned int line2 = 1;
    ofstream outfile;
    outfile.open(filename.c_str());
    for(; line2 != line; line2++)
    {
        cout << filename << " | Pasting line " << line2 << endl;
        outfile << linesArray[line2];
    }
    cout << filename << " has had it's newlines removed." << endl;
    return 0;
}


This post has been edited by hulla: 04 October 2011 - 06:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1