C++ 为什么我的代码不会打开任何文件?
出于某种原因,我尝试输入的每个文件都表示它不存在。我已经做了几个文件,试图和饲料它,但没有一个工作。那么我是做错了什么还是做了别的什么C++ 为什么我的代码不会打开任何文件?,c++,visual-c++,C++,Visual C++,出于某种原因,我尝试输入的每个文件都表示它不存在。我已经做了几个文件,试图和饲料它,但没有一个工作。那么我是做错了什么还是做了别的什么 //Program will read in a list of movies with ratings from a file supplied //by the user and then ask the user what rating they would like to see. //Then it will give the user the o
//Program will read in a list of movies with ratings from a file supplied
//by the user and then ask the user what rating they would like to see.
//Then it will give the user the option of seeing all the movies with
//that rating or a random movie with that rating.
#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<fstream>
#include <string>
#include <vector>
#include<time.h>
#include<stdlib.h>
//Namespace declaration
using namespace std;
int returnRandom(int range);
//starting of main function
int main()
{
//Varibale data type declarations
int i, count = 0, num;
string word1, word2;
//array declarations
string m_name[100];
string m_rating[100];
string name;
string rate;
//filestream declaration
ifstream infile;
string x;
cout << "Please type the name of the file you would like to use. " << endl;
cin >> x;
//open file for reading
infile.open(x.c_str());
//error checking if file does not exist
if (!infile)
{
cout << "can not open the file." << endl;
}
//start reading the data from the file
while (infile >> word1)
{
m_name[count] = word1;
infile >> word1;
m_rating[count] = word1;
count++;
}
infile.close();
//asking for user input
int choice;
cout << "Please enter the rating of the movie to watch: ";
getline(cin.ignore(), rate);
cout << "Type 1 to print all the movies of the input rating." << endl;
cout << "Type 2 to print a random movie of input rating: " << endl;
cout << "Enter your choice: ";
cin >> choice;
cout << endl;
//search movies which has the rating as input given by the user
if (choice == 1)
{
for (i = 0; i<count; i++)
{
if (m_rating[i] == rate)
{
//Display movies
cout << "Movie " << m_name[i] << " has " << rate << " ratings." << endl;
}
}
}
else
{
if (choice == 2)
{
int index = returnRandom(count);
while (m_rating[index] != rate)
index = returnRandom(count);
cout << "Movie " << m_name[index] << " has " << rate << " ratings." << endl;
}
else
cout << "Invalid Input!";
}
return 0;
}//end of main
//function to return the random index of input rating
int returnRandom(int range)
{
srand(time(NULL));
return 0 + (rand() % (0 - range + 1));
}
//程序将从提供的文件中读取具有分级的电影列表
//然后询问用户希望看到的评级。
//然后,它将为用户提供观看所有电影的选项
//或者是一部随机的电影。
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//名称空间声明
使用名称空间std;
int returnRandom(int范围);
//主功能的启动
int main()
{
//变量数据类型声明
int i,计数=0,num;
字符串word1,word2;
//数组声明
字符串m_name[100];
串m_额定值[100];
字符串名;
串速率;
//文件流声明
河流充填;
字符串x;
cout x;
//打开文件进行读取
填充开放式(x.c_str());
//检查文件是否不存在时出错
如果(!infle)
{
cout word1)
{
m_name[count]=word1;
填充>>word1;
m_评级[计数]=字1;
计数++;
}
infle.close();
//请求用户输入
智力选择;
你能发布一个目录设置的屏幕截图吗?ifstream将相对于可执行文件的路径…文件位于哪里?如果你只是给它提供文件名,那么文件应该位于程序目录中(项目的.exe存在的地方)。否则,你需要键入文件的完整路径'C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\呃,我要去学校学机电一体化,唯一需要的是梯形逻辑,它和C++有很大的不同。