#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;
}
我試圖做一個簡單的C++程序計算的元音字母出現在一個txt文件,使用Visual Studio 2015 該代碼在調試模式,但是當我沒有調試(從Visual Studio中啓動)它不會打開文件。爲什麼會發生,我該如何解決? TXT文件是在程序的同一目錄爲什麼Visual Studio在「無需調試」模式下打開文件?
當我如使用完整路徑我沒有問題Y:\ Documents \ Visual Studio 2015 \ Projects \ stream \ stream \ myfile.txt但是我想知道爲什麼它不適用於相對路徑,例如myfile.txt的
謝謝!
該文本文件在_Debug_文件夾中是不是? –
它應該與項目文件位於同一文件夾中,除非更改了項目的設置。 – drescherjm
在MSVS中,文本文件需要位於源文件的相同位置。你的文本文件在哪裏? – NathanOliver