c++ - 没有匹配函数调用 'fscanf'

标签 c++ scanf

例如,我正在尝试从文件中读取时间 (12:00 A) 我需要阅读所有三个部分。这是我的。

#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;

int main()
{
    string name, filename;
    ifstream inputFile;

    cout << "What is the name of the file to be provessed ";
    cin >> filename;
    inputFile.open(filename.c_str());

    getline(inputFile, name);
    fscanf (inputFile, "%d:%d %c", &startH, &startM, &startAP);
    fscanf (inputFile, "%d:%d %c", &endH, &endM, &endAP);
    inputFile >> payRate;

我从标题中得到错误,我不确定我做错了什么。

最佳答案

函数fscanf是 header 中声明的标准 C 函数 <cstdio>以下面的方式(我将展示如何在 C 中的 header <stdio.h> 中声明该函数。事实上除了关键字 restrict 在 C++ 中使用相同的声明)

int fscanf(FILE * restrict stream,
const char * restrict format, ...);

如您所见,没有 std::ifstream 类型的参数

因此编译器发出错误,因为它无法找到名称为 fscanf 且第一个参数类型为 std::ifstream 的函数,同时它无法隐式转换类型为 std::ifstream 的对象。指向 FILE * 类型的指针

不应将 C++ 流函数与 C 流函数混合使用。

关于c++ - 没有匹配函数调用 'fscanf',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22290546/

相关文章:

c++ - 如何在 CMake 中使用 c++20 模块?

c++ - 以有效的方式在一组边中找到现有的圆

c++ - 你如何让 FindFirstFile 打开你给它的路径的子目录?

c - C中不输入任何字符

c - Sscanf 在提取字符串时未按预期运行

c++ - 为什么 C++ iostream 总是左手分配?

c++ - cpp 中基于 FFT 的图像配准(可选地使用 OpenCV)?

C - scanf 行为不当

c - (C) 扫描后比较 long long 数组的两个元素不起作用

c - 我如何使用scanf读取c中的txt文件