简体   繁体   English

C ++:从txt文件读取和写入的成员函数

[英]C++: Member functions that read and write from txt file

I apologize in advance for how poorly this question is asked, I'm really struggling here. 对于这个问题提出的要求我很抱歉,我真的很努力。 I am writing a class named Point in C++ with private members x and y, and member functions getX, getY, setX, setY, read and write. 我正在用私有成员x和y和成员函数getX,getY,setX,setY,读取和写入在C ++中编写一个名为Point的类。 I have been able to do everything except read and write, as I am awful with input/output files. 我已经能够完成除读取和写入之外的所有操作,因为我对输入/输出文件感到厌烦。 I have the following declaration for read and write: 我有以下读取和写入声明:

void read(istream& ins);
void write(ostream& outs);

The RME is as follows for read: RME如下所示:

* Requires: ins is in good state.
* Modifies: ins, x, y.
* Effects:  Reads point in form (x,y)

and for write: 并写:

* Requires: outs is in good state.
* Modifies: outs.
* Effects:  Writes point in form (x,y).

'read' takes ordered points like (1, 5), (2, 7), etc. from a given file "data1.txt" and extracts the x and y components (at least, I believe this is what it should do). “读取”从给定的文件“ data1.txt”中获取有序的点,例如(1、5),(2、7)等,并提取x和y分量(至少,我相信这是应该做的) 。 I was provided with a test suite for read: 我提供了一个测试套件供阅读:

void test_point() {
Point pt1;

pt1.setX(15);

cout << "pt1 is: " << pt1 << endl;

ifstream input_file;
input_file.open("data1.txt");
pt1.read(input_file);
cout << "pt1 is: " << pt1 << endl;

return;}

I really have no idea how to write the read function. 我真的不知道如何编写读取功能。 I have tried defining characters a, b, c, and integers u, v, and executing: 我试过定义字符a,b,c和整数u,v,然后执行:

ins >> a >> u >> b >> v >> c;

but that didn't work. 但这没用。 Could someone please help me see how to implement this? 有人可以帮我看看如何实现吗?

Quite a few things missing from your question, that you will need such that the use of this class to be viable. 您的问题中遗漏了很多东西,您需要确保使用此类可行。 For one, reading a file of ordered points should not be implemented as a member function. 例如,读取有序点文件不应作为成员函数实现。 If anything, you could use a loop as such: 如果有的话,可以这样使用循环:

    while(input_file) {
        // set a point to have members x, y that were read from file
        // store this point in a vector of points
    }

Otherwise there is no reasonable way to store a bunch of points that you have read from a file. 否则,没有合理的方法来存储您从文件中读取的一堆点。

Your idea for this solution within the read function should definitely work (assuming chars a, b, c, and ints u, v): 您在read函数中对该解决方案的想法肯定可以工作(假设字符a,b,c和int u,v):

    input_stream >> a >> u >> b >> v >> c;

In fact, with the format that you have given us (taking an ifstream as an argument), there really is no need for be much else in the read function, as it mutates the object and doesn't need to return anything. 实际上,使用您给我们的格式(以ifstream作为参数),在read函数中确实不需要很多其他操作,因为它可以改变对象并且不需要返回任何内容。 Your implementation is the simplest way to parse such "records" in a structured file. 您的实现是解析结构化文件中此类“记录”的最简单方法。

After you read all these, set the x coordinate of the caller object to u, and the y to v. This should be a void function, does not need to return anything but should alter the Point object that it is being called for. 阅读完所有这些内容后,将调用者对象的x坐标设置为u,将y设置为v。这应该是一个void函数,不需要返回任何内容,但应该更改要调用的Point对象。 This member function should be called on a temporary point object in the loop that I mentioned, then add that object to a vector of points as such: 应该在我提到的循环中的一个临时点对象上调用此成员函数,然后将该对象添加到点向量中,如下所示:

    vector<Point> points;
    while(...) {
        // declare Point
        // initialize with values from read
        points.push_back(//the point you just created);
    }

If in fact you need to be able to read multiple points. 实际上,如果您需要能够读取多个点。

In summary, your read function needs: 总之,您的读取功能需要:

  1. To check that the ifstream is actually good first of all, which I didn't mention yet (but this is one of your requirements): 首先要检查ifstream实际上是否很好,我还没有提到(但这是您的要求之一):

     if (!input_file) { //however you want to handle this error } 
  2. The temporary char and int variables (serving as buffers) to read into (by the way, you can even read straight into x and y instead of reading into u and v then copying, just saying). 要读入的临时char和int变量(用作缓冲区)(顺便说一句,您甚至可以直接读入x和y,而不是读入u和v然后进行复制,只是说)。

  3. If you choose to not read straight into x and y, you must assign the u and v values to x and y. 如果选择不直接读入x和y,则必须将u和v值分配给x和y。 Now your object is complete. 现在您的对象已完成。

As for the write function, instead of std::cout you will use the name of the ofstream that was passed as an argument to the write function, and write the records with the format that you have shown. 至于写函数,将使用作为参数传递给写函数的ofstream的名称,而不是std :: cout,并以显示的格式写记录。 This is (in essence) no different from printing to a console, except the output is on a text file. 从本质上讲,这与打印到控制台没有什么不同,除了输出在文本文件上。

Note: Make sure you understand the difference between iostream objects (istream, ostream) and fstream (ifstream, ofstream, fstream) objects, which are preferable in this case. 注意:请确保您了解iostream对象(istream,ostream)和fstream(ifstream,ofstream,fstream)对象之间的区别,在这种情况下,这是更可取的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM