简体   繁体   English

在 fstream 上使用 << 编写

[英]Writing using << on an fstream

I'm trying to write a long to a text file using c++ fstream class.我正在尝试使用 c++ fstream 类将long写入文本文件。 The file is already created on disk before the execution.该文件在执行前已在磁盘上创建。 I run the following code and can read the initial value but can't save the new one, overwriting.我运行以下代码并且可以读取初始值但无法保存新值,覆盖。 What am i doing wrong?我究竟做错了什么?

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>


using namespace std;

int main(int argc, char* argv[]) {

    long f;

    fstream myFile("data.txt", fstream::in|fstream::out);
    cout << "f before: " << f << endl;
    myFile >> f;
    cout << "f after: " << f << endl;
    f++;
    cout << "f after increment: " << f << endl;

    myFile << f;
    myFile.close();

    return 0;
}

After that, I read the value in the file and it isn't changed.之后,我读取文件中的值并且它没有改变。 What I did wrong here?我在这里做错了什么?

You need to rewind to the beginning of the file before writing.在写入之前,您需要倒带到文件的开头。 Otherwise the second value is written after the first one.否则,第二个值在第一个值之后写入。

You need to add myFile.seekp(ios::beg);您需要添加myFile.seekp(ios::beg); just before myFile << f;就在myFile << f;之前myFile << f; in order to update the count correctly.以便正确更新计数。

If you want to keep appending to the end, add myFile.clear();如果你想继续追加到最后,添加myFile.clear(); before myFile << f;myFile << f;之前myFile << f; . . This will cause the contents to become : 1 -> 12 -> 1213 -> 12131214 -> 1213121412131215 .这将导致内容变为: 1 -> 12 -> 1213 -> 12131214 -> 1213121412131215 This is required because eof is reached upon reading the input.这是必需的,因为在读取输入时达到 eof。 Note that get and put pointers are the same .请注意 get 和 put 指针是相同的

As you have yourself correctly pointed out, this is required because the file has just the number, not even the newline.正如您自己正确指出的那样,这是必需的,因为文件只有数字,甚至没有换行符。 Thus the read operation hits straight the EOF and causes problems.因此,读取操作直接命中 EOF 并导致问题。 To work around it, we clear eof status and continue.为了解决这个问题,我们清除 eof 状态并继续。

Adding a newline at the end is a solution as you suggested.在最后添加换行符是您建议的解决方案。 In that case在这种情况下

myFile.seekp(ios::beg);
myFile << f<<"\n";

Would be the complete solution.将是完整的解决方案。

I figured what I was doing wrong here:我想我在这里做错了什么:

My file contained ONLY the long that I wanted to read and write afterwards.我的文件只包含我之后想要读写的长度。 When I read the file, I reached EOF and then couldn't rewind or write anything at the end.当我阅读文件时,我到达了 EOF,然后在最后无法倒带或写入任何内容。

That said, my solution was to include a space or a \\n at the end of the file.也就是说,我的解决方案是在文件末尾包含一个空格或一个 \\n。

Does anyone know why the API works this way?有谁知道为什么 API 以这种方式工作? Does not seeem very useful for me...对我来说似乎不是很有用...

It has to do with this line:它与这一行有关:

myFile >> f;我的文件>> f;

Remove it and everything works just fine.删除它,一切正常。 I'm not familiar with fstream but it seems to me this code would try to force a string in a long.我对 fstream 不熟悉,但在我看来,这段代码会尝试强制输入很长的字符串。 I'm also not allowed to cast it to a long, which makes me think this is never meant to be executed like this.我也不允许将其转换为 long,这让我认为这永远不会像这样执行。 I suggest you read up on how to retrieve a value from a file as a long type and then try again.我建议您阅读有关如何从文件中检索一个长类型值的内容,然后再试一次。

edit:编辑:
After reading a bit this site suggested you have to close and reopen the file between reading and writing, I was amazed that actually fixed it.在阅读了一些网站建议您必须在读取和写入之间关闭并重新打开文件后,我很惊讶实际上修复了它。 I can't help but wonder why though, I thought fstream was meant for reading OR writing OR both.... Anyway, here is your working code:我不禁想知道为什么,我认为 fstream 是为了阅读或写作或两者兼而有之......无论如何,这是你的工作代码:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char* argv[]) {

    long f;

    fstream myFile("data.txt", fstream::in|fstream::out);
    cout << "f before: " << f << endl;
    myFile >> f;
    cout << "f after: " << f << endl;
    f++;
    cout << "f after increment: " << f << endl;
    myFile.close();
    myFile.open("data.txt", fstream::in|fstream::out);

    myFile << f;
    myFile.close();

    return 0;
}

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

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