having a problem reading data from a binary file

[b][red]This message was edited by ph34r me at 2005-4-30 19:49:7[/red][/b][hr]
This message has been deleted by me, the topic creator. I had to post my entire code to get help for a solution, but other people in my class may browse this site, and i've offered them links to the site in case they needed help in the past. If they were to use that source code, it would be cheating, and we'd immediately fail the course if the instructor were to find out. I know the odds of that happening are slim, but i'd rather not take my chances. Sorry for any inconvenience this edit causes.

Comments

  • [b][red]This message was edited by DB1 at 2005-4-30 15:53:22[/red][/b][hr]
    : [code]
    : void main()
    : {
    : FILE *in;
    : FILE *out;
    :
    : in=fopen("lab3.in.txt","r");
    :
    : if(!in)
    : {
    : printf("cannot open input file");
    : exit(0);
    : }
    :
    : out=fopen("pj5b.out.txt","w");
    :
    : if(!out)
    : {
    : printf("cannot open output file");
    : exit(0);
    : }
    : [/code]
    :
    : The problem is that this part:[code]struct person
    : {
    : int id;
    : char name[10];
    : int pr;
    : int sal;
    : } s1[10];
    :
    :
    : fseek(out, SEEK_SET, 0); // seek to the beginning of the file
    : i=0;
    : while(fread(&s1[i], sizeof(struct person), 1, out)==1)
    : {
    : printf("read in binary file: %d %s %d %d
    ", s1[i].id, s1[i].name, s1[i].pr, s1[i].sal);
    : getch();
    : i++;
    : }[/code]doesn't seem to work properly. My output doesn't print to the screen there. I've been troubleshooting the hell out of this, but I'm getting nowhere, it SHOULD be working! Please help
    :


    It's probably because you opened the file in write only mode, then attempt to read from it. Try opening it in read/write mode. Also you should be opening the file in binary mode..

    [code]
    out=fopen("pj5b.out.txt",[red]"w+b"[/red]);
    [/code]

    Also, use main with the return type of 'int', not void.


    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]



  • thanks very much for the input, I can't believe the problem was that simple.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion