Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.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++ 新到<;dirent.h>;,试图访问目录中的数据_C++_Directory_Opendir_Istringstream_Dirent.h - Fatal编程技术网

C++ 新到<;dirent.h>;,试图访问目录中的数据

C++ 新到<;dirent.h>;,试图访问目录中的数据,c++,directory,opendir,istringstream,dirent.h,C++,Directory,Opendir,Istringstream,Dirent.h,我以前从未使用过dirent.h。我使用istringstream读取文本文件(单数),但需要尝试修改程序以读取目录中的多个文本文件。这就是我尝试实现dirent的地方,但它不起作用 也许我不能用stringstream?请告知 为了便于阅读,我去掉了我正在用单词做的毛茸茸的东西。在我添加dirent.h文件之前,这对一个文件来说非常有效 #include <cstdlib> #include <iostream> #include <string> #inc

我以前从未使用过
dirent.h
。我使用istringstream读取文本文件(单数),但需要尝试修改程序以读取目录中的多个文本文件。这就是我尝试实现dirent的地方,但它不起作用

也许我不能用stringstream?请告知

为了便于阅读,我去掉了我正在用单词做的毛茸茸的东西。在我添加dirent.h文件之前,这对一个文件来说非常有效

#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>  // for istringstream
#include <fstream>
#include <stdio.h>
#include <dirent.h>

void main(){

    string fileName;
    istringstream strLine;
    const string Punctuation = "-,.;:?\"'!@#$%^&*[]{}|";
    const char *commonWords[] = {"AND","IS","OR","ARE","THE","A","AN",""};
    string line, word;
    int currentLine = 0;
    int hashValue = 0;

    //// these variables were added to new code //////

    struct dirent *pent = NULL;
    DIR *pdir = NULL; // pointer to the directory
    pdir = opendir("documents");

    //////////////////////////////////////////////////


    while(pent = readdir(pdir)){

        // read in values line by line, then word by word
        while(getline(cin,line)){
            ++currentLine;

            strLine.clear();
            strLine.str(line);

            while(strLine >> word){

                        // insert the words into a table

            }

        } // end getline

        //print the words in the table

    closedir(pdir);

    }
#包括
#包括
#包括
#istringstream的include//
#包括
#包括
#包括
void main(){
字符串文件名;
伊斯特林溪斯特林;
常量字符串标点符号=“-,.;:?\”!@$%^&*[]{}124;”;
const char*commonWords[]={“AND”、“IS”、“OR”、“ARE”、“THE”、“A”、“AN”、“A”);
字符串行、字;
int currentLine=0;
int hashValue=0;
////这些变量被添加到新代码中//////
struct dirent*pent=NULL;
DIR*pdir=NULL;//指向目录的指针
pdir=opendir(“文件”);
//////////////////////////////////////////////////
while(pent=readdir(pdir)){
//逐行读取值,然后逐字读取
while(getline(cin,line)){
++电流线;
strLine.clear();
strLine.str(行);
while(strLine>>word){
//将单词插入表中
}
}//结束getline
//打印表格中的单词
closedir(pdir);
}

您应该使用
int main()
而不是
void main()

检查对
opendir()
的调用时应该会出错

您需要打开一个文件,而不是使用
cin
读取文件的内容。当然,您还需要确保文件已正确关闭(可能是什么也不做,让析构函数执行其操作)

请注意,文件名将是目录名(
“documents”
)和由
readdir()返回的文件名的组合

还请注意,您可能应该检查目录(或者至少检查当前目录和父目录的
”)

<安德鲁·克尼格和Barbara Moo的书中有一章讨论了如何在C++中封装<代码> OpenDIR()/C++ >函数族,使它们在C++程序中表现得更好。
希瑟问:

我应该在
getline()
中放入什么,而不是
cin

当前的代码读取标准输入,即当前的
cin
。这意味着,如果您使用
/a.out
启动程序,它将读取您的
program.cpp
文件,而不管它在目录中找到了什么。因此,您需要根据找到的文件创建一个新的输入文件流ith
readdir()


由于第一次读取操作(通过
getline()
)将失败(但您可能应该根据目录项的名称安排跳过
目录项),因此您可能只需打开目录即可如果 FIN <代码>是循环中的局部变量,则当外环循环时, FIN < /代码>将被销毁,这应该关闭文件。

注意,Valuy主()/<代码>不是C++中主程序的有效原型之一(C中为非标准).hi,非常抱歉,我不知道upvoting!已返回并修复了此问题。感谢您的提醒:)好的,因此我已将void更改为int(不确定为什么我首先执行此操作tbh。),并进行了错误检查。目录“文档”“是正确的,我以前只是打印出文档的名称。所以我认为这是正确的。所以…我很困惑,我应该在getline()中放什么而不是cin?这些让我非常困惑。”。
while (pent = readdir(pdir))
{
    ...create name from "documents" and pent->d_name
    ...check that name is not a directory
    ...open the file for reading (only) and check that it succeeded
    ...use a variable such as fin for the file stream
    // read in values line by line, then word by word
    while (getline(fin, line))
    {
         ...processing of lines as before...
    }
}