C++ C++;尝试使用Ifstream打开.txt文件时出现问题

C++ C++;尝试使用Ifstream打开.txt文件时出现问题,c++,fstream,ifstream,C++,Fstream,Ifstream,这一小段代码旨在查看文本文件并识别已编写的帐号,以便稍后在我的程序中,您可以找到正确的帐号,而不会出现两个帐号(id)相同的错误。但无论我做什么,无论是在ifstream对象的位置使用双反斜杠、正斜杠还是双正斜杠;我总是得到“找不到文件”作为输出 #include <iostream> #include <fstream> using namespace std; int main() { ifstream accountsread("‪G:/Coding/Te

这一小段代码旨在查看文本文件并识别已编写的帐号,以便稍后在我的程序中,您可以找到正确的帐号,而不会出现两个帐号(id)相同的错误。但无论我做什么,无论是在ifstream对象的位置使用双反斜杠、正斜杠还是双正斜杠;我总是得到“找不到文件”作为输出

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ifstream accountsread("‪G:/Coding/Test/test/test/accounts.txt");
    if (accountsread.is_open()) {
        int tempAccno;
        std::string tempname;
        char tempchar;
        int accountsfound = 0;
        int input;

std::cout << "Enter the ID of the account \n";
        cin >> x;

        while (!accountsread.eof()) {
            accountsread >> tempAccno;
            if (tempAccno == input) {
                accountsfound++;
            }
            else {}


        }
        if (accountsfound > 0) {
            cout << "number found";
        }
        else {
            cout << "number not found";
        }
    }
    else {
        cout << "cannot find file";
    }
}
#包括
#包括
使用名称空间std;
int main(){
ifstream accountsread(“‪G:/Coding/Test/Test/Test/accounts.txt”);
如果(accountsread.is_open()){
int tempAccno;
std::字符串tempname;
char tempchar;
int accountsfound=0;
int输入;
std::cout>x;
而(!accountsread.eof()){
账户READ>>临时账户编号;
if(tempAccno==输入){
accountsfound++;
}
else{}
}
如果(accountsfound>0){

cout
std::ifstream
可以使用相对路径和绝对路径。对于您的问题,如果您确实需要文件的绝对路径,我建议您查看STL中的。但是,如果它与您的工作目录位于同一目录中,则不需要使用绝对路径。下面是我如何完成任务的

#包括
#包括
#include//应该包含,因为您使用的是std::string
//请注意,我不是“使用名称空间std;”
int main()
{
std::ifstream accountsRead(“accounts.txt”);
如果(accountsRead.is_open())
{
国际帐户id;
bool账户_found=假;
std::cout>帐户(id))
{//如果用户输入非数字,此循环将处理
std::cout tmpAccNum)
{//此循环逐行将文件读入tmpAccNum
如果(tmpAccNum==帐户id)
{
account_found=true;
打破
}
}
如果(找到的帐户)
{
std::cout在您的场景(windows)中,转义反斜杠(\)是路径分隔符。可能需要在cmd提示符下验证该文件。文件资源管理器可能很棘手,例如,该文件可能名为accounts.txt.txt,但如果文件扩展名被隐藏,您将看不到它。建议阅读: