Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.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++;? 我有一个文件夹,包含了200个单词文档,我想从库FSFET中使用IFStfink读取它们到C++。我有两个问题:_C++_File_Input_File Io_Inputstream - Fatal编程技术网

有没有办法在C++;? 我有一个文件夹,包含了200个单词文档,我想从库FSFET中使用IFStfink读取它们到C++。我有两个问题:

有没有办法在C++;? 我有一个文件夹,包含了200个单词文档,我想从库FSFET中使用IFStfink读取它们到C++。我有两个问题:,c++,file,input,file-io,inputstream,C++,File,Input,File Io,Inputstream,1) fin能够读入.doc文件,但由于.doc文件不是纯文本,所以会在屏幕上显示无意义的内容 2) 我知道没有办法让程序自动读取具有不相关文件名的多个文件 由于这两个问题,我将手动检查每个.doc文件并将其更改为.txt文件。另外,我把它们称为1.TXT、2.TXT、3.TXT等,这样我就可以使用C++中的for循环来读取它们(我将在每个迭代中将循环控制变量i转换成字符串x,并读取“x.txt”)。 虽然这会起作用,但我只浏览了83个文件,大约花了一个小时。有没有办法让C++自动读取这些文件?

1) fin能够读入.doc文件,但由于.doc文件不是纯文本,所以会在屏幕上显示无意义的内容

2) 我知道没有办法让程序自动读取具有不相关文件名的多个文件

由于这两个问题,我将手动检查每个.doc文件并将其更改为.txt文件。另外,我把它们称为1.TXT、2.TXT、3.TXT等,这样我就可以使用C++中的for循环来读取它们(我将在每个迭代中将循环控制变量i转换成字符串x,并读取“x.txt”)。
虽然这会起作用,但我只浏览了83个文件,大约花了一个小时。有没有办法让C++自动读取这些文件?C++必须首先把每一个都变成一个.txt文件,这样我就可以打印有意义的文本到屏幕上。

假设你在谈论微软Word和“文件夹”,我猜你在运行Windows。 Windows API提供了一对函数,允许程序自动查找现有文件的名称

在Linux和Unix平台上,有两个函数名为
opendir
readdir
,它们的作用相同


如果您想编写跨平台代码,有一些库在操作系统功能之上提供了一个抽象层,例如
boost::filesystem

boost库非常适合这些类型的文件/文件系统操作。请检查下面的代码。这基本上会转到保存所有文档文件的文件夹(ws),并遍历其中的所有文件。代码假定文件夹“ws”只有文件,没有文件夹。一旦你有了文件名,你就可以对它进行各种操作

我不明白你为什么要把扩展名改成txt,但包括了几行代码。更改扩展名不会影响其内容

#include <sstream>
#include <iostream>
#include <boost/filesystem.hpp>

namespace fs = boost::filesystem;

int main(){

    // ref : https://theboostcpplibraries.com/boost.filesystem-paths

    // ws : workspace where you keep all the files
    fs::path ws = fs::path(getenv("HOME")) / "ws";

    // ref : https://theboostcpplibraries.com/boost.filesystem-iterators
    fs::directory_iterator it{ws};

    while (it != fs::directory_iterator{}){
        std::cout << "Processing file < " << *it << " >" << std::endl;
        // ... do other stuff

        // Parse the current filename into its parts, then change the extension to txt
        // ref : https://theboostcpplibraries.com/boost.filesystem-paths
        std::stringstream ss;
        ss << (ws / fs::path(*it).stem()).native() << ".txt";

        fs::path new_path(ss.str());

        std::cout << "Copying into < " << new_path << " >" << std::endl;

        // ref : http://www.boost.org/doc/libs/1_53_0/libs/filesystem/doc/reference.html
        fs::copy_file(*it++, new_path, fs::copy_option::overwrite_if_exists);
    }

    return 0;
}
g++ -std=c++14 -o main main.cc -lboost_filesystem -lboost_system