传指针参数以及读写文件

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

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

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

原文地址:https://www.cnblogs.com/smart-zihan/p/13434286.html