C++向文件中写入数据(内有注释)

我目前是一名在校学生,文章仅仅作为自己记录笔记和参考,各位大佬看到如有不足之处,还请多多指教,感谢.(侵删)

#include<iostream>
#include<fstream>

using namespace std;

int main()
{
    
    
	char szAutoRun[] = "[AutoRun]\
	\r\nopen=notepad.exe \
	\r\nhsell\\open=打开(&O)\
	\r\nshell\\open\\Command=notepad.exe \
	\r\nshell\\explore=资源管理器(&X) \
	\r\nshellexecute=notepad.exe \
	\r\nshell\\Auto\\Command=notepad.exe";
	//文件内容可自行更改
	/*关于autorun.inf文件
	open=命令行--指定设备启用时运行至命令行
	shell/关键字/Command=命令行--定义设备右键菜单执行命令行
	shell/关键字=文本--定义设备右键菜单文本
	shellexecute=执行文件路径名[参数]--指定设备启动时执行文件
	autorun文件代码参考至C++黑客编程
	注释参考至CSDN朗涯技术博客文章*/


	ofstream outfile;//定义输入文件类
	outfile.open("C:\\ASSASSIN.txt");//括号里面为写入文件的目的地址

	outfile << szAutoRun << endl;//向文件写入数据

	ifstream infile;//定义输出文件类
	infile.open("C:\\ASSASSIN.txt");

	infile >> szAutoRun;//读取数据
	for (const auto& str : szAutoRun)
		cout << str;//显示数据

	infile.close();//关闭文件

	getchar();
	return 0;
}

最后附上运行截图:
在这里插入图片描述
文件操作的三个类:
ofstream,向文件写入数据类
ifstream,向文件读取数据类
fstream,可同时读写的文件类
(不足之处还请大佬们多多指教)
本文参考博客链接:
https://my.oschina.net/u/2245781/blog/983741
http://blog.csdn.net/mafuli007/article/details/7271975

おすすめ

転載: blog.csdn.net/qq_46282869/article/details/121242643