吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 718|回复: 8
收起左侧

[求助] C++代码出错如何改呢?

[复制链接]
jtwc 发表于 2023-10-3 19:10
本帖最后由 jtwc 于 2023-10-3 20:23 编辑

各位老师,C++代码出错如何改呢?谢谢了,源码如下:
错误        1        error C2679: 二进制“<<”: 没有找到接受“std::string”类型的右操作数的运算符(或没有可接受的转换)
[C++] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include <Windows.h>
 
int main() {
        std::string wavFile = "1.wav";
        std::string csvFile = "a.csv";
 
        // 监控文件
        HANDLE hFile = CreateFile(wavFile.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        if (hFile == INVALID_HANDLE_VALUE) {
                std::cout << "无法打开文件 " << wavFile << std::endl;
                return 1;
        }
 
        // 创建CSV文件
        std::ofstream csv(csvFile);
        if (!csv) {
                std::cout << "无法创建文件 " << csvFile << std::endl;
                CloseHandle(hFile);
                return 1;
        }
 
        // 监控文件声音
        DWORD bytesRead;
        BYTE buffer[44]; // WAV文件头部大小为44字节
        while (true) {
                if (ReadFile(hFile, buffer, sizeof(buffer), &bytesRead, NULL)) {
                        // 检查是否为WAV文件
                        if (bytesRead == sizeof(buffer) && buffer[0] == 'R' && buffer[1] == 'I' && buffer[2] == 'F' && buffer[3] == 'F' && buffer[8] == 'W' && buffer[9] == 'A' && buffer[10] == 'V' && buffer[11] == 'E') {
                                // 检查是否有声音
                                if (buffer[40] != 0 || buffer[41] != 0 || buffer[42] != 0 || buffer[43] != 0) {
                                        // 写入CSV文件
                                        csv << "1" << std::endl;
                                        csv.flush();
                                        std::cout << "检测到声音,已写入文件 " << csvFile << std::endl;
                                }
                        }
                }
                Sleep(1000); // 每秒检查一次
        }
 
        // 关闭文件和CSV
        CloseHandle(hFile);
        csv.close();
 
        system("pause");
        return 0;
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

sgbyg 发表于 2023-10-3 19:28

代码没问题  因为我这里可以运行
 楼主| jtwc 发表于 2023-10-3 19:34
sgbyg 发表于 2023-10-3 19:28
代码没问题  因为我这里可以运行

老师,我这个报错是咋回事呢?
sgbyg 发表于 2023-10-3 19:38
jtwc 发表于 2023-10-3 19:34
老师,我这个报错是咋回事呢?

std::string报错那就把std::string换成printf输出呗
不行就把std::string注释掉 输出是多余的不是吗
你的编译器是vs20xx自带的  而我用的是clang/mingw
不要叫老师 叫我giegie~
 楼主| jtwc 发表于 2023-10-3 19:45
sgbyg 发表于 2023-10-3 19:38
std::string报错那就把std::string换成printf输出呗
不行就把std::string注释掉 输出是多余的不是吗
...

忘了加头文件#include <sstream>
nihaoz 发表于 2023-10-3 19:47
考虑加一下#include<string>
yes2 发表于 2023-10-3 20:01
或者在std::string变量的后面加上.c_str()
小雨网络 发表于 2023-10-6 14:32
#include <iostream>
#include <fstream>
#include <Windows.h>
#include <string>

int main() {
    std::string wavFile = "1.wav";
    std::string csvFile = "a.csv";

    // 监控文件声音
    BYTE buffer[44]; // WAV文件头部大小为44字节
    while (true) {
        // 监控文件
        HANDLE hFile = CreateFile(wavFile.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        if (hFile == INVALID_HANDLE_VALUE) {
            std::cerr << "无法打开文件 " << wavFile << std::endl;
            return 1;
        }

        DWORD bytesRead;
        if (ReadFile(hFile, buffer, sizeof(buffer), &bytesRead, NULL)) {
            // 检查是否为WAV文件
            if (bytesRead == sizeof(buffer) && buffer[0] == 'R' && buffer[1] == 'I' && buffer[2] == 'F' && buffer[3] == 'F' && buffer[8] == 'W' && buffer[9] == 'A' && buffer[10] == 'V' && buffer[11] == 'E') {
                // 检查是否有声音
                if (buffer[40] != 0 || buffer[41] != 0 || buffer[42] != 0 || buffer[43] != 0) {
                    // 创建CSV文件
                    std::ofstream csv(csvFile, std::ios::app);
                    if (!csv) {
                        std::cerr << "无法创建文件 " << csvFile << std::endl;
                        CloseHandle(hFile);
                        return 1;
                    }

                    // 写入CSV文件
                    csv << "1" << std::endl;
                    csv.flush();
                    std::cout << "检测到声音,已写入文件 " << csvFile << std::endl;
                    
                    // 关闭CSV
                    csv.close();
                }
            }
        }

        // 关闭文件
        CloseHandle(hFile);
        Sleep(1000); // 每秒检查一次
    }

    return 0;
}
 楼主| jtwc 发表于 2023-10-7 13:23
小雨网络 发表于 2023-10-6 14:32
#include
#include
#include

感谢老师
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-5-22 17:44

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表