Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;如何读取任意文本文件_C++_File_Input_Text_Stream - Fatal编程技术网

C++ C++;如何读取任意文本文件

C++ C++;如何读取任意文本文件,c++,file,input,text,stream,C++,File,Input,Text,Stream,我有一个项目,我必须读取一个文本文件,并使用图书密码对其内容进行编码。我假设的第一步是读取两个文本文件并计算页码/行号/列号(页码由分隔符“\f”确定,行由“\n”确定)。但是,我不希望用户每次都必须将文件重命名为“message.txt”才能读取该文件。不管怎样,C++程序都能读取用户所输入的任何文本文件吗? 我当前的测试程序代码: #include <iostream> // Basic I/O #include <string> // strin

我有一个项目,我必须读取一个文本文件,并使用图书密码对其内容进行编码。我假设的第一步是读取两个文本文件并计算页码/行号/列号(页码由分隔符“\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;
    }
}
因此:

有C++版本吗?我尝试了ifstream-read(argv[1]),它没有像我预期的那样工作,并且引发了一个异常。我觉得像char *avg[]是用在C中的东西,而不是C++,因为C++有一个字符串,字符串不是C.中的字符数组。 想知道有没有人可以给我写一个样本,看看C++中的语法如何工作。

我可以使用什么输入函数来计算转义字符,如“\n”和“\f”?
我从Java知道,我可以使用charAt()并在循环或C中执行,它只是一个char数组。在C/C++中,输入对我来说真的很混乱,我将继续看关于这些不同函数如何工作的教程。您对
主要
参数
argc
argv
的理解基本正确。您将使用的comand行参数是文件名

为了使用它们,您需要打开具有给定文件名的文件,然后才能提取数据。要打开输入文件,可以使用
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;
}

#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[]){
//检查参数数量是否正确
如果(4!=argc){
//参数数量错误,请通知用户

正确的解决方案是使用
std::ifstream
。不幸的是,如果您的全部描述是
std::ifstream
“没有如我预期的那样工作,并引发了异常”。至于“任何想法”你用C++ C++学习C++的书,你不理解这本书中所有的例子,使用<代码> STD::IFStry?C++与C是向后兼容的,因此支持<代码> char **ARGV(AFAK,STD::String对象不是,这是标准库的一部分,不是核心语言)。您能将异常复制到您的帖子中,以便我们更好地了解出了什么问题吗?
std::ifstream
读取该文件是正确的,但在我看来,您甚至无法达到这一点-您实际上陷入了如何将文件名从C样式的argv指针中取出的困境。听起来正确吗?无关:while(!cin.eof())
是一个bug。有关详细信息,请参阅。它仍然是
char*argv[]
。如果您愿意,可以将其全部转换为
std::vector
std::string
,但通常没有什么需要。不过,首先要确保您有足够的参数使用
argc
。最好的做法是将问题中不起作用的代码与用于运行pro的命令行一起添加到问题中很有可能有人能在很短的时间内告诉你出了什么问题。谢谢。我用了一个更简单的版本,但它很有效。同样的想法:)
argv[0] = program name
argv[1] = book cipher
argv[2] = message file
argv[3] = output file (encoded message goes here)
#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;
}