文件输入流输出流测试

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    string data;
    fstream  afile;
    afile.open("afile.txt", ios::in);
    int option1,option2;
    string A, B;
    afile >> option1 >> A;
    afile >> option2 >> B;
    cout << option1 << " " << A << endl;
    cout << option2 << " " << B << endl;
    afile.close();
    system("pause");
    return 0;
}

注意字符串编码,必须是ASCI,WIN10记事本默认是UTF-8,另存为里转一下就好

可以自己写一个转码函数 参考博客

#include <fstream>
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;

string UTF8ToGB(const char* str)
{
     string result;
     WCHAR *strSrc;
     LPSTR szRes;

     //获得临时变量的大小
     int i = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
     strSrc = new WCHAR[i+1];
     MultiByteToWideChar(CP_UTF8, 0, str, -1, strSrc, i);

     //获得临时变量的大小
     i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
     szRes = new CHAR[i+1];
     WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, i, NULL, NULL);

     result = szRes;
     delete []strSrc;
     delete []szRes;

     return result;
}

int main()
{   
    char txt[100];
    string msg;
    ifstream infile;
    infile.open("2.txt");

    if(!infile.is_open())
    {
        cout<<""<<endl;
        exit(0);
    }

    while(!infile.eof())
    {       
        infile.getline(txt,100);
        msg=UTF8ToGB(txt);
        cout<<msg<<endl;

    }

    infile.close();
    getchar();
}
--------------------- 
作者:ZhanCF 
来源:CSDN 
原文:https://blog.csdn.net/zhancf/article/details/49930969 
版权声明:本文为博主原创文章,转载请附上博文链接!
View Code
原文地址:https://www.cnblogs.com/robotpaul/p/10231598.html