文件 I/O 操作 - 奇怪的字符输入
File I/O operations - strange character entry?
我在以下程序中使用 str.erase()
函数来擦除输出的某些部分。但是最后我得到了像这样的奇怪输出����
.
我的文件内容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
在读取输入之前,您正在检查eof()
。 修改循环,如下所示:
while ( 1 )
{
getline (myfile,line);
if ( myfile.eof() )
break;
// Rest of the loop
}
相关文章:
- 从格式化输入操作中获取读取字符数
- 如何忽略第一个字符输入后的所有内容
- 使用单个变量对字符输入进行计数
- 确定用户字符输入 C++ 的结束
- 将单个字符串/字符输入串行监视器
- 如何将字符输入限制为仅获取一个字符串"cin"
- C++:使用 fgets() 读取字符输入时出错
- 如何拒绝 cin 中的字符输入并定义最小和最大整数值
- C++ 字符输入仅输出第一个字母
- 限制 2D 数组中的字符输入
- 循环直到整数输入在所需范围内无法处理非数字字符输入
- 如何在字符级别操作Unicode字符串
- C++常量字符*输入
- 重载标头或.cpp中的输入操作>>
- 使用带符号字符输入的isalnum-Visual C++
- 限制C++中的字符输入
- 字符输入问题
- 字符** + 输入" "字符
- 文件 I/O 操作 - 奇怪的字符输入
- 为什么我的流输入操作会被跳过