0

This was an exam question a few years ago, and I am struggling to solve it.

I have to build a program that reads from a text file a set of number, with a space in between, that could either be integers or floats. Then it prints the number with the maximum number of digits in it using a recursive function.

I don't know how to read the numbers from the file. If I read them all like floats they end up with the same amount of digits. Here's my code until know, but I don't know how to proceed. For example:

In the text file we have the numbers: 1 2 1.5 1.456 2 2.78 7

The number with the maximum number of digits is 1.456 with 4 digits in it.

#include <stdio.h>
#include <stdlib.h>


int main()
{
    int n,i,nr=0;
    float v[100];
    FILE *fp;
    fp = fopen("text.txt","r");
    while(fscanf(fp,"%f",&v[nr])==1){
        nr++;
    }
    fclose(fp);



    return 0;   
}
Michi
  • 5,175
  • 7
  • 33
  • 58
Al.Ka
  • 21
  • 5
  • you should check fp `if (fp==NULL){ /* error */ }` – Michi Jun 08 '16 at 18:21
  • @Michi I always do that but it is not my problem now – Al.Ka Jun 08 '16 at 18:22
  • 3
    Can you give a better example? Example input and output? – Tyler Jun 08 '16 at 18:22
  • Then show us a possible content of that file and an input/output like @Tyler said – Michi Jun 08 '16 at 18:22
  • I updated the question – Al.Ka Jun 08 '16 at 18:24
  • Read one character at a time with `fgetc`. There are five possibilities: digit, dot, space, newline, or other. – user3386109 Jun 08 '16 at 18:32
  • 1
    From @Tyler's comment, emphasis on **output** (and possibly something about how your recursive function works as it seems to not work on all floats). – ebyrob Jun 08 '16 at 18:33
  • why make things complicated. Have float variable `maxDigits` set it to `0.0f` read each successive number, into a temporary float value. Compare the just read (temporary) float value with the value in `maxDigits` if maxDigits is less than temporary float value, then assign maxDigits = temporary'. Loop until EOF encountered. – user3629249 Jun 09 '16 at 07:03

2 Answers2

2

If you treat the input as a simple string, you should be able to just strlen(), no?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
    size_t maxdigits = 0, i;
    char buf[128], winner[128];
    char winner[128];
    FILE* fp = fopen("text.txt", "r");
    if (fp == NULL) {
       printf("missing text.txt\n");
       return -1;
       }
    memset(winner, 0, sizeof(winner));
    while(fscanf(fp, "%s", buf) == 1) {
        i = strlen(buf);
        if (strchr(buf, '.') != NULL)
            i--;
        if (i > maxdigits) {
            maxdigits = i;
            strcpy(winner, buf);
            }
        }
    fclose(fp);
    printf("winner '%s' has %d digits\n", winner, maxdigits);
    return 0;
    }
user590028
  • 11,364
  • 3
  • 40
  • 57
  • 1
    It should ignore decimals. – Alyssa Haroldsen Jun 08 '16 at 18:36
  • `int i` should be `size_t i` because of the return type of `strlen`, but even if you fix it you still have comparison problem with maxdigits which is an int – Michi Jun 08 '16 at 18:41
  • and `maxdigits` too ...followed up with `%zu` format specifier. – Weather Vane Jun 08 '16 at 18:42
  • by the way, when you fix that...you end with a segfault – Michi Jun 08 '16 at 18:42
  • I appreciate the various tweaks -- but I really was hoping to "lead" the OP to finished product -- NOT create it myself. The version above compiles & works on his sample input set using `gcc 4.4.7`. Enhancements should be up to OP. – user590028 Jun 08 '16 at 18:45
  • I meant this part [CLICK](http://pastebin.com/raw/4N0aEn9z) where the check of *fp is not present. – Michi Jun 08 '16 at 18:47
  • Did you create 'text.txt' before running valgrind? Program has no error checking (so OP can focus on the core of the program). Obviously production code would need to check `fopen` succeeded AND make sure `fscanf` did not exceed size of `buf`. Also, program does not explicitly close `fp`. All exercises for OP. – user590028 Jun 08 '16 at 18:54
  • @user590028 You are missing the whole point here, and yes I used [This](http://ideone.com/N7bS6j) and valgrind says [This](http://pastebin.com/raw/KiPXE0N0) – Michi Jun 08 '16 at 18:57
  • @Michi well what *is* your point? Tell us the error in your own linked code for heaven sake. Apart from not initialising `winner`. – Weather Vane Jun 08 '16 at 19:04
  • I think valgrind is warning you about memory leak no? That would likely be because `fp` was not closed. – user590028 Jun 08 '16 at 19:05
  • I surrender -- all fixes applied. – user590028 Jun 08 '16 at 19:09
  • @user590028 Thanks man! Only the idea of treating the input as a string would be fine, but you even provided the code. – Al.Ka Jun 08 '16 at 19:43
0

First you have to use the "split" function.

Seems like here is something for you enter link description here

Community
  • 1
  • 1
Daniel Azamar
  • 672
  • 7
  • 8