C++读写本地文件(文件IO流)

我们在开发项目过程中经常会写一些项目的配置信息
今天记录一下C++的IO流,其实很简单

首先我用C++打开一个C盘的txt文件并且读取出来,
然后在结尾写要给END

如图所示:
这里写图片描述

第一步:
首先要在c/c++的预处理器定义中添加
添加_CRT_SECURE_NO_WARNINGS

#include "stdafx.h"
#include<cstdlib>
#include<cstdio>
#include<stdio.h>
#include<iostream>
using namespace std;

int main(int argc,char* argv[])
{
    FILE *pFile = fopen("C:\\a.txt", "r+");         //r代表以只读方式打开文件,详情百度fopen函数

    if (NULL == pFile)
    {
        cout << "打开文件失败" << endl;
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    char str[256];
    while (EOF != fscanf(pFile, "%s", str))     //如果读取不到文件结束符
    {
        cout << str << endl;
    }

    fprintf(pFile, "END");          //写入END
    fclose(pFile);                  //关闭文件

    system("PAUSE");
    return EXIT_SUCCESS;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36409711/article/details/79313260