Reputation: 1725
I am using the str.erase()
function in the following program to erase certain part of output. But at the last I am getting a strange output like this ����
.
The content of my file is Current name of the file = ABCD-1234
Here is my code:
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
//std::ifstream;
int main ()
{
string line;
ifstream myfile ("/home/highlander141/NetBeansProjects/erase_remove/dist/Debug/GNU-Linux-x86/abc.txt");
if (myfile.is_open())
{
while ( !myfile.eof() ) //myfile.good()
{
getline (myfile,line);
//line = myfile.get();
//if(!myfile.eof())
cout << line <<endl;
std::string str (line);
str.erase (str.begin()+0, str.end()-9);
std::cout << str << endl;
}
myfile.close();
//remove("/home/highlander141/NetBeansProjects/erase_remove/dist/Debug/GNU-Linux-x86/abc.txt");
}
else cout << "Unable to open file";
return 0;
}
And output of my program is
Current name of the file = ABCD-1234
ABCD-1234
����
RUN FINISHED; exit value 0; real time: 10ms; user: 0ms; system: 0ms
Upvotes: 1
Views: 126
Reputation: 8839
You are checking for eof()
before reading the input. Modify the loop as follows:
while ( 1 )
{
getline (myfile,line);
if ( myfile.eof() )
break;
// Rest of the loop
}
Upvotes: 2