如何在C++中读取和写入文本文件

How to read and write to a text file in C++?

本文关键字:文本 文件 读取 C++      更新时间:2023-10-16

大家好,我刚刚开始学习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;

显然,输入输出流可以同时充当两者。

在代码示例中,使用 coutcin流对象。 cout 代表控制台输出和 cin 代表 console-input 。这些是用于与默认控制台交互的预定义流。

若要与文件交互,需要使用ifstreamofstream类型。与cincout类似,ifstream代表input-file-streamofstream代表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的更多信息