zoukankan      html  css  js  c++  java
  • 传指针参数以及读写文件

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

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

    #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。

  • 相关阅读:
    POJ 3253 Fence Repair
    POJ 2431 Expedition
    NYOJ 269 VF
    NYOJ 456 邮票分你一半
    划分数问题 DP
    HDU 1253 胜利大逃亡
    NYOJ 294 Bot Trust
    NYOJ 36 最长公共子序列
    HDU 1555 How many days?
    01背包 (大数据)
  • 原文地址:https://www.cnblogs.com/smart-zihan/p/13434286.html
Copyright © 2011-2022 走看看