如何在C++中读取和写入文本文件
How to read and write to a text file in C++?
大家好,我刚刚开始学习C++,我想知道如何读取和写入文本文件。我看过很多例子,但它们都很难理解/遵循,而且各不相同。我希望这里有人能帮忙。我是一个完全的初学者,所以我需要明确的说明。以下是我正在尝试执行的操作的示例:
#include <iostream>
#include <fstream>
using namespace std;
string usreq, usr, yn, usrenter;
int start ()
{
cout << "Welcome..."
int main ()
{
cout << "Is this your first time using TEST" << endl;
cin >> yn;
if (yn == "y")
{
ofstream iusrfile;
ofstream ousrfile;
iusrfile.open("usrfile.txt", "w");
iusrfile >> usr;
cout << iusrfile;
iusrfile.close();
cout << "Please type your Username. n";
cin >> usrenter;
if (usrenter == usr)
{
start ();
}
}
else
{
cout << "THAT IS NOT A REGISTERED USERNAME.";
}
return 0;
}
所需的头文件:
#include <iostream>
#include <fstream>
声明输入文件流:
ifstream in("in.txt");
声明输出文件流:
ofstream out("out.txt");
如果要使用变量作为文件名,而不是硬编码,请使用以下命令:
string file_name = "my_file.txt";
ifstream in2(file_name.c_str());
从文件读取到变量(假设文件有 2 个 int 变量):
int num1,num2;
in >> num1 >> num2;
或者,从文件中读取一行时间:
string line;
while(getline(in,line)){
//do something with the line
}
将变量写回文件:
out << num1 << num2;
关闭文件:
in.close();
out.close();
文件 IO 的默认 c++ 机制称为流。流可以有三种类型:输入、输出和输入输出。输入流的作用类似于数据源。要从输入流中读取数据,请使用>>
运算符:
istream >> my_variable; //This code will read a value from stream into your variable.
操作员>>
对不同类型的行为不同。如果在上面的例子中my_variable
是一个整数,那么将从 strem 中读取一个数字,如果my_variable
是一个字符串,那么将读取一个单词,依此类推。您可以通过编写 a、b 和 c 作为变量istream >> a >> b >> c;
从流中读取多个值。
输出流充当接收器,您可以将数据写入其中。若要将数据写入流,请使用<<
运算符。
ostream << my_variable; //This code will write a value from your variable into stream.
与输入流一样,您可以通过编写如下内容将多个值写入流:
ostream << a << b << c;
显然,输入输出流可以同时充当两者。
在代码示例中,使用 cout
和cin
流对象。 cout
代表控制台输出和 cin 代表 console-input
。这些是用于与默认控制台交互的预定义流。
若要与文件交互,需要使用ifstream
和ofstream
类型。与cin
和cout
类似,ifstream
代表input-file-stream
,ofstream
代表output-file-stream
。
您的代码可能如下所示:
#include <iostream>
#include <fstream>
using namespace std;
int start()
{
cout << "Welcome...";
// do fancy stuff
return 0;
}
int main ()
{
string usreq, usr, yn, usrenter;
cout << "Is this your first time using TEST" << endl;
cin >> yn;
if (yn == "y")
{
ifstream iusrfile;
ofstream ousrfile;
iusrfile.open("usrfile.txt");
iusrfile >> usr;
cout << iusrfile; // I'm not sure what are you trying to do here, perhaps print iusrfile contents?
iusrfile.close();
cout << "Please type your Username. n";
cin >> usrenter;
if (usrenter == usr)
{
start ();
}
}
else
{
cout << "THAT IS NOT A REGISTERED USERNAME.";
}
return 0;
}
如需进一步阅读,您可能需要查看 c++ I/O 参考
要阅读,您应该创建一个 ifsteam 而不是 ofstream 的实例。
ifstream iusrfile;
您应该在阅读模式下打开文件。
iusrfile.open("usrfile.txt", ifstream::in);
这种说法也是不正确的。
cout<<iusrfile;
如果您尝试打印从文件中读取的数据,您应该执行以下操作:
cout<<usr;
您可以在此处阅读有关ifstream及其API的更多信息
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- C++试图读取一个文件并输出到另一个文本文件
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 已修改的LinkedList未在文本文件本身中更新
- C++文本文件的获取线
- 为什么C++的文件 I/O 在读取文本文件时忽略初始空行?我怎样才能让它不这样做?
- 打印逐行存储的文本文件
- C++文本文件输入