金明仕饮料机械报价:c++高手帮忙看看。。。关于文本文件字符串获取,执行后出来都是乱码!!!

来源:百度文库 编辑:神马品牌网 时间:2024/05/02 03:39:13
#include <iostream>
#include <fstream.h>
#include <stdio.h>
#include <string>
int main()
{int a;

char buffer[17];int i;
ifstream file("d:\a.txt",ios::in);
while(!file.eof()&&i<sizeof buffer)
file.get(buffer[i++]);
std::string b;
b.append(buffer);
std::cout<<b;
std::cin>>a;
}
改成 ifstream file("d:\\a.txt",ios::in); 依旧是乱码。。。
b.append(buffer);可以将字符数组转化为字符串。。。我试过。。。

把cout<<b;
改成
char * str=(char *)b.getdata();
cout<<str;
原因b是string对象,存储的是string对象的地址而不是其字符串的地址

ifstream file("d:\a.txt",ios::in); 改成 ifstream file("d:\\a.txt",ios::in);

这样你才算读到了文件 最先的代码没能正确地打开文件 当然读不到有效的数据 所有是乱码
在字符串中,右斜杠要用\\表示