0

I'm trying to make two functions, a writefile and a readfile. Obviously, writefile will write to a file using the contents from the struct pokemon, and readfile will read the file line by line and get the contents of the file. For example, after writing to a file successfully with two pokemon in the struct, the text file will look like this:

30
Pikachu
12
Charizard

The read file is supposed to store the level and name in the struct, and I'm currently having trouble. I've already been told that using fgets is correct, but I'm not doing it correctly.

struct pokemon
{
    int level;
    char name[30];
};

int readfile(struct pokemon pokearray[], int* num, char filename[])
{

    FILE * fp;
    int i = 0;
    fp = fopen(filename, "r");

    pokearray[10].level;

    if (fp == NULL)
    {
        printf("errorrr");
        return -1;
    }

    while (!feof(fp))
    {
        fgets(pokearray[i].level, 10, fp);

    }

    fclose(fp);

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • What is the statement `pokearray[10].level;` supposed to do? How many elements are there in the `pokearray` array? – Some programmer dude Oct 12 '22 at 03:42
  • 1
    Also, [`while (!feof(fp))` should always be considered wrong](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong). Use `for` loop instead. Then the counting of `i` becomes much simpler, and you can add a condition to make sure it doesn't go out of bounds. Also add a check for what `fgets` *returns* as part of the condition, so you know when there's an error of end of file. – Some programmer dude Oct 12 '22 at 03:44
  • And note that `fgets` reads *strings*. If you want to read integers, you need to convert the string to an integer after calling `fgets`. For example by using [`strtol`](https://en.cppreference.com/w/c/string/byte/strtol). – Some programmer dude Oct 12 '22 at 03:46
  • Where is the definition of the `struct pokemon` type? Do you allocate the array in the calling code, or should the function shown allocate the array? It looks like you'll need to read two lines per character, though if you use `fscanf()`, you can use a single call to that for each character. Can the character names ever have spaces (several words)? How big can the character names be? – Jonathan Leffler Oct 12 '22 at 04:52
  • Why not combine it, using a temp array for the level and convert with `sscanf()`. Then you could do something like `char tmplevel[32];` and then `while (fgets (tmplevel, size, stream) && sscanf (tmplevel, "%d", &pokearray[i].level) == 1) && fgets (pokearray[i].name, size, stream)) { i++; }` or something similar that validates each part before considering the read and conversion of level and name a success? – David C. Rankin Oct 12 '22 at 05:07
  • struct pokemon { int level; char name[30]; }; – Isaiah Dela Cruz Oct 12 '22 at 05:07
  • Please [edit] your question to add details, like a [mre]. – Some programmer dude Oct 12 '22 at 05:08
  • David does that basically put the name and level in its own array THEN puts it into the pokemon.level and pokemon.name respectively? – Isaiah Dela Cruz Oct 12 '22 at 05:11
  • No. If you have a struct with a member `level` and a member `name` that just allows you to read both at once. Reading the line with the `level` as a string first into `tmplevel` and then converting to the `pokearray[i].level` using `sscanf()`. That avoids the problem with the trailing `'\n'` left by `fscanf()` and also avoids the additional characters left unread in your input stream in the event of at *matching-failure*. You only increment `i` (to your next struct in the array of struct) if both reads and the conversion succeed. – David C. Rankin Oct 12 '22 at 05:31
  • Do you have a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) you can post? That will encourage an answer as it will decrease the likelihood your question is closed for not meeting the requirements of the site. Just post a complete version of what you have, so we can copy/paste/compile to help answer your question. – David C. Rankin Oct 12 '22 at 05:35

0 Answers0