【问题标题】:Address of variable supplied instead of its value提供变量的地址而不是其值
【发布时间】:2021-12-31 14:06:03
【问题描述】:

我想在 C++ 中测试 fopenfclose 的示例,其中代码读取文件中的整数值,但如果文件为空(无整数),则检索到的值不是 0,而是 -858993460。我认为这是fscanf() 中变量的地址,但我怎么能得到null/0 的值 以下代码有问题

#include <stdio.h>

int main()
{

    {
        int a, sum = 0 ,num, n = 0;
        FILE* pFile;
        pFile = fopen("input.txt", "r");
        fscanf(pFile, "%d", &num);
        while (n != 5) {
            n = n + 1;
            sum = sum + num;
            fscanf(pFile, "%d", &num);
            
        }
        fclose(pFile);
        printf("I have read: %d numbers and sum is %d \n", n, sum);
        scanf("Hi %d", &a);
        return 0;
    }
}

input.txt:(空)或:

12 32 43 56 78

注意:代码是有问题的,因为num 永远不会被读取为0,所以循环继续进行。

我将添加有关该问题的更多详细信息:如果输入文件包含五个整数:

12 32 43 56 78

代码如下:

#include <stdio.h>

int main()
{

    {
        char str[80];
        int a, sum = 0 ,num, n = 0;
        FILE* pFile;
        pFile = fopen("input.txt", "r");
        fscanf(pFile, "%d", &num);
        while (n != 5) {
            n = n + 1;
            sum = sum + num;
            fscanf(pFile, "%d", &num);
            
        }
        fclose(pFile);
        printf("I have read: %d numbers and sum is %d \n", n, sum);
        scanf("Hi %d", &a);
        return 0;
    }
}

输出:

I have read: 5 numbers and sum is 221

【问题讨论】:

  • 如果您使用的是C++,请不要标记C
  • -858993460 = 0xFFFF FFFF CCCC CCCC,我猜这不是地址。
  • 如果您不向我们展示您的代码,则无法判断它可能有什么问题。
  • 将您的代码添加到您的问题中,我们或许可以提供帮助。
  • 显示的代码非常精神分裂。它#includes 是一个 C++ 头文件,然后继续使用其中的任何内容,而是使用 C 的 stdio.h,甚至不包括在内。

标签: c++ visual-studio-2010


【解决方案1】:

执行此操作的更多 C++ 方式如下所示。您可以以此作为参考。

#include <iostream>
#include <fstream>
#include <sstream>
int main()
{
    std::ifstream inputFile("input.txt");
    
    std::string line;
    int num = 0, sum = 0; //always initialize built in types in local/block scope 
    
    if(inputFile)
    {
        //go line by line
        while(std::getline(inputFile, line))
        {
            std::istringstream ss(line);
            
            //go through individual numbers
            while(ss >> num)
            {
                sum += num;
            }
        }
        
    }
    
    else 
    {
        std::cout<<"Input file cannot be read"<<std::endl;
    }
    inputFile.close();
    
    std::cout << "The sum of all the intergers from the file is: "<<sum<<std::endl;
    return 0;
}

以上程序的输出可见here

【讨论】:

  • @我经常使用 std:: 但我也想尝试 fopen 和 fscanf。
  • @mohamed 好的,您可以使用您认为更适合您需求的任何方法。我展示了一种方法。
  • @mohamed 这种方式也是 C++ 中最常用的方式之一。
【解决方案2】:

修复很简单,永远不会错过 IO 函数的返回值。

FILE* pFile;
// pFile = fopen("input.txt", "r");
// fscanf(pFile, "%d", &num);
// while (n != 5) {
if ((pFile = fopen("input.txt", "r")) == NULL)  // If unable to open file
    return 1;
while (n != 5 && fscanf(pFile, "%d", &num) == 1) {  // And if num is successfully assigned
    n = n + 1;
    sum = sum + num;
    // Odd: fscanf(pFile, "%d", &num);
}
fclose(pFile);

【讨论】:

    猜你喜欢
    • 2015-08-19
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    相关资源
    最近更新 更多