Thread: file: fscanf problem

  1. #1
    Registered User
    Join Date
    Dec 2005

    Unhappy file: fscanf problem

    i used this codes to enter records at a file:

    	for(x=0; x<3; x++) {
    		printf("\nEnter account number: ");
    		printf("\nEnter PIN: ");
    		printf("\nEnter initial 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," ");
    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,

    	for(x=0; x<3; x++) {
    	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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > 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

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    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.
    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:
    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
    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
    Cambridge, UK
    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.

    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");
            return 1;
            fscanf(fp,"%s %d %d",str,&num2,&num3);
        printf("%s [ %d %d ]",str,num2,num3);
        return 0;
    /* my output
    hello [ 10 20 ]
    my test.txt file
    hello 10 20

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
Website Security Test