【问题标题】:Error: Expression must have integral or unscoped enum type while reading d_name错误:在读取 d_name 时,表达式必须具有整数或无范围枚举类型
【发布时间】:2019-05-06 15:28:59
【问题描述】:

我需要检查文件夹中的文件名是否以 .config 结尾。所以我使用下面的代码。

#include "stdafx.h"
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <fstream>
#include <iostream>
using namespace std;


void main(){
DIR *dirp;
struct dirent *dp;
char prnStr[254];

if ((dirp = opendir("D:/New folder/")) == NULL) {
        cout << "Could not open directory!!";
        return;
    }
    if ((dp = readdir(dirp)) != NULL) {
        prnStr << dp->d_name;
        size_t findprn = prnStr.str().find(".config");

        if ((int)findprn != -1) {
            cout << "Found .config File!!!!!";
        }
        else {
            cout << "No .config files detected at this time...";
        }
    }
   }

在这一行出现错误 prnStr d_name; as 错误:表达式必须具有整数或无范围枚举类型

【问题讨论】:

  • prnStr &lt;&lt; dp-&gt;d_name 毫无意义。目前尚不清楚您认为它应该实现什么,但无论它是什么,它都不会那样做。 prnStr.str()也是如此

标签: c++ visual-c++ directory dirent.h


【解决方案1】:

听起来您想使用std::ostringstream。把prnStr改成这样的对象。

换行

char prnStr[254];

std::ostringstream prnStr;

另外,您正在寻找.config 文件。也许你应该换行

        cout << "Found .prn File!!!!!";

        cout << "Found .config File!!!!!";

改变

        cout << "No .prn files detected at this time...";

        cout << "No .config files detected at this time...";

【讨论】:

  • 感谢@R Sahu 如果我将 prnStr 更改为 std::ostringstream 那么这 2 行将抛出错误 prnStr d_name; size_t findprn = prnStr.str().find(".config");没有操作符
  • @AlexMathew,您使用的是什么平台和编译器?我有点担心您使用的是void main()。该标准要求您使用int main()
  • @AlexMathew 你是不是忘了#include &lt;sstream&gt;
  • @molbdnilo @R Sahu 谢谢。错误已解决,但我没有在该目录中获取文件。我在目录中添加了配置文件,但 d_name 是“。”。所以总是没有找到文件消息
  • @AlexMathew 您只读取了第一个目录条目,即.(当前目录本身)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多