c++ - File I/O operations - strange character entry? -


i using str.erase() function in following program erase part of output. @ last getting strange output ����.

the content of file current name of file = abcd-1234

here 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 open file";   return 0; } 

and output of program is

current name of file = abcd-1234 abcd-1234  ����  run finished; exit value 0; real time: 10ms; user: 0ms; system: 0ms 

you checking eof() before reading input. modify loop follows:

while ( 1 ) {     getline (myfile,line);     if ( myfile.eof() )         break;      // rest of loop } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

IIS->Tomcat Redirect: multiple worker with default -