簡體   English   中英

嘗試使用 Ifstream 打開 .txt 文件的 C++ 問題

[英]C++ problem trying to open a .txt file using Ifstream

這段代碼旨在查看文本文件並識別已經寫入的帳號,以便稍后在我的程序中,您可以找到正確的帳戶,而不會出現兩個具有相同帳號(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

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命名空間。 其次,您不需要為每個if語句指定一個else ,並且您可能不應該這樣做,除非在達到else情況時實際上有要執行的命令。

編輯

如果您仍然需要絕對路徑,您可以這樣做:

#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... */
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM