【发布时间】:2016-05-11 18:36:15
【问题描述】:
#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
char ch;
int a = 0;
int e = 0;
int i = 0;
int o = 0;
int u = 0;
std::ifstream file("myfile.txt", std::ifstream::in);
if (file.is_open())
{
while (file.get(ch))
{
if (ch == 'a')
a++;
if (ch == 'e')
e++;
if (ch == 'i')
i++;
if (ch == 'o')
o++;
if (ch == 'u')
u++;
}
std::cout << "Repetitions of a: " << a << std::endl;
std::cout << "Repetitions of e: " << e << std::endl;
std::cout << "Repetitions of i: " << i << std::endl;
std::cout << "Repetitions of o: " << o << std::endl;
std::cout << "Repetitions of u: " << u << std::endl;
}
else std::cout << "Error in opening file" << std::endl;
return 0;
}
我正在尝试使用 Visual Studio 2015 制作一个简单的 c++ 程序来计算 txt 文件中元音字母的出现次数。 此代码在调试模式下工作,但是当我在没有调试的情况下(从 Visual Studio 内部)启动它时,它不会打开文件。为什么会发生,我该如何解决? txt文件在程序同目录下
当我使用完整路径时,我没有遇到问题,例如Y:\Documents\Visual Studio 2015\Projects\stream\stream\myfile.txt 但我想知道为什么它不适用于相对路径,例如我的文件.txt
谢谢!
【问题讨论】:
-
文本文件不是在Debug文件夹中吗?
-
除非您更改了项目的设置,否则它应该与项目文件位于同一文件夹中。
-
在 MSVS 中,文本文件需要位于源文件所在的相同位置。你的文本文件在哪里?
-
“不调试就运行”到底是什么意思?从 Visual Studio IDE 内部启动它而不进行调试?还是从 IDE 外部启动它?
-
从 VS 内部启动,无需调试
标签: c++ visual-studio visual-studio-2015