다음 프로그램에서 str.erase()
함수를 사용하여 출력의 특정 부분을 지울 수 있습니다. 하지만 마지막으로 나는 이상한 결과를 얻고있다. ����
.파일 I/O 작업 - 이상한 문자 입력?
내 파일의 내용은 다음 Current name of the file = ABCD-1234
내 코드입니다 :
#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;
}
그리고 내 프로그램의 출력은 입력을 읽기 전에
Current name of the file = ABCD-1234
ABCD-1234
����
RUN FINISHED; exit value 0; real time: 10ms; user: 0ms; system: 0ms
실제 코드를 표시 할 수 있습니까? 'line'은 어디에도 선언되어 있지 않습니다. –
@JesseGood 수정 된 코드를 지금 확인하십시오 ... – highlander141
'getline'의 반환 값을 확인하지 않고'eof'를 사용하면 잘못된 방법입니다. 줄의 길이가'str.end() -9'가 유효하기에 충분한 지 점검하지 않는다. –