调试时打开文件时出错 [英] Error opening file when debugging

查看:171
本文介绍了调试时打开文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  // ConsoleApplication1.cpp:定义控制台应用程序的入口点。 
//

#includestdafx.h
#include< fstream>
#include< iostream>

$ b int _tmain(int argc,_TCHAR * argv [])
{
std :: ifstream inFile(test.txt); (!inFile.is_open()){
std :: cout<<< 不起作用<<的std :: ENDL;
}

inFile.close();
返回0;
}

程序无法打开文件,文件存在于同样的文件夹作为可执行文件(我也尝试把文件的显式路径:C:\ Users \ ..)

变量inFile的值尝试打开文件后是:

  + inFile {_Filebuffer = {_ Set_eback = 0xcccccccc<读取字符串时的错误>> ; _Set_egptr = 0xcccccccc<读取字符串的字符时出错> ...}} std :: basic_ifstream< char,std :: char_traits< char> > 


解决方案

试试这个:

  // ConsoleApplication1.cpp:定义控制台应用程序的入口点。 
//

#includestdafx.h
#include< fstream>
#include< iostream>

$ b int _tmain(int argc,_TCHAR * argv [])
{
std :: ifstream inFile(c:\\test.txt ); $!
$ b if(!inFile.is_open())
{
std :: cout<< 不起作用<<的std :: ENDL;
}

inFile.close();
返回0;



$ b $ p
$ b

如果你把你的文件放到 C :\test.txt 。其余的由你来弄清楚。相对路径的工作,你需要确保文件是你的程序寻找它(或其他方式)。尝试打印出当前的路径,如果你不确定有什么不对。


When i try to run the following program:

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
    std::ifstream inFile("test.txt");

    if(!inFile.is_open()){
        std::cout << "Doesn't work" << std::endl;
    }

    inFile.close();
    return 0;
}

The program isn't able to open the file, the file exists in the same folder as the executable(I also tried to put in the explicit path of the file: C:\Users\..)

The value of the variable inFile after trying to open the file is:

+ inFile    {_Filebuffer={_Set_eback=0xcccccccc <Error reading characters of string.> _Set_egptr=0xcccccccc <Error reading characters of string.> ...} }    std::basic_ifstream<char,std::char_traits<char> >

解决方案

Try this:

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <fstream>
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
    std::ifstream inFile("c:\\test.txt");

    if(!inFile.is_open())
    {
        std::cout << "Doesn't work" << std::endl;
    }

    inFile.close();
    return 0;
}

This should work if you put your file to C:\test.txt. The rest is up to you to figure out. Relative paths work, you need to make sure the file is where your program looks for it (or the other way 'round). Try to print out the current path if you are unsure what's wrong.

这篇关于调试时打开文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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