Error: Expression must have integral or unscoped enum type while reading d_name

Error: Expression must have integral or unscoped enum type while reading d_name

我需要检查文件夹中是否存在以 .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 << dp->d_name;作为错误:表达式必须具有整数或无作用域的枚举类型

听起来你想使用 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...";