-2

This is my code for loading data from binary file to array of structs but I get error that file is not found. File is there and it works with other functions.

void ispis2(void) {
    float data_num = brojanje();
    int N = data_num / 3;
    ARTIKL artikl[120];
    FILE *fp = NULL;
    fp = fopen("artikli.bin", "rb");
    if (fp == NULL) {
        fprintf(stderr, "Pogreska: %s\n", strerror(errno));
    }
    else {
        for (int i = 0; i < N; i++) {
            fread(&artikl[i].ime, sizeof(artikl), 1, fp);
            fread(&artikl[i].cijena, sizeof(artikl), 1, fp);
            fread(&artikl[i].ID, sizeof(artikl), 1, fp);
        }

        for (int i = 0; i < N; i++) {
            printf("Ime: %s", artikl[i].ime);
            printf("Cijena: %f", artikl[i].cijena);
            printf("ID: %d", artikl[i].ID);

        }
    }
}
  • 1
    The file is not there where you think it is, check where your program runs. Tell us more about your platform/IDE/compiler etc. – Jabberwocky Jun 08 '18 at 07:52
  • 1
    Double check the file name and use the full path as necessary. – 9769953 Jun 08 '18 at 07:53
  • 3
    This: `fread(&artikl[i].ime, sizeof(artikl), 1, fp);` looks very suspicious, as it's basically saying that the single field `ime` has the same size as the entire array of 120 `ARTIKL`s. That makes no sense, since the field is in one element of that array. You probably meant `sizeof artikl[i].ime`. – unwind Jun 08 '18 at 07:59
  • 1
    Your question should have some [MCVE] and is not one since we don't know what is `ARTIKL` – Basile Starynkevitch Jun 08 '18 at 08:32
  • I am using sam file for function that writes into binary file and works perfectly. ARTIKL is a struct. I adjusted fread to sizeof artikl[i].ime and it still doesnt work. – J. Markovic Jun 08 '18 at 08:44

1 Answers1

1

You probably are not running your program in the environment (notably check the current working directory) you want.

Of course your fread-s smell bad. Read again the documentation of standard C input/output functions, in particular of fread. The sizeof(artikl) argument is surely wrong (but since your question don't explain what an ARTIKL really is, we cannot help more).

The notion of directory is unknown by the C11 standard n1570. See also this. But if you are on a POSIX system, you could query it with getcwd(3). Other operating systems have different ways of querying it (e.g. GetCurrentDirectory on Windows). Read Operating Systems: Three Easy Pieces to understand more about operating systems, and read also a good C programming book.

So you could perhaps improve the error handling:

if (fp == NULL) {
    fprintf(stderr, "Pogreska: %s\n", strerror(errno));
    char wdbuf[128];
    memset(wdbuf, 0, sizeof(cwdbuf));
    fprintf(stderr, "working directory: %s\n", 
            getcwd(wdbuf, sizeof(wdbuf));
}

(the above code don't handle failure of getcwd; a robust program should handle that)

BTW, I am not sure that handling binary files like you do is worthwhile (a binary format is very brittle, and you need to document its format very precisely; remember that most of the time, data is more valuable than the application processing it). You might consider instead storing the data in some textual format (like JSON, YAML, ....) or in some sqlite database. Consider using appropriate libraries (e.g. jansson for JSON, libsqlite for sqlite)

Don't forget to compile your code with all warnings and debug info (so gcc -Wall -Wextra -g with GCC). Read the documentation of your compiler (e.g. how to invoke it) and of your debugger. Use the debugger to understand the behavior of your program.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • This is project for school and I am trying to make cash register. I need to have it done today so if you can help in any way I would be grateful. I am using visual studio 2017 and that binary file is something like a database for all items in store. – J. Markovic Jun 08 '18 at 08:46
  • You won't do it for today. You are late, and there are some basic concepts of C programming that you still don't understand. I'm sorry about that, but we cannot really help! For your next school project, put more time and efforts in it. BTW, StackOverflow is *not* a *do-my-homework* site – Basile Starynkevitch Jun 08 '18 at 08:50
  • I need to have some kind of cash register program today. Do you at least have an idea for simple version of that? – J. Markovic Jun 08 '18 at 09:01
  • No, you believe I will do your homework. But I won't. I'm probably older than your father, and I did taught at university, so I understand the importance that students write their program on their own. See http://norvig.com/21-days.html ; next time, work more! – Basile Starynkevitch Jun 08 '18 at 09:04
  • Then my answer should be enough. Of course you need to spend a few days reading some good C programming book, and reading the documentation of every function that you are using – Basile Starynkevitch Jun 08 '18 at 09:07