• 传指针参数以及读写文件


    这是一些需要经常用到的方法,回过头来用时总会出现一些小错误,还是记录下来,以后照模板用好了。

    首先,传入指针参数,然后从中获值

    #include "pch.h"
    #include <iostream>
    
    using namespace std;
    
    int get_times(char* times) {
        int i = 3;
        sprintf_s(times, 5, "%d", i);
    
        return 0;
    }
    
    int main()
    {
        char times[5];
        get_times(times);
        printf("%s
    ", times);
    }

    接着,读写文件

    #include "pch.h"
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    void read_file() {
        ifstream myfile("aaa.txt");
        if (!myfile.is_open()) {
            cout << "can not open this file" << endl;
        }
        else {
            char buff[128] = { 0 };
            myfile.read(buff, sizeof(buff));
            printf("%s
    ", buff);
        }
        myfile.close();
    }
    
    void write_file() {
        ofstream out("aaa.txt");
        if (!out.is_open()) {
            cout << "can not open this file" << endl;
        }
        else {
            out << 222;
        }
        out.close();
    }
    
    int main()
    {
        read_file();
        write_file();
    }

    OK。

  • 相关阅读:
    HDU 2098 分拆素数和
    HDU 2034 *人见人爱A-B
    HDU 1236 排名(Microsoft_zzt)
    HDU 5702 Solving Order
    HDU 2033 人见人爱A+B
    HDU 2029 Palindromes _easy version
    HDU 2021 发工资咯:)
    HDU 2071 Max Num
    HDU 2039 三角形
    页面使用element-tree
  • 原文地址:https://www.cnblogs.com/smart-zihan/p/13434286.html
Copyright © 2020-2023  润新知