delet the linenumber
Apr 8, 2013 at 1:27am UTC
I have some problem in using the fstream.
my objective is to delet the line number : such as a line 1.#include <iostream>
will be converted to #include <iostream> in the out. txt.
However, it doesn't work.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string str;
void filter(string&str)
{
while (str.size()>0&&str[0]>='0' &&str[0]<=9||str[0]=='.' )
{
int length=str.length();
for (int i=1;i<length; i++)
str[i-1]=str[i];
str[length-1]='\0' ;
}
}
int main()
{ cout<<"the filter is initializing" <<endl;
ifstream infile("in.txt" );
ofstream outfile("out.txt" );
while (getline(infile,str))
{
filter(str);
for (int i=0;i<str.length();i++)
{ cout<<str[i];
cout<<endl;
outfile<<str;
}
}
infile.close();
outfile.close();
system("pause" );
return 0;
}
Last edited on Apr 8, 2013 at 2:03pm UTC
Apr 8, 2013 at 1:32am UTC
You are not doing any checks. You need to put a check in filter() and then return a boolean (true on success, false on failure)
Apr 8, 2013 at 3:46am UTC
Out of curiosity, what does this print?
1 2 3 4
double x;
string str;
while (infile >> x && getline(infile, str))
cout << str << '\n' ;
How does it handle the decimal? Do you break infile?
That line of thinking would be the way I would do it:
1 2 3 4 5 6
int x;
char decimal;
string line;
while ((infile >> x >> decimal) && getline(infile, str))
cout << str << '\n' ;
~most sites have an ability to turn the line numbers off~
Last edited on Apr 8, 2013 at 3:48am UTC
Apr 8, 2013 at 2:07pm UTC
I think this would work.thanks
1 2 3 4 5 6
int x;
char decimal;
string line;
while ((infile >> x >> decimal) && getline(infile, str))
cout << str << '\n' ;
Topic archived. No new replies allowed.