zoukankan      html  css  js  c++  java
  • 文件输入流输出流测试

    #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
  • 相关阅读:
    vue Syntax Error: Unexpected token {
    MQ 分拆Json数据包然后上传
    京东商城投诉商家
    C# 读写Txt文件
    DB2时间函数 实现 时间加减
    VS恢复默认设置
    只用一次循环开销 将类似 1 A 、1 B 的数据返回成为 1 A,B 的拼接形式
    DB2 With语句递归
    属性与字段的区别
    With语句在数据统计应用
  • 原文地址:https://www.cnblogs.com/robotpaul/p/10231598.html
Copyright © 2011-2022 走看看