hairE -3 Newbie Poster

Well this says 'solved', but i came across it with the very same homework problem. I attached my code, which works correctly and is pretty simple. Figure other students will have this problem in the future. Included the essentials.

int main()
{
	char line[250];                      //Array
	const int LIMIT = 250;
	ifstream inFile;
	char ch;
	

	inFile.open("CPPHumor.txt");
	if (inFile.fail())
	{
		 cout << "\nThe input file was not successfully opened"
		 << "\n Please check that the file currently exists."
		 << endl;
		 exit(1);
     
    }
	ofstream outFile;

	outFile.open("RemoveLdWS.txt");
	if (outFile.fail())
	{
		 cout << "\nThe output file was not successfully opened"
		 << "\n Please check that the file currently exists."
		 << endl;
         exit(1);
    }
	while(inFile.eof() == false)    //end of file?
	{
		 if(inFile.peek() == '\n')       //Check for blank line(double spacing)
		 {
			 inFile.get(ch);   
			 outFile << ch;  //if so, print the blank line
		 }
		 else
		 {
			 inFile.get(ch);
			 while(ch == ' ')
			 {
				inFile.get(ch);           //move pointer to first non whitespace character
			 }
			inFile.get(line, LIMIT);       //read rest of line
			outFile << ch << line;       //print first non- ws char as well as the rest of the line.
		  }
     }

	cout <<"File written successfully";
	

	inFile.close();
	outFile.close();
	cin.get();
	return 0;
}

*****The second part of the problem was to also remove the blank lines. For this, simply remove the outfile << ch after checking if the first character is a newline character. So after ** if(peek() == '\n') ** remove the statement to print the blank line to your output file. This will produce no leading whitespaces or blank lines.

jonsca commented: Don't bump old thread, start your own -1
iamthwee commented: eof? -> No -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.