#include
#include
#include
#include
#include
using namespace std;
int main()
{
ifstream sfile("1.txt");
if (!sfile)
{
cerr << "无法打开文件" << endl;
system("pause");
return -1;
}
string line;
vector<string> word;
while (getline(sfile, line))
{
word.push_back(line);
}
sfile.close();
//for (auto it :word)
vector<string>::const_iterator it = word.begin();
while (it != word.end())
{
istringstream line_str(*it);
string words;
while (line_str >> words)
cout << words << " ";
cout << endl;
++it;
}
system("pause");
return 0;
}
你将文件编码修改一下
应该是字符集问题,因为txt文件中存储的GBK字符集,你读取的时候使用你指定的字符集进行转换,的出来的就会是正常了,希望能帮到您!
void mian()
{
stream = fopen("1.txt", "r");
if (stream == NULL)
printf("The file 1.txt was not opened\n");
else
{
/*fprintf(stream, "%s %ld %f %c", "helloworld",
65, 3.14, 'c');*/
/*Set pointer to beginning of file:*/
fseek(stream, 0, SEEK_SET);
char ch;
while ((ch = getc(stream)) != EOF)
putchar(ch);
}
}
用fopen()函数,输出的也是这种乱码,不知道哪里出现了问题
编码问题,如果是全部乱码,应该输入字符的编码与输出编码不一致;如果是中文乱码,应该是输出编码不支持中文。
不要用txt 换个编辑本文文件
应该跟你的vc版本语言有关
应该是输入字符的编码与输出编码不一致
用UltraEdit 打开txt,单步调试看你读取的内容。比对16进制数据,看看读取的是否一致
getline(sfile, line);这条指令,sfile内容提示读取字符串内容出错!!!!这是什么原因啊!!!
读取时不时可以设置编码形式吗?
文件编码与控制台编码不一致,最简单方式,修改文件编码,改为ANSI或者GBK,应该就能正常打印了
谢谢各位,应该是我电脑上安装了加密软件的原因,换了一台未加密的电脑试了一下,可以正常读取文件并输出到控制台。
很可能是你的txt文件的编码格式的问题,试试将txt文件的编码格式改成unicode
你看一下,自己原源文件格式是哪一种,如果不能修改原文文件或者是禁止修改,可以在代码中进行转码