首页 > 解决方案 > 尝试使用 Ifstream 打开 .txt 文件的 C++ 问题

问题描述

这段代码旨在查看文本文件并识别已写入的帐号,以便稍后在我的程序中,您可以找到正确的帐户,而不会出现两个具有相同帐号(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";
    }
}

在 windows 中,文本文件的位置是 G:\Coding\Test\test\test\accounts.txt

标签: c++fstreamifstream

解决方案


std::ifstream可以使用相对路径以及绝对路径。对于您的问题,如果您确实需要文件的绝对路径,我建议您查看STL的<filesystem>标头。但是,如果它与您的工作目录位于同一目录中,则无需使用绝对路径。这是我完成任务的方法

#include <iostream>
#include <fstream>
#include <string>  // Should include since you're using std::string

// Note that I am NOT "using namespace std;"

int main()
{
    std::ifstream accountsRead("accounts.txt");
    if (accountsRead.is_open())
    {
        int account_id;
        bool account_found = false;

        std::cout << "Enter the ID of the account: ";
        while (!(std::cin >> account_id))
        { // This loop handles if the user inputs non-number
            std::cout << "Please enter a NUMBER below!\n";
            std::cout << "Enter: ";
            std::cin.ignore(10000, '\n');
            std::cin.clear();
        }
        
        int tmpAccNum;
        while (accountsRead >> tmpAccNum)
        { // This loop reads the file, line by line, into tmpAccNum
            if (tmpAccNum == account_id)
            {
                account_found = true;
                break;
            }
        }

        if (account_found)
        {
            std::cout << "Number found!" << std::endl;
        }
        else
        {
            std::cout << "Number not found..." << std::endl;
        }
    }
    else
    { // Error opening file
        std::cout << "File not found or is corrupted" << std::endl;
    }
}

从风格上讲,关于您的代码的一些事情。首先,您永远不应该using namespace std,并且(如果出于某种原因)没有理由混合和匹配std仅在某些std成员上指定命名空间。其次,您不需要else为每个if-statement 指定一个,并且您可能不应该这样做,除非在else达到 case 时实际上有要执行的命令。

编辑

如果你仍然需要一个绝对路径,你可以这样做:

#include <filesystem>

int main()
{
    // Create path object for the file path
    std::filesystem::path file_path("G:\Coding\Test\test\test\accounts.txt");

    // The '.string()' method for a 'std::path' object returns the string
    // version of the path, so you can use it with an 'std::ifstream'
    std::ifstream accounts(file_path.string());  // Opens file via 'ifstream'
    /* And so on... */
}


推荐阅读