簡體   English   中英

C++如何從任意文本文件中讀取

[英]C++ How to read from an arbitrary text file

我有一個項目,我必須從文本文件中讀取並使用 Book Cipher 對其內容進行編碼。 我假設的第一步是從兩個文本文件中讀取並計算頁/行/列號(頁碼由分隔符 '\\f' 確定,行由 '\\n' 確定)。 但是,我不希望用戶每次都必須將他們的文件重命名為“message.txt”才能讀取文件。 無論如何,C++ 程序是否可以讀取用戶輸入的任何文本文件?

我當前的測試程序代碼:

#include <iostream>     // Basic I/O
#include <string>       // string classes
#include <fstream>      // file stream classes
#include <sstream>      // string stream classes
#include <locale>       // locale class
using namespace std;



// version 0
int main() {
    while (!cin.eof()) {
        char ch;
        cin >> ch;
        cout << ch;
    }
}

它從文件中獲取所有內容並正確輸出,但是這樣我只能接受一個輸入文件。 我希望我能做這樣的事情:

int main( int argc, char* argv[])

所以:

argv[0] = program name
argv[1] = book cipher
argv[2] = message file
argv[3] = output file (encoded message goes here)

這個有C++版本嗎? 我嘗試了 ifstream read(argv[1]),它沒有按我的預期工作,並且拋出了異常。 我覺得 char* argv[] 是在 C 中使用的東西,但不是 C++,因為 C++ 有一個字符串,而字符串不是像 C 中那樣的字符數組。想知道是否有人可以給我寫一個示例來看看語法是如何工作的為此,在 C++ 中。

我可以使用哪個輸入函數來計算轉義字符,例如 '\\n' 和 '\\f'? 我從 Java 知道,我可以使用 charAt() 並在循環或 C 中執行它,它只是一個字符數組。 在 C/C++ 中輸入對我來說真的很困惑,我將繼續查看有關這些不同函數如何工作的教程。 任何想法將不勝感激。

你對main參數argcargv理解基本正確。 您將用作命令行參數的將是文件的名稱。

為了使用它們,您需要打開具有給定文件名的文件,然后才能提取數據。 要打開輸入文件,您可以使用std::ifstream並使用其構造函數,您可以在其中傳遞文件名。 布爾值! std::fstream運算符已被覆蓋,並將返回操作的狀態(結果)。 因此,您可以編寫if (fileStream)來檢查打開操作是否成功。

對於計數某事,有很多可能性。 您可以使用標准算法。

請參閱下面的一些示例代碼:

#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <algorithm>


int main(int argc, char* argv[]) {

    // Check if the number of parameters are correct
    if (4 != argc) {
        // Wrong number of arguments, inform the user
        std::cerr << "\n*** Wrong comand line parameters: please call with:\n"
            << "encode cipherFileName messageFileName outputFileName\n\n";
    }
    else
    {
        // Now, try to open all files. Open cipher file and check if this worked
        if (std::ifstream cipherFileStream(argv[1]); cipherFileStream) {

            // Open message file and check, if that worked
            if (std::ifstream messageFileStream(argv[2]); messageFileStream) {

                // And Open output file and check, if that worked
                if (std::ofstream outputFileStream(argv[3]); outputFileStream) {

                    // Read the complete message file into one string
                    std::string messageData(std::istreambuf_iterator<char>(messageFileStream), {});
                    // Count some special charcters in the message file
                    size_t newLines = std::count(messageData.begin(), messageData.end(), '\n');
                    size_t formFeeds = std::count(messageData.begin(), messageData.end(), '\f');

                    // Show result to user
                    std::cout << "\nNew lines:  " << newLines << "\nForm feeds: " << formFeeds << "\n";

                }
                else {
                    std::cerr << "\n*** Error: Could not open output file\n";
                }
            }
            else {
                std::cerr << "\n*** Error: Could not open message file\n";
            }
        }
        else {
            std::cerr << "\n*** Error: Could not open book cipher file\n";
        }
    }
    return 0;
}

暫無
暫無

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

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