Thursday, July 9, 2009

C++ Problem?

Alright, I have to write a program that reads text from one file and writes it to a separate file, but takes out every string of two or more consecutive spaces and replaces it with one space. Here's what I have so far, but it doesn't work...can anyone help me?





#include %26lt;iostream%26gt;


#include %26lt;cmath%26gt;


#include %26lt;cstdlib%26gt;


#include %26lt;fstream%26gt;





int main()





{


using namespace std;





ifstream in_data;


ofstream out_data;





cout%26lt;%26lt;"Begin editing files."%26lt;%26lt;endl;





in_data.open("data_prob_6_p369.txt");


if (in_data.fail( ))


{


cout%26lt;%26lt;"Input file could not open."%26lt;%26lt;endl;


exit(1);


}


out_data.open("edited_data_prob_6_p369...


char c;





in_data.get(c);


while(!in_data.eof( ))


{


if(isspace(c))


{


out_data%26lt;%26lt;" ";


in_data.get(c);


while(isspace(c))


{


in_data.get(c);


}


}


out_data%26lt;%26lt;c;


in_data.get(c);


}


in_data.close( );


out_data.close( );





cout%26lt;%26lt;"Done editing files."%26lt;%26lt;endl;





system("pause");





return 0;


}

C++ Problem?
#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;





using namespace std;





int main()


{





ifstream in_data("data_prob_6_p369.txt");


if (! in_data)


{


cout%26lt;%26lt;"Input file could not open."%26lt;%26lt;endl;


exit(1);


}





ofstream out_data("edited_data_prob_6_p369");


if (! in_data)


{


cout%26lt;%26lt;"Output file could not open."%26lt;%26lt;endl;


exit(1);


}





cout%26lt;%26lt;"Begin editing files."%26lt;%26lt;endl;





string str;





while (getline(in_data, str))


{


string::size_type x;





while ((x = str.find(" ")) != string::npos)


{


str.erase(x, 1);


}





out_data %26lt;%26lt; str %26lt;%26lt; endl;


}





cout%26lt;%26lt;"Done editing files."%26lt;%26lt; endl;





in_data.close();


out_data.close();





system("pause");





return 0;


}


No comments:

Post a Comment