Thread: I have a problem if FILEs

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    21

    Unhappy I have a problem if FILEs



    hello ther i am a new one here
    I have a problem with files :
    That i can't read and write to a file when the wreten item is
    int or float (when i write ther comes charecters whit out meaning).
    and when i write char it is very G D ...


    please solve my problem
    ofcours in C,,,

    thank you,,,,

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Could you post the code that is giving you problems? It sounds like you're using a function to print char to a file and actually sending it int or float, that can cause what appears to be garbage. Paste the function that prints to the file in reply, and don't forget to use the code tags Click here for more info.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    21

    Smile thank you Prelude for answering

    hello thank you for answering
    and thos os the code useing fwrite.
    i hope that you can help my....

    PHP Code:
    #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","a+");
        
    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);
                }

    2 know (I opend the file a,a+,w,w+,wb)but it did't work

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Change the mode to "wb" and it will work. I suppose you could also use "ab", I think anyway, but the 'b' means binary.

    The reason why you don't think it worked is because the numbers are in binary form in your datafile. Text characters appear the same in binary or text but numbers in binary when viewed in a text file look screwed up. Your program works, just change the mode in the fopen statement.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    21
    ok, this is my final program:
    PHP Code:
    #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","ab");
        
    printf("\n Enter (1)to write. (0)to EXIT.");
        
    scanf("%d",&ch);
                while(
    ch!=0)
                {
                    
    readrec(&r);
                
                    
    fwrite(&r,sizeof(struct data),1,fp);
                    
    fprintf(fp,"\n");
                        
    printf("\n Enter (1)to write. (0)to EXIT.");
                        
    scanf("%d",&ch);
                }
                
    fclose(fp);

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


    when i input
    id:1 gpa:1
    the out put is
    id:1 gpa:1
    id:10 gpa:1
    that what is in the file struct.txt----> (  ).

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    That is correct.

  7. #7
    Unregistered
    Guest
    I know in my exam i had 20/20 (FULL MARK).
    but when i run the program it did't work eather???
    the problem is still thir?



    please help me!!!!

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    21
    sorry the upper reply was me...

  9. #9
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    What problem? The program works correctly. I don't see any problem with it.

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    21

    Unhappy me to...


    i do't see a problem to and so my Dr and so my frinds

    ******but it did't work******

    please can you try to compile it and see ?
    or give me a nother code from your self ?
    please this is my only problem in the C lang
    it drivs me crazy

    I started smoking (just a jok)

    please heeeeeeeelp me !!!!

  11. #11
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Works fine.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    feof doesn't work as you expect - it only returns TRUE when some other file reading function has also returned TRUE.

    So
    Code:
    while(!feof(fp))
    
        {
    
            fread(&r,sizeof(struct data),1,fp);
    
                printf("\nID:%d\t\tGPA:%d",r.id,r.gpa);
    
        }
    Should be

    Code:
            while ( fread(&r,sizeof(struct data),1,fp) == 1 ) {
                printf("\nID:%d\t\tGPA:%d",r.id,r.gpa);
            }
    Or you could just update your while loop to check the return result of fread, and only output information when it returns success...

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    21
    my problem is still ther:::::::::::::::::::::::::::;


    I wender if my windows xp that I ues is the problem????
    coz xp is in java ? will it make sinc?

    please help mt

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > I wender if my windows xp that I ues is the problem????
    > coz xp is in java ? will it make sinc?

    What? XP is in Java? Um, no. XP is definately not written in Java. So what's your problem, other than not being able to form complete words and sentences? Can you not read? Can you not write? What exactly?

    *EDIT* Let me clarify, can your 'program' not read and write? It's obvious that you can't...

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Dec 2001
    Posts
    21
    i am a begener in c programing

    my problem that i can't read write int, float, ...from file

    the above program did't work>>
    that is my problem...


    abd for the xp micrpswft sed that it is wrten in java.

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, 05:42 PM
  2. Problem with Header files.
    By chakra in forum C Programming
    Replies: 5
    Last Post: 05-10-2008, 01: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