第一次使用ifstream.无法找到文件;保存在目录中 [英] First time using ifstream. Cant find file; is saved in directory

查看:108
本文介绍了第一次使用ifstream.无法找到文件;保存在目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用< fstream> 的程序.我需要包含的文件称为 employee.dat ,该文件在目录中列出.我在 cout 语句中说:

I'm writing a program that uses <fstream>. The file I need to include is called employee.dat and it's listed in the directory. I put in a cout statement that says:

ifstream inFile ("EMPLOYEE.DAT");

if (! inFile)
{
    cout << "!!Error in opening 'EMPLOYEE.DAT"<< endl;
}

该文件位于目录中,打开/查找文件应该没有任何问题.这是我第一次使用 ifstream 类.有什么花招吗?

The file is in the directory and there shouldn't be any issues opening/finding it. This is my first time using the ifstream class. Are there any tricks to it?

推荐答案

如果从Visual Studio环境中运行,请注意,可执行文件可能与数据文件不在同一目录中.默认情况下,二进制文件通常内置在 Debug Release 文件夹中.您有几种选择:

If you are running from the Visual Studio environment, be aware that your executable probably isn't in the same directory as your data file. Binaries tend to be built into a Debug or Release folder by default. You have several options:

  1. 将数据文件移动到正确的目录中.
  2. 设置工作目录(在项目设置中的调试"下).
  3. 使用完整或相对路径作为文件名.

很有可能您正在寻找正确的位置,但是文件已被锁定.确保未在任何可能阻止其他进程打开它的编辑器或其他程序中打开它.

It's also possible that you are looking in the right place, but the file is locked. Make sure it's not open in any editor or other program that might prevent other processes from opening it.

这篇关于第一次使用ifstream.无法找到文件;保存在目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆