0

Here is the question:

Write a function that takes a file handle f as a parameter and returns the number of occurrences of all digits in the file found in the file. Write a program to show how this function can be used to find the number of digits in the file "abd.dat".

After I finished this question, I noticed it need a return in the function, I just do the print inside of the function. I made some try to modify the code to do the print in main() function, but it seems not work, the output becomes weird.

#include <stdio.h>
#include <stdlib.h>
#define BASE 10
int digits(FILE *);
int main()
{
    FILE *fp = fopen("abc.dat", r);
    digits(fp);
    fclose(fp);
    return EXIT_SUCCESS;
}
int digits(FILE *f)
{
    long long num, digits;
    int i, lastDigit;
    int occu[BASE];
    fscanf(f, "%lld", &num);
    for (i = 0; i < BASE; i++)
    {
        occu[i] = 0;
    }

    digits = num;
    while (digits != 0)
    {
        lastDigit = digits % 10;
        digits /= 10;
        occu[lastDigit]++;
    }

    printf(Occurrences of each digit in %lld is: \n, num);
    for (i = 0; i < BASE; i++)
    {
        printf("Occurrences of %d = %d\n", i, occu[i]);
    }
}
PatientJK
  • 43
  • 5
  • Can you provide some code so we could see where the potential error takes place? – Adan Vivero Nov 19 '19 at 17:12
  • And what exactly is your question? – hager Nov 19 '19 at 17:14
  • Where's the code in main that doesn't work? – ikegami Nov 19 '19 at 17:16
  • sorry I was adding the code. – PatientJK Nov 19 '19 at 17:16
  • I would like to not print the result in the digits funcition, instead of using return then print in main(). I tried to return occu[], but when I print in mian the output becomes strange. – PatientJK Nov 19 '19 at 17:19
  • 2
    I'd accept another parameter and write the result in it. Thus `void digits(FILE* f, int* occu)`. – AProgrammer Nov 19 '19 at 17:20
  • 1
    occu array is declared inside digits function, and that is on the stack. You need to pass in storage in function – OldProgrammer Nov 19 '19 at 17:21
  • What is the output you're trying to get? In this code, it's just returning an integer for the main. If you want it to print something else, you might need to change that int in the first line to something else. – Adan Vivero Nov 19 '19 at 17:23
  • 1
    Your code assumes the file contains a single integer, and that it fits in a `long long`. You should be using `read` or `fread` to read chunks of the file, then counting the characters that are `>= '0'` and `<= '9'`. /// Furthermore, your code attempts to count the number of instance of each different digit, but the question merely asks for the number of digits. – ikegami Nov 19 '19 at 17:23
  • In your instructions, it seems as if you want to count the number of occurrences and return it. You might want to print then the statement in your main and put in the method so it gives you the number of occurrences. Your function is only returning 1 digit. – Adan Vivero Nov 19 '19 at 17:25
  • @AdanVivero The code I provide can give me the correct output, but I want to do a little change to let the digits function only give the return, not printing the results. – PatientJK Nov 19 '19 at 17:29
  • I store all the counts in occu[BASE], if I would like to give a return about the array. I used return occu; then create a empty array in main() to gets the result compute from the digits function. but it does not work – PatientJK Nov 19 '19 at 17:31
  • Does this answer your question? [Returning an array using C](https://stackoverflow.com/questions/11656532/returning-an-array-using-c) – n. m. could be an AI Nov 19 '19 at 18:12

1 Answers1

2

you can define your function to return int* instead of int In this case you need to use malloc for not losing the array at the end of the function. after this you can print it from the main

Yoni Melki
  • 205
  • 3
  • 16