读取txt文件时出现奇怪的错误

时间:2015-03-22 08:15:25

标签: c++

编辑:我收到了错误。我需要在文件名前加上“books /”。

我正在从目录中读取文件名,然后逐个打开这些文件。

我读了文件并打印了它的内容。

我为此编写了一个C ++代码,但它无效。我无法在其中发现任何错误。

我做错了什么?

我的代码 -

#include <bits/stdc++.h>
#include <omp.h>
#include <dirent.h>

using namespace std;

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

    DIR *dirp;
    struct dirent *dp;


    if ((dirp = opendir("books")) == NULL) {
        perror("couldn't open '.'");
        return 0;
    }


    do {
        errno = 0;
        if ((dp = readdir(dirp)) != NULL) {        

            if(strlen(dp->d_name)>4){

                string str; 
                int count=0;

                ifstream in(dp->d_name);
                //cout<<dp->d_name<<"....."<<endl;
                while (!in.eof() && count<100) {
                    in >>str;
                    cout<<str;
                    count++;
                }

                in.close();
            }

        }
    } while (dp != NULL);


    if (errno != 0)
        perror("error reading directory");

    (void) closedir(dirp);
    return 0;
}

编辑:我收到了错误。我需要在文件名前加上“books /”。

0 个答案:

没有答案
相关问题