简体   繁体   English

如何在 Visual Studio Community 2015 上打开输入文件、C++?

[英]How to open input file, C++ on visual studio community 2015?

Does anyone know why the file isn't opening?有谁知道为什么文件打不开? I also tried just putting "infile.txt" and placing it in the folder of the program and also the debug folder but the ways I used to check for open error both triggered meaning that it could not open.我还尝试将“infile.txt”放入程序文件夹和调试文件夹中,但我用来检查打开错误的方式都触发了它无法打开的意思。 I know I can hard code the location but I don't want to.我知道我可以对位置进行硬编码,但我不想这样做。

I heard you should do stringobj.c_str() but I don't know if that's accurate?我听说你应该做stringobj.c_str()但我不知道这是否准确?

#include "stdafx.h"

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    ifstream infile;
    ofstream outfile;

    string fileloc = "infile.txt";

    infile.open(fileloc);


    if (!infile)
    {
        cout << "open fail 1" << endl;
    }

    bool fail = infile.fail();
    if (fail)
    {
        cout << "open fail 2";
    }

    return 0;
}

Note that the directory structure (at least for VS2013) is 请注意,目录结构(至少对于VS2013)是

<base>
   - Solution Directory
     - Debug
     - Release
     - Project Directory
       - Debug
       - Release

The program by default runs in the project directory (even though it is built to the solution/debug directory). 默认情况下,程序在项目目录中运行(即使它是针对solution / debug目录构建的)。

If you accepted the default naming convention when starting your project, you should be putting your file in the "Projects\\ConsoleApplication1\\ConsoleApplication1" directory, not "Projects\\ConsoleApplication1" 如果在启动项目时接受了默认命名约定,则应将文件放在“Projects \\ ConsoleApplication1 \\ ConsoleApplication1”目录中,而不是“Projects \\ ConsoleApplication1”中。

Check your working directory in Project Settings -> Debugging. 在项目设置 - >调试中检查您的工作目录。 Make your file available there. 在那里提供您的文件。

First, the documentation for the signature of 首先,签名的文件

std::ifstream::open( const char * filename, ios_base::openmode mode=ios_base::in)

does indicate it requires a const char *, exactly what std::string::c_str() provides. 确实表明它需要一个const char *,正是std :: string :: c_str()所提供的。 However, there is an overload for open which accepts a const str &, which means it works the same way for both on most implementations. 但是,open有一个重载接受const str&,这意味着它在大多数实现中都以相同的方式工作。

Otherwise, what you're grappling with is known as the current working directory (or cwd). 否则,您正在努力解决的问题称为当前工作目录(或cwd)。 Apparently you're not sure where THAT directory is. 显然你不确定那个目录在哪里。 It may be different while you run the debugger on Visual Studio than it is when you run your program from the command line, and it may be different in various IDE's. 在Visual Studio上运行调试器时,可能与从命令行运行程序时的调试器不同,在各种IDE中可能有所不同。

I'm not sure why you want to ensure your program only opens a file by name in the current directory, and not give the full path, but... 我不确定你为什么要确保你的程序只在当前目录中按名称打开文件,而不是给出完整路径,但是......

You may want to inquire what the current working directory is, so you can solve the mystery wherever you try this. 您可能想查询当前工作目录是什么,这样您就可以在任何地方尝试解决这个问题。 In my Visual Studio 2015, the directory ends up being the directory ABOVE debug, but that depends entirely on how your project is configured, and we can't see that out here. 在我的Visual Studio 2015中,目录最终成为ABOVE调试目录,但这完全取决于项目的配置方式,我们在这里看不到。

So, try: 所以,试试:

std::string cwd = getcwd( NULL, 0 );

This requires a header <direct.h> on Windows in Visual Studio, but it will give you the directory you're trying to figure out. 这需要Visual Studio中Windows上的头文件<direct.h> ,但它会为您提供您想要弄清楚的目录。

with

string fileloc = "infile.txt";

if you put infile.txt in the same folder of the cpp file, it should be fine. 如果你把infile.txt放在cpp文件的同一个文件夹中,它应该没问题。

btw I delete your first line 顺便说一句我删除你的第一行

#include "stdafx.h"

I use cygwin console, may have minor diff 我使用cygwin控制台,可能会有轻微的差异

For my issue - i was stuck at loading image by opencv - i was wrong to place directory with jpg in the root of the C++ project对于我的问题 - 我被 opencv 加载图像卡住了 - 我在 C++ 项目的根目录中放置带有 jpg 的目录是错误的

WRONG:错误的:

在此处输入图像描述

CORRECT:正确的: 在此处输入图像描述

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

相关问题 Visual Studio Community 2015 安装 (Visual C++) - Visual Studio Community 2015 Installation (Visual C++) 无法在Visual Studio 2015社区RTM中创建/打开C ++项目 - Unable to create/open C++ project in Visual Studio 2015 Community RTM C ++ boost nuget无法打开Visual Studio 2015文件 - C++ boost nuget cannot open file Visual Studio 2015 如何在“ Microsoft Visual Studio Community 2015”的现有“ C ++项目”中创建新的“头文件(.h)”? - How do I create a new “header file (.h)” in an existing “C++ Project” in “Microsoft Visual Studio Community 2015”? Visual Studio 2015社区C ++构建失败:任务已取消 - Visual Studio 2015 Community C++ Build Failure: A task was cancelled 在C ++ Visual Studio 2015社区中获取本地时间 - Get the Local Time in C++ Visual Studio 2015 Community 部署应用程序,C ++,Visual Studio 2015社区版本 - Deploy app, C++, Visual Studio 2015 community version 使用Visual Studio 2015社区版在C ++中#include excel错误 - #include excel error in C++ with Visual Studio 2015, Community edition 为什么Visual Studio Community 2015中的C ++项目只包含通用应用程序? - How come C++ project in Visual Studio Community 2015 only contain Universal Apps? 无法打开包含文件:'stdio.h' - Visual Studio Community 2017 - C++ 错误 - Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM