Home > Blockchain >  Address of variable supplied instead of its value
Address of variable supplied instead of its value

Time:01-01

I wan testing an example of fopen and fclose in C where the code reads the integer values in thee file but if the file is empty (no integers), the value retrieved is not 0 but -858993460. i think this is the address of the variable in fscanf() but how could i get the null or /0 value the following code is problematic

#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 : (empty) or :

12 32 43 56 78

NOTE: the code is problematic because num is never read as 0 so the loop keeps going on.

I will add further details on the problem: if the input file contains five integers:

12 32 43 56 78

and the code goes as follows:

#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;
    }
}

output:

I have read: 5 numbers and sum is 221

CodePudding user response:

A more C way of doing this would be as shown below. You can use this as a reference.

#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;
}

The output of the above program can be seen here.

CodePudding user response:

The fix is simple, never miss returned values of IO functions.

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);
  • Related