C++文件 I/O 问题

发布于 2024-08-29 10:47:02 字数 569 浏览 8 评论 0原文

我正在尝试打开一个通常包含内容的文件,出于测试的目的,我想在文件可用/存在的情况下初始化程序,这样程序应该创建空文件,但在实现它时遇到问题。这是我的代码,最初

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离不开的别离 2024-09-05 10:47:02

您已经发布了用于读取文件的代码,而不是创建空文件 - 您无需在这方面扩展您的问题。而且您发布的代码不适合阅读。 eof() 函数在读取之后检测文件末尾,而不是读取之前 - 基本上,您几乎不应该使用它。相反,您应该测试每次读取是否成功:

while( (city >> cityName) && (latitude >> lat) && (longitude >> lon) {
    t.add(cityName, lat, lon);
}

另外,如果您想创建一个输入流,为什么不使用 ifstream 对象显式地这样做:

ifstream city( "city.txt" );

不需要弄乱 ios 标志。您应该真正测试 open 是否也有效:

if ( ! city.is_open() ) {
   throw "open failed" ;   // or whatever
}

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:

while( (city >> cityName) && (latitude >> lat) && (longitude >> lon) {
    t.add(cityName, lat, lon);
}

Also, if you want to create an input stream, why not do so explicitly using an ifstream object:

ifstream city( "city.txt" );

No need to mess around with ios flags. You should really test if the open worked too:

if ( ! city.is_open() ) {
   throw "open failed" ;   // or whatever
}
一紙繁鸢 2024-09-05 10:47:02

首先,如果您不确定文件 do 是否存在 - 将 citylatlon 设置为一些合理的默认值。

其次,您可以直接使用 fstreamis_open 成员:

fstream f;
f.open("f.txt", ios::in);
if (f.is_open()) {
    // attempt reading here
} else {
    f.open("f.txt", ios::out | ios::app); // create empty file
}

如果由于某种原因从文件读取失败,您仍然可以将所有变量设置为默认值。

First, if you are unsure that files do exists - set city, lat and lon to some sane defaults.

Second, you can use directly is_open member of fstream:

fstream f;
f.open("f.txt", ios::in);
if (f.is_open()) {
    // attempt reading here
} else {
    f.open("f.txt", ios::out | ios::app); // create empty file
}

If reading from files fails for some reason you still have all variables set to defaults.

笔芯 2024-09-05 10:47:02

使用 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().

孤独难免 2024-09-05 10:47:02

如果您想(非破坏性地)创建一个没有文件的文件,您可以以追加模式 (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文