#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を使用して、txtファイルの母音の数をカウントする簡単なC++プログラムを作成しようとしています。 このコードはデバッグモードで動作しますが、デバッグなしで起動します)それはファイルを開くことはありません。なぜそれが起こり、どのように私はそれを修正することができますか? txtファイルはプログラムの同じディレクトリにありますなぜVisual Studioは「デバッグなしで起動」モードでファイルを開きませんか?
私は完全なパスを使用すると問題はありません。 Y:\ Documents \ Visual Studio 2015 \ Projects \ stream \ stream \ myfile.txtしかし、私はそれが相対パスで動作しない理由を知りたいと思います。 myfile.txt
ありがとうございます!
テキストファイルは_Debug_フォルダにありますか? –
プロジェクトの設定を変更していない限り、プロジェクトファイルと同じフォルダにある必要があります。 – drescherjm
MSVSでは、テキストファイルはソースファイルと同じ場所にある必要があります。あなたのテキストファイルはどこですか? – NathanOliver