0

The following code works fine in Dev C++ but when I am running in turbo C I am not getting any output. I already have data stored in users.bin file.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_LIMIT 50
struct User {
   char userName[20];
   char password[20];
   float accountBalance;  
   short int failedAttempts;
   char status;             //---------- l for locked account and u for non-locked account
}user[MAX_LIMIT];
FILE* fp;
void main()
{
    short int userCode;
    char uName[10];
    //will read the available user information from users.bin file
    fp=fopen("users.bin","rb");
    fread(user,sizeof(struct User),MAX_LIMIT,fp);
    printf("%s",user[1].userName);
}
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
havegudday
  • 65
  • 6
  • So stop using Turbo C? :) – klutt Oct 28 '20 at 14:49
  • 2
    Check if `fopen()` is successful (`fp != NULL` after that) and stop using `fp` to file processing if it is `NULL. Having it printing error message when there are errors is also helpful to diagnosis. – MikeCAT Oct 28 '20 at 14:49
  • Is there *any* good reason to use 30+ years old compiler today? – Eugene Sh. Oct 28 '20 at 14:49
  • Some will not believe you until you show a check of `fp` after `fopen()`, in the code, where it should always be. – Weather Vane Oct 28 '20 at 14:50
  • it is for a project and those teachers want to run it in Turbo C – havegudday Oct 28 '20 at 14:50
  • "These teachers" should look out of the window and realize it is the end of 2020 :( Anyway, you are not the first one. – Eugene Sh. Oct 28 '20 at 14:51
  • @havegudday I'm a bit curious. Why do they want that? There are several completely free top class modern compilers out there for several platforms. – klutt Oct 28 '20 at 14:51
  • Also the placements of bytes representing the (array of) structure may be different from what you expect because some paddings may or may not be added. How did you create the file `users.bin`? – MikeCAT Oct 28 '20 at 14:52
  • @klutt I guess it is because they teach the course they have prepared in the previous century and are lazy to change anything. Or maybe it is the course that was prepared by *their* teachers... – Eugene Sh. Oct 28 '20 at 14:53
  • I'm actually astonished by it. I mean, using old and outdated software is one thing, but completely ancient? – klutt Oct 28 '20 at 14:54
  • @klutt maybe it's a Dell 286. It's a cultural thing too. If something works, it is not thrown away. – Weather Vane Oct 28 '20 at 14:55
  • You need to recreate your `users.bin` from scratch for Turbo-C. Alternatively, use a portable file format. Files made by writing`struct`s are not portable. – n. m. could be an AI Oct 28 '20 at 14:56
  • 1
    @Eugene in some rural schools in Africa they don't even have *pencil and paper!* There is a very different world out there to your own zone. – Weather Vane Oct 28 '20 at 14:57
  • What is the value of fp? What is the return value of fread? What have you tried to get this to work? – kungjohan Oct 28 '20 at 15:00
  • @EugeneSh.: Anyone wishing to be good at C needs to understand both the language that the Standard was intended to describe (including what the published Rationale for the Standard referred to as "popular extensions"), and the language that is processed by "top class" compilers whose maintainers value "clever optimizations" over correctness or suitability for a maximal variety of purposes. – supercat Oct 28 '20 at 15:03
  • @WeatherVane *" in some rural schools in Africa they don't even have pencil and paper!*" - True, but a computer suitable for running Turbo C is probably more expensive than one suitable for gcc ;) – klutt Oct 28 '20 at 15:05
  • This question is likely the answer to your question. I cannot mark as dup, because I have already close voted. https://stackoverflow.com/questions/4274055/what-is-a-portable-way-to-writie-a-struct-to-a-file-in-c – klutt Oct 28 '20 at 15:08
  • In addition you need to check the return value of every function you call, and print an appropriate message if the call is not successful. If all else fails, use the debugger that comes with Turbo C and peek into your data. – n. m. could be an AI Oct 28 '20 at 15:15
  • Anyway, the problem here is likely that Turbo C uses 16 bit alignment, but modern PC uses 32 bit. – Lundin Oct 28 '20 at 15:15
  • 1
    @Lundin *"Being poor is no excuse for running Turbo C"* - I'm gonna make a t-shirt with that! :D – klutt Oct 28 '20 at 15:30
  • @MikeCAT I created users.bin file on your suggestion through turbo C and now I am able to access the file data. Thanks – havegudday Oct 28 '20 at 15:30
  • @klutt Lol, I think it is going to be quite popular – Eugene Sh. Oct 28 '20 at 15:31
  • @havegudday I think you should get one of those t-shirts and start a revolution! – klutt Oct 28 '20 at 16:04

0 Answers0