-1

I don't know how to code a function that browse a file with a specific modulo.

Example, if I have 3 as modulo and a file is containing :

abcdefghijk 

then, my function should returns :

adgj

My code of the function:

char f1(char* name_file, int modulo)
{
    char characters_ok[];
    unsigned char character_read;
    fileToOpen=fopen(name_file,"r");
    do
    {
        character_read=fgetc(fileToOpen);
        //and I don't know what to write...
    }
    while(!feof(fileToOpen));
    fclose(fileToOpen);
    return characters_ok;
}
  • 7
    count the characters you read, and act appropiately. – wildplasser Dec 26 '17 at 18:37
  • Function `fgetc` returns `int` so change `unsigned char character_read;` to `int character_read;`. Then you can control the loop by checking whether the function returns `EOF`, instead of the dreadful `while(!feof(fileToOpen))`. Also the function type `char` does not match what you are returning, a pointer decayed from `char[]`, and anyway: that array goes out of scope. – Weather Vane Dec 26 '17 at 18:40
  • Calling printf before calling 'errorfile' is very likely a mistake, since 'errorfile' probably wants to know the value of errno as set by `fopen`, but `printf` may change it. – William Pursell Dec 26 '17 at 18:43
  • 1
    `do ... while (!feof(...))` will lead to an infinite loop on a read error. (You could do `while(!feof(...) && ! ferror(...))`, but it's a bad idiom. See https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – William Pursell Dec 26 '17 at 18:45
  • @LinuxCrusher a better idiom is `while((character_read = fgetc(fileToOpen)) != EOF) { /* do stuff */ }` since `feof` does not do what you imagine. – Weather Vane Dec 26 '17 at 18:46
  • @WeatherVane I want to have a char type in character_read so why would I change to int character_read? I need to keep the char type of character_read. If I want to return character_read in my function what should I do then? – LinuxCrusher Dec 26 '17 at 19:36
  • @LinuxCrusher because that is the type returned by the function. Note that `EOF` is probably `-1` which is out of the range of `unsigned char`. That's the other reason why you should use `int`. BTW few of the C library functions take `char` as arguments, or return `char` as the function value. They mostly use `int`, so, go with the flow, not your own ideas. Aside: the value `'a'` has type `int` - not `char`. – Weather Vane Dec 26 '17 at 19:43
  • @WeatherVane Ok, so when my function will return this int, it will be an int corresponding to the ASCII code, right? – LinuxCrusher Dec 26 '17 at 20:54
  • As I wrote, `'a'` has type `int`. If you don't believe me, try `printf("%zu\n", sizeof('a'));` which will not print `1`. Try using `int` and see if the C library writers know what they are doing! – Weather Vane Dec 26 '17 at 21:00
  • @wildplasser thanks! I have found a way thanks to you. I will update to show if it is correct. – LinuxCrusher Dec 26 '17 at 21:09
  • I think you are looking to simply replace `//something to do...` with `*(characters_ok++) = character_read`. Note that you'll have to declare characters_ok as a pointer rather than an array to do that, which you need to do anyway if you want to return it from your function. – William Pursell Dec 26 '17 at 21:55
  • @WilliamPursell Why writing ++ at the end of characters_ok ? I want that every valided character is put my array character_ok. Example: character_ok[0]=a , character_ok[1]=d, character_ok[2]=g and character_ok[3]=j. Does your code doing the same thing? – LinuxCrusher Dec 27 '17 at 16:21
  • Yes. In my example, I'm assuming that you have declared characters_ok as a pointer. Incrementing it is a common idiom for the more verbose `characters_ok[i] = character_read; i += 1` – William Pursell Dec 27 '17 at 19:18
  • @WilliamPursell Thanks! I have updated the code. It compile without problems. Thanks to all of you! If I understand, if a question has been answered I just have to delete it right? – LinuxCrusher Dec 27 '17 at 19:48

1 Answers1

0

Solved: Thanks to people in the comments, the answer to my question is to simply count the character (with an incremented counter) I read and to use the test

compt % modulo) == 0

So, the complete answer is:

       int* f1(char* name_file, int modulo)
{
    int* characters_ok;
    int compt=0;
    int character_read;
    fileToOpen=fopen(name_file,"r");
    if (fileToOpen == NULL)
    {
        printf("%s : ",name_file);
        errorfile();
    }
    while ((character_read = fgetc(fileToOpen)) != EOF)
    {
        if ((compt % modulo) == 0)
        {
           *(characters_ok++)=character_read;
        }
        compt++;
    }
    fclose(fileToOpen);
    return characters_ok;
}