0

I was attempting a problem regarding pointers from a book called problem solving and program design in C. we were formally introduced to functions fscanf() and fprintf() where they accept both input file and output file pointers respectively as the first argument of the function. our task is to read from an input file, extract the count for the numbers encoded in floats that are present in the input file and write each number separated by a newline character to an output file and write the number of count underneath the final number that has been written to the output file. the pseudocode for the algorithm read:

main() {
    double num = 0;     
    int count = 0; 
    input_status = fscanf(inp, "%lf", &num);     
    while (input_status != -1){
       fprintf(outp, "%.2f\n", num); 
       count++
       input_status = fscanf(inp, "%lf", &num);     
    } 
    // write to the output file the count
    fprintf(outp, "%d\n", count) 

    close(inp & outp); 
    return (0); 
}    

In the context of this particular algorithm, the follow up question asked: "why can't we write the count as the first item of the output file in this case".

well I answered: the loop basically checks whether we have reached an end of file or not - once the end of file is reached, the pointer variable basically points to the end of the file. Now unless we explicitly defined two for loops - one to count for the number of elements that are present in the input file (our count variable) and the other to write to the output file all the numbers that are present in the input file, we cannot do this as opposed to outputting the elements followed by the number of elements at the end of the file.

I want to know if my answer is valid or not.

  • 2
    Much simpler: you do know the total number of items before having read the last one. So, to do it, one would have to store all numbers read, print the count and then print the numbers stored. – Jean-Baptiste Yunès Mar 09 '23 at 13:52
  • Because you don't know (yet) dynamic memory allocation. Anyway: your algorithm is flawed, what are you going to print as first item? – DDS Mar 09 '23 at 14:06
  • @DDS I apologize for my answer earlier. In the output file I wanted to write each elements followed by its count - for input file consisting of data that are not of type float, we are essentially skipping over that while loop and writing the count as the first item in our output file if that is what you meant. – IdeadlySkies Mar 09 '23 at 14:33
  • What I was saying is that at startup, the first element it prints an invalid number (or the initialization value). AKA: the first line it prints does not contain a valid float because you print it before you read it from the file. – DDS Mar 09 '23 at 14:48
  • **sigh** thank you for pointing that out, fix: added "input status = fscanf(inp, "%lf", &num);" before the loop sequence. – IdeadlySkies Mar 09 '23 at 15:11
  • @DDS How long does it take you to get to this point (i.e. good at problem solving, dissecting how codes work in a flash, etc)? I am willing to put in the time to get good through deliberate practice as oppose to aimless practice. – IdeadlySkies Mar 09 '23 at 15:34
  • Just the time of getting a computer engineering degree (5 years) and some years passed writing code for earning a living (and there still are people well more able than me at doing this). – DDS Mar 10 '23 at 08:42

0 Answers0