file: fscanf problem

This is a discussion on file: fscanf problem within the C Programming forums, part of the General Programming Boards category; i used this codes to enter records at a file: Code: fp=fopen("account.dat","w+"); clrscr(); for(x=0; x<3; x++) { printf("\nEnter account number: ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    3

    Unhappy file: fscanf problem

    i used this codes to enter records at a file:

    Code:
    	fp=fopen("account.dat","w+");
    	clrscr();
    	for(x=0; x<3; x++) {
    		printf("\nEnter account number: ");
    		scanf("%s",&record[x].accountno);
    		printf("\nEnter PIN: ");
    		scanf("%s",&record[x].accountpin);
    		printf("\nEnter initial balance: ");
    		scanf("%d",&record[x].balance);
    		for(y=0; y<5; y++) fprintf(fp,"%c",record[x].accountno[y]); fprintf(fp," ");
    		for(y=0; y<4; y++) fprintf(fp,"%c",record[x].accountpin[y]); fprintf(fp," ");
    		fprintf(fp,"%d\n",record[x].balance);
    		}
    i input accountno, accountpin, and balance thrice and respectively: and account.dat contains:
    abcde 1234 2000
    fghij 5678 4000
    klmno 9012 6000
    and in another program, i used fscanf to load up the records... but some special characters appear in part of record[x].accountpin,

    http://i35.photobucket.com/albums/d1...vin/cerror.jpg

    Code:
    fp=fopen("account.dat","r+");
    	clrscr();
    	for(x=0; x<3; x++) {
    	fscanf(fp,"%s",&record[x].accountno);
    	fscanf(fp,"%s",&record[x].accountpin);
    	fscanf(fp,"%d",&record[x].balance);
    	printf("%s %s %d \n",record[x].accountno,record[x].accountpin,record[x].balance);}
    help me pls, how can i load up my records w/o any errors? as you can see in the image, accountpin merge with accountno, and it contains some special character, and i dont know why...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > for(y=0; y<5; y++) fprintf(fp,"%c",record[x].accountno[y]);
    You're printing the junk in the string which is left there from previous runs.
    Since you read a string, just do
    fprintf(fp,"%s",record[x].accountno);

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Looking at that output you provided, it's obvious that you're accountno and accountpin fields are not properly NULL terminated... or you aren't using %s correctly in this case.
    Code:
    printf("%s %s %d \n",record[x].accountno,record[x].accountpin,record[x].balance);
    When you print the accountno field you get abcde1234 (this is because you are using %s and there is no NULL after the accountno field which is what %s looks for to stop its printing) followed by the funky character and then fortunately you seem to encounter a NULL which stops the printing. Then you print accountpin and again get 1234 followed by the funky character because there is no proper NULL after those 4 characters.

    You can either increase the size of those fields by 1 character and make sure you NULL terminate them before printing or you can alter your printf statement slightly to this:
    Code:
    printf("%5.5s %4.4s %d \n",record[x].accountno,record[x].accountpin,record[x].balance);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    i think i dont have any problem in printing, my purpose of printing the output is to know if my variables has the same value, may i know how i can load up the records with no errors or how can i fscanf / fread in a proper way?

    sorry for the late details
    Last edited by catatonicalvin; 03-08-2006 at 03:11 PM.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    how i can load up the records with no errors or how can i fscanf / fread in a proper way?
    well, the use of fscanf in your code is right to an extent. if u look at Salems post u will come to know where the mistake is. as u are reading the string from the file dont need to specilfy the &(address of operator ). i have posted u a sample code of how to use the fscanf. and about the fread() fucntion, as far as u conser me u dont need that fucntion in your code. this fucntion basically used for reading data from binary file in terms of chunks of bbytes from the file. it make more sense when u wanted to read one record at a time from a file binary file.

    Code:
    #include<stdio.h>
    
    int main()
    {
        FILE *fp;
        char str[25];
        int num1,num2,num3;
        
        if((fp = fopen("test.txt","r+"))==NULL)
        {
            printf("Error: File cannot be opened\n");
            getchar();
            return 1;
        }
        else
            fscanf(fp,"%s %d %d",str,&num2,&num3);
        
        printf("%s [ %d %d ]",str,num2,num3);
        fclose(fp);
        getchar();
        return 0;
    }
    /* my output
    hello [ 10 20 ]
    
    my test.txt file
    hello 10 20
    */
    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 12:14 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM

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