Why isn't the whitespace removed?

L

Linuxguy

Hi guys

I need your help.. I am trying to read a line from a file and remove
whitespaces from it but it doesn't work..

thanks

my code;



#include <iostream>
#include <string>
#include <fstream>

using namespace std;
void rm_white(string &str);
int main()
{
char chr;
ifstream infile;
string line;
int k=0;
int i=0;
infile.open ("test");
while(getline(infile,line))

cout<<line; //test

rm_white(line);

cout<<line<<endl;

return 0;
}
void rm_white(string &str)
{
str.erase(0,str12.find_first_not_of(" \t\r\n\f"));
}
 
U

usr.root

Linuxguy 写é“:
Hi guys

I need your help.. I am trying to read a line from a file and remove
whitespaces from it but it doesn't work..

thanks

my code;



#include <iostream>
#include <string>
#include <fstream>

using namespace std;
void rm_white(string &str);
int main()
{
char chr;
ifstream infile;
string line;
int k=0;
int i=0;
infile.open ("test");
while(getline(infile,line))

I think there must be a {} in your code,because there isn't only one
line in your file .
like this :
while(getline(infile,line))
{
cout<<line; //test
rm_white(line);
cout<<line<<endl;
}
 
P

puzzlecracker

try this:
#include <iostream>
#include <string>
#include <fstream>
#include<functional>
#include <algorithm>

using namespace std;
void rm_white(string &str);
int main()
{
char chr;
ifstream infile;
string line;
int k=0;
int i=0;
infile.open ("test");
while(getline(infile,line))
{

cout<<line; //test


rm_white(line);


cout<<line<<endl;

}
return 0;

}

void rm_white(string &str) {
str.erase(std::remove_if(str.begin(), str.end(),
std::bind2nd(std::equal_to<char>(), ' ')), s.end());
}
 
O

Old Wolf

Linuxguy said:
I need your help.. I am trying to read a line from a file and
remove whitespaces from it but it doesn't work..

What do you mean by "doesn't work" ? Doesn't compile? Produces
unexpected output?
int main()
{
char chr;
ifstream infile;
string line;
int k=0;
int i=0;
infile.open ("test");
while(getline(infile,line))

cout<<line; //test

rm_white(line);

cout<<line<<endl;

return 0;
}

If we indent and bracket your code properly, we get:

while(getline(infile,line))
{
cout<<line;
}
rm_white(line);
cout<<line<<endl;

So you can see that it outputs all of the valid lines in the
file, and then removes whitespace on the last line, and then
outputs that last line again. Is this what you were seeing?

I think what you meant was to have { } around all three of
the statements following the 'while' .
void rm_white(string &str)
{
str.erase(0,str12.find_first_not_of(" \t\r\n\f"));
}

Note: your function, at best, will only remove leading
whitespace -- not all whitespace.

This may do funny things if the string doesn't contain any
whitespace characters. (In fact it doesn't even compile,
because str12 is undefined). Instead:

string::size_type pos = str.find_first_not_of(" \t\r\n\f");
if (pos != string::npos)
str.erase(0, pos);
 
K

Kristo

puzzlecracker said:
void rm_white(string &str) {
str.erase(std::remove_if(str.begin(), str.end(),
std::bind2nd(std::equal_to<char>(), ' ')), s.end());
}

To save typing, you could also use the C function isspace for the
predicate in remove_if. isspace will remove any whitespace character,
not just literal spaces. If my documentation is correct, you can find
isspace in the header <cctype>.

Kristo
 
L

Linuxguy

Thanks for everybody who has repiled. I changed my code little bit but
still I can not remove whitespaces...

new code;

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
void rmwhites(string &mystring);
int main()
{
ifstream infile;
string line;
infile.open ("test");
while(getline(infile,line))
{
cout<<line<<endl;
}

rmwhites(line);

cout<<line<<endl;

return 0;
}
void rmwhites(string &mystring)
{
mystring.erase(0,mystring.find_first_not_of(" \t\r\n\f"));
}

input file;
<test> testing</test>
<test2> testing </test2>

output that I got after run my program (samething)
<test> testing</test>
<test2> testing </test2>
 
K

Karl Heinz Buchegger

Linuxguy said:
Thanks for everybody who has repiled. I changed my code little bit but
still I can not remove whitespaces...

new code;

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
void rmwhites(string &mystring);
int main()
{
ifstream infile;
string line;
infile.open ("test");
while(getline(infile,line))
{
cout<<line<<endl;
}

rmwhites(line);

The call to rmwhites() is still not in the loop!
You still read lines, print them. And after the
last line has been processed you call rmwhitees.
 
L

Linuxguy

Thanks Karl

I put rmwhites() in my while loop but now my program doesn't remove all
spaces as old wolf told. I think I should think how I can remove all
spaces..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,795
Messages
2,569,644
Members
45,356
Latest member
deepthi.kodakandla

Latest Threads

Top