简体   繁体   English

新的 <dirent.h> ,尝试访问目录中的数据

[英]New to <dirent.h>, trying to access data in a directory

I've never used dirent.h before. 我以前从未使用过dirent.h I was using istringstream to read through text files (singular), but have needed to try to revise the program to read in multiple text files in a directory. 我使用istringstream读取文本文件(单数),但是需要尝试修改程序以读取目录中的多个文本文件。 This is where I tried implementing dirent, but it's not working. 这是我尝试实施Dirent的地方,但是没有用。

Maybe I can't use it with the stringstream? 也许我不能在stringstream中使用它? Please advise. 请指教。

I've taken out the fluffy stuff that I'm doing with the words for readability. 为了便于阅读,我已经删除了正在使用的蓬松材料。 This was working perfectly for one file, until I added the dirent.h stuff. 这对于一个文件来说完美的,直到我添加了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);

    }

You should be using int main() and not void main() . 您应该使用int main()而不是void main()

You should be error checking the call to opendir() . 您应该在检查对opendir()的调用时出错。

You will need to open a file instead of using cin to read the contents of the file. 您将需要打开一个文件,而不是使用cin来读取文件的内容。 And, of course, you will need to ensure that it is closed appropriately (which might be by doing nothing and letting a destructor do its stuff). 并且,当然,您将需要确保将其正确关闭(这可能是通过不执行任何操作并让析构函数执行其操作)。

Note that the file name will be a combination of the directory name ( "documents" ) and the file name returned by readdir() . 请注意,文件名将是目录名( "documents" )和readdir()返回的文件名的组合。

Note too that you should probably check for directories (or, at least, for "." and ".." , the current and parent directories). 还要注意,您可能应该检查目录(或者至少检查当前目录和父目录"."".." )。

The book "Ruminations on C++" by Andrew Koenig and Barbara Moo has a chapter that discusses how to wrap the opendir() family of functions in C++ to make them behave better for a C++ program. 安德鲁·科尼希(Andrew Koenig)和芭芭拉·穆(Barbara Moo)撰写的《 C ++上的反刍 》一书中的一章讨论了如何在C ++中包装opendir()函数家族,以使它们对C ++程序表现得更好。


Heather asks: 希瑟问:

What do I put in getline() instead of cin ? 我要在getline()而不是cin放入什么?

The code at the moment reads from standard input, aka cin at the moment. 此刻的代码从标准输入(即cin读取。 That means that if you launch your program with ./a.out < program.cpp , it will read your program.cpp file, regardless of what it finds in the directory. 这意味着,如果使用./a.out < program.cpp启动./a.out < program.cpp ,则无论在目录中找到什么内容,它都会读取program.cpp文件。 So, you need to create a new input file stream based on the file you've found with readdir() : 因此,您需要根据通过readdir()找到的文件创建一个新的输入文件流:

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...
    }
}

You probably can get away with just opening the directories since the first read operation (via getline() ) will fail (but you should probably arrange to skip the . and .. directory entries based on their name). 由于第一次读取操作(通过getline() )将失败,因此您可能只需要打开目录就可以了(但是您可能应该根据它们的名称来跳过...目录条目)。 If fin is a local variable in the loop, then when the outer loop cycles around, fin will be destroyed, which should close the file. 如果fin是循环中的局部变量,则当外循环循环时, fin将被销毁,这将关闭文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM