C++文件 I/O 问题
我正在尝试打开一个通常包含内容的文件,出于测试的目的,我想在文件可用/存在的情况下初始化程序,这样程序应该创建空文件,但在实现它时遇到问题。这是我的代码,最初
void loadFiles() {
fstream city;
city.open("city.txt", ios::in);
fstream latitude;
latitude.open("lat.txt", ios::in);
fstream longitude;
longitude.open("lon.txt", ios::in);
while(!city.eof()){
city >> cityName;
latitude >> lat;
longitude >> lon;
t.add(cityName, lat, lon);
}
city.close();
latitude.close();
longitude.close();
}
我已经尝试了我能想到的一切,ofstream、ifstream,添加了 ios::out 的所有变体。谁能解释一下我该怎么做才能解决这个问题。谢谢!
I am trying to open a file which normally has content, for the purpose of testing i will like to initialize the program without the files being available/existing so then the program should create empty ones, but am having issues implementing it. This is my code originally
void loadFiles() {
fstream city;
city.open("city.txt", ios::in);
fstream latitude;
latitude.open("lat.txt", ios::in);
fstream longitude;
longitude.open("lon.txt", ios::in);
while(!city.eof()){
city >> cityName;
latitude >> lat;
longitude >> lon;
t.add(cityName, lat, lon);
}
city.close();
latitude.close();
longitude.close();
}
I have tried everything i can think of, ofstream, ifstream, adding ios::out
all all its variations. Could anybody explain me what to do in order to fix the problem. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您已经发布了用于读取文件的代码,而不是创建空文件 - 您无需在这方面扩展您的问题。而且您发布的代码不适合阅读。 eof() 函数在读取之后检测文件末尾,而不是读取之前 - 基本上,您几乎不应该使用它。相反,您应该测试每次读取是否成功:
另外,如果您想创建一个输入流,为什么不使用 ifstream 对象显式地这样做:
不需要弄乱 ios 标志。您应该真正测试 open 是否也有效:
You have posted code for reading files, not creating empty ones - you need t expand your question in this regard. And what you have posted is not good code for reading. The eof() function detects end of file following a read, not before one - basically, you should almost never use it. Instead, you should test the success of each read:
Also, if you want to create an input stream, why not do so explicitly using an ifstream object:
No need to mess around with ios flags. You should really test if the open worked too:
首先,如果您不确定文件 do 是否存在 - 将
city
、lat
和lon
设置为一些合理的默认值。其次,您可以直接使用
fstream
的is_open
成员:如果由于某种原因从文件读取失败,您仍然可以将所有变量设置为默认值。
First, if you are unsure that files do exists - set
city
,lat
andlon
to some sane defaults.Second, you can use directly
is_open
member offstream
:If reading from files fails for some reason you still have all variables set to defaults.
使用 ifstream.fail() 检查文件是否正确打开。如果出现问题(文件不存在或您无权读取或 stg,否则我不知道),它会返回 true。因此,当失败时,您可以使用 ofstream.open() 创建一个。
Use ifstream.fail() to check if file is opened properly. It returns true if something goes wrong (file does not exists or you do not have permission to read or stg else I am unaware of). So when failed you can create one with an ofstream.open().
如果您想(非破坏性地)创建一个没有文件的文件,您可以以追加模式 (
ios::app
) 打开它,如果该文件不存在,该模式将会创建该文件,如果有,则保持不变。If you want to (non-destructively) create a file if there isn't one, you can open it in append mode (
ios::app
), which will create it if it doesn't exist, and leave it untouched if it does.