简体   繁体   English

使用ofstream而不是ifstream和fstream /创建的文件,似乎无法访问gcount()

[英]File created when using ofstream, but not ifstream and fstream/cannot seem to access gcount()

all. 所有。 A new project of mine involves reading names from a file, and I realized, especially for somebody who likes to (attempt to, more like) makes games, reading/writing to store information is very useful. 我的一个新项目涉及从文件中读取名称,我意识到,特别是对于喜欢(尝试,更喜欢)制作游戏的人来说,读取/写入存储信息非常有用。 I looked into it and discovered the std library pulls through again. 我调查了一下,发现标准库再次通过。 I subsequently realized that, at least for me, the libraries from ios, ios_base, iostream, fstream, etc etc, seem to be kind of complex. 随后,我意识到,至少对我来说,ios,ios_base,iostream,fstream等的库似乎有点复杂。

I looked around, but am not quite sure why this specific approach does not work. 我环顾四周,但不确定为什么这种特定方法行不通。 It puzzles me because examples found online that seem to follow the library as they should - I will refer to one on cplusplus.com. 这让我感到困惑,因为在网上找到的示例似乎都应遵循该库-我将在cplusplus.com上引用其中的一个。 Of interesting note is that one worked as expected, while another didn't. 有趣的是,一个按预期工作,而另一个则没有。 Far as I know, that is. 据我所知,就是这样。

My problem is, I can create a file using ofstream, but not ifstream nor fstream. 我的问题是,我可以使用ofstream创建文件,但不能使用ifstream或fstream创建文件。 The way I understand it, there constructors, which I was using (which I beleive is identical paramaters as you would pass to open - that is, the filename and flags) are identical except in that they have different default perameters - ostream has ios::out, istream has ifstream has ios::in, and fstream has ios::in | 据我了解,这里使用的构造函数(我相信与您要打开的传递函数是相同的参数-即文件名和标志)是相同的,除了它们具有不同的默认性能参数-ostream具有ios: :out,istream具有ifstream具有ios :: in,而fstream具有ios :: in | ios::out - both flags, which is logical since it is the combination of both classes. ios :: out-两个标志,这是合乎逻辑的,因为它是两个类的组合。 Note this is not a problem of WHERE the file is created, rather, but the fact of its creation. 请注意,这不是在哪里创建文件的问题,而是在创建文件的事实。 I know this by two reasons. 我知道这有两个原因。 First, when using ofstream the file appears in expected directory, but not at all with ifstream nor fstream. 首先,当使用ofstream时,该文件显示在预期目录中,但对于ifstream或fstream而言则根本不存在。 Second, using the is_open function, (specifically, testing the expressions "while (is_open)" , the console never closed (I test most new concepts in the console because, well, it is simple), but with the others it did. If it had, then it never would have ended, since it can never go out of scope and thus the destructor spells the end for the open file. 其次,使用is_open函数(具体来说,测试表达式“ while(is_open)”时),控制台永远不会关闭(我在控制台中测试了大多数新概念,因为很简单),但是其他方面都做到了。它已经拥有了,那么它就永远不会结束,因为它永远不会超出范围,因此析构函数会为打开文件拼写结尾。

My second problem was with using gcount - mostly how to access it. 我的第二个问题是使用gcount-主要是如何访问它。 No matter how I tried (std::gcount, file::gcount, fstream::gcount...) it never recognized what it was. 不管我如何尝试(std :: gcount,file :: gcount,fstream :: gcount ...),它都永远不会意识到它是什么。 I was a bit befuddled. 我有点困惑。

Now comes a bit off off-topic ness to the specific issue, but more into the general reason of why I encountered this problem to begin with, which is sort of a discussion but I think it could be beneficial thing to learn, unless somehow I am the only one with this problem. 现在,针对特定问题有些偏离主题,但更多地归结为我为什么会首先遇到此问题的一般原因,虽然这是一种讨论,但我认为这可能是有益的,除非我以某种方式学习是唯一有这个问题的人。

Firstly, the tuturial I had read over (learncpp.com) goes over the seekp/seekg functions, and said that it moves a relative amount in BYTES. 首先,我阅读过的教程(learncpp.com)遍历了seekp / seekg函数,并说它在BYTES中移动了相对数量。 It occured to me this is generally the same as a char, but is it possible that this is different on a different system (ie, if a char was 2 bytes, you could figure this out, and apply this to the file searching), or will it always go by character, essentially, anyway? 在我看来,这通常与char相同,但是在不同的系统上这可能是不同的(即,如果char为2字节,则可以弄清楚,并将其应用于文件搜索),还是从本质上说,它总是会按字符排列?

And my main wonder... how to really use these tools to do things. 我的主要奇迹是……如何真正使用这些工具来做事。 Let's say I want to put a name on each line. 假设我要在每行上输入一个名称。 I would be a bit unsure how to do that, - newline character perhaps? 我不太确定该怎么做,也许是换行符? Or the other end, reading a name per line - how do you find the starting position of the next line? 还是另一端,每行读取一个名称-您如何找到下一行的起始位置? (my issue with gcount arrises because I figured you could use getline(), then gcount(), and voila, move that many characters... not sure if that works though) (我的gcount问题到来了,因为我认为您可以使用getline(),然后使用gcount()和voila,移动那么多字符...不确定是否可以使用)

Thanks. 谢谢。

To answer just one of your questions: you can't create a file with ifstream because, well, it doesn't create files; 仅回答您的一个问题:您不能使用ifstream创建文件,因为它不能创建文件; it's simply not intended to. 这根本不是故意的。 This purpose of this class is to read data from existing files, and to open a file and read from it, it must exist first. 此类的目的是从现有文件中读取数据,并打开文件并从中读取文件,该文件必须首先存在。 Simple as that. 就那么简单。

As far as fstream goes, you could make a case that it would make sense for it to create a file if none exists, but in fact, that's not what it does; fstream而言,如果没有文件存在,您可能会认为创建一个文件是有意义的,但是实际上,这不是它的作用。 the argument to fstream 's constructor must be an existing file. fstream的构造函数的参数必须是现有文件。 If the named file doesn't exist, a failure bit is set in the object, and no file is created. 如果命名文件不存在,则在对象中设置一个失败位,并且不创建文件。

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

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