I have a problem if FILEs

This is a discussion on I have a problem if FILEs within the C Programming forums, part of the General Programming Boards category; >>abd for the xp micrpswft sed that it is wrten in java. I assume you mean "and for the xp ...

  1. #16
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    >>abd for the xp micrpswft sed that it is wrten in java.

    I assume you mean "and for the xp microsoft said that it is written in java"

    If you mean that.......that makes me laugh my head off.

    I mean....the idea of all those XP developers all sitting around JBuilder wearing those SunSoft baseball hats......come on thats the funniest thing ever.....

  2. #17
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    U like the smiley faces don't you?

  3. #18
    Liran Oz
    Guest
    Here is the solution you are searching for,
    #include<stdio.h>
    //-------------------------------------
    struct data
    {
    int id;
    int gpa;
    };
    //-------------------------------------
    //-------------------------------------
    void readrec(struct data *r)
    {
    printf("\n Enter the ID No: ");
    scanf("%d",&r->id);
    printf("\n Enter the GPA: ");
    scanf("%d",&r->gpa);
    }
    //-------------------------------------
    main()
    {
    FILE *fp;
    struct data r;
    int ch=0;

    fp=fopen("struct.txt","wb");
    printf("\n Enter (1)to write. (0)to EXIT.");
    scanf("%d",&ch);
    while(ch!=0)
    {
    readrec(&r);

    fwrite(&r,sizeof(struct data),1,fp);
    printf("\n Enter (1)to write. (0)to EXIT.");
    scanf("%d",&ch);
    }
    fclose(fp);

    fp=fopen("struct.txt","rb");
    while((fread(&r,sizeof(struct data),1,fp)) != 0)
    {
    printf("\nID:%d\t\tGPA:%d",r.id,r.gpa);
    }
    fclose(fp);

    }

    note: when you put new line (fprint(fp, "\n"); ) and you are not reading it back in the loading routine, you are destroying the aligment of the saved file structure.

    the problem with the last entry displayed by the load routine caused by the fact that you check for the EOF (using feof), but in the last reading the fread havn't read the EOF yet, and in the next time the loop was running, fread red it and added it to the area pointed by the address of the struct, causing it to show bad results.

    note: if you want the file to use older entrys, change the writing mode to ab (now it's wb).

    also note: the code was tested with vc6, and it found working currectly.
    liran_oz@hotmail.com

  4. #19
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    ...so they're microsoft...and they're writing thier latest OS in Java...that alone is funny. What's more is I'm pretty sure you CAN'T write an OS in Java... I mean, there aren't even POINTERS for chrissakes...

    Oh man, where do you get your info?

  5. #20
    Registered User
    Join Date
    Nov 2001
    Posts
    23
    scanf("%d",&r->id);


    Hi guys,
    I was just wondering... for the above code, since r is already a pointer, why do you want the address of the pointer?

    wouldn't it be better to have

    scanf("%d",r->id);


    your comments please

  6. #21
    Registered User
    Join Date
    Jan 2002
    Posts
    1

    Re: thank you Prelude for answering

    [QUOTE]Originally posted by talal*c

    hello, infact there is nothing wrong with your code, if you use fread to read "struct.txt" u'll see that u can get what u've written..
    but if u try to see the contents of "struct.txt" by external programs like notepad.exe instead of reading records by c code, u will see different characters, and this is natural.. because when u open a file the content of file is what meaning u get..


    here is the code..

    void writerec(struct data *r)
    {
    printf("ID : %d \n",r->id);
    printf("GPA : %d \n",r->gpa);
    }

    main()
    {
    ..... bla bla

    while(fread(&r,sizeof(struct data),1,fp))
    writerec(&r);

    fclose(fd);
    getch();

    .....
    }

  7. #22
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi,
    I can see from reading the posts what the problem is. What talal*c is trying to do is run his program then open the file in a text editor and hoping to view the results. If he is writing to a binary file this is impossible. As it says writing binary stores on file as binary. If he requires to read the file he will have to open in text format and then use fputs, fprintf or other to write to the file.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  8. #23
    Registered User
    Join Date
    Dec 2001
    Posts
    21
    ohh than you vare mutch
    Liran Oz Liran Oz Liran Oz Liran Oz Liran Oz Liran Oz Liran Oz
    thank you

    this problom was driving me crazy

    thank you agen
    and thanks avry budy<<<

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem compiling files that store functions
    By tiachopvutru in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2008, 06:42 PM
  2. Problem with Header files.
    By chakra in forum C Programming
    Replies: 5
    Last Post: 05-10-2008, 02:30 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. problem with sending files to a class method..
    By utoots in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2003, 01:38 AM
  5. problem with resource files
    By nickeax in forum Windows Programming
    Replies: 1
    Last Post: 03-14-2003, 02:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21