Thread: Storing structure data in txt file instead of dat file?

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    47

    Exclamation Storing structure data in txt file instead of dat file?

    hello friends,

    I am new to C

    recently i have been studying code of bank management system

    where i see the input files are stored in .dat files

    but i just wonder can i store these structure data in plain .txt file ? if so how?

    here is the code of the function

    Code:
    void new_acc()
    
    {
        int choice;
        FILE *ptr;
    
    
        ptr=fopen("record.dat","a+");
        account_no:
        system("cls");
        printf("\t\t\t\xB2\xB2\xB2\ ADD RECORD  \xB2\xB2\xB2\xB2");
        printf("\n\n\nEnter today's date(mm/dd/yyyy):");
        scanf("%d/%d/%d",&add.deposit.month,&add.deposit.day,&add.deposit.year);
        printf("\nEnter the account number:");
        scanf("%d",&check.acc_no);
        while(fscanf(ptr,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n",&add.acc_no,add.name,&add.dob.month,&add.dob.day,&add.dob.year,&add.age,add.address,add.citizenship,&add.phone,add.acc_type,&add.amt,&add.deposit.month,&add.deposit.day,&add.deposit.year)!=EOF)
        {
            if (check.acc_no==add.acc_no)
                {printf("Account no. already in use!");
                fordelay(1000000000);
                    goto account_no;
    
    
                }
        }
        add.acc_no=check.acc_no;
            printf("\nEnter the name:");
        scanf("%s",add.name);
        printf("\nEnter the date of birth(mm/dd/yyyy):");
        scanf("%d/%d/%d",&add.dob.month,&add.dob.day,&add.dob.year);
        printf("\nEnter the age:");
        scanf("%d",&add.age);
        printf("\nEnter the address:");
        scanf("%s",add.address);
        printf("\nEnter the citizenship number:");
        scanf("%s",add.citizenship);
        printf("\nEnter the phone number: ");
        scanf("%lf",&add.phone);
        printf("\nEnter the amount to deposit:$");
        scanf("%f",&add.amt);
        printf("\nType of account:\n\t#Saving\n\t#Current\n\t#Fixed1(for 1 year)\n\t#Fixed2(for 2 years)\n\t#Fixed3(for 3 years)\n\n\tEnter your choice:");
        scanf("%s",add.acc_type);
    
    
            fprintf(ptr,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d\n",add.acc_no,add.name,add.dob.month,add.dob.day,add.dob.year,add.age,add.address,add.citizenship,add.phone,add.acc_type,add.amt,add.deposit.month,add.deposit.day,add.deposit.year);
    
    
    
    
        fclose(ptr);
        printf("\nAccount created successfully!");
        add_invalid:
        printf("\n\n\n\t\tEnter 1 to go to the main menu and 0 to exit:");
        scanf("%d",&main_exit);
        system("cls");
        if (main_exit==1)
            menu();
        else if(main_exit==0)
                close();
        else
            {
                printf("\nInvalid!\a");
                goto add_invalid;
            }
    }
    i tried this
    Code:
    ptr =fopen("record.txt","a");
    instead of
    Code:
     ptr=fopen("record.dat","a+");
    ok that works..but what if i need to style how should text show in tabular form ?

    right now everything shows in single line text..



    and what is the use of storing in .dat files?
    Last edited by sash_007; 06-26-2019 at 07:41 PM.

  2. #2
    Registered User
    Join Date
    May 2019
    Posts
    214
    ok that works..but what if i need to style how should text show in tabular form ?

    right now everything shows in single line text..
    Tabular form may well be little more than including tabs in the format (check printf formatting and /t), but the problem of "a single line" may be your operating system convention. Windows usually expects both a carriage return ("\r") and a line feed ("\n"), while linux is fine with just a line feed character.

    and what is the use of storing in .dat files?
    It's just an extension. The extension doesn't actually define a format, there are merely some conventions. ".dat" is one of those which doesn't actually define a format, but indicates it is a data file. It is assumed that ".txt" files contain text, and should, but the use of the extension means nothing about what code produces in that file. Nothing would prevent a ".txt" file from containing binary information except a well behaved program. It also doesn't inform as to the encoding of that text. It could be simple ASCII, or UTF-8, or UTF-16, or anything else which qualifies as text (though, usually, we expect ASCII or no more than UTF-8).

    Most formalized formats, like .jpg, are expected to conform to specific format definitions. For many professional applications which write out data, they choose an extension name to indicate it is a particular type of data file, like ".doc" or ".pdf". As such, ".dat", being so generic and unspecified, is probably best avoided for anything but experimental or student applications.

  3. #3
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    81
    Quote Originally Posted by sash_007 View Post
    recently i have been studying code of bank management system
    Is the code legit? If so, where did you get access to a bank management system's codebase?

  4. #4
    Registered User
    Join Date
    May 2019
    Posts
    47
    codewithc

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    but i just wonder can i store these structure data in plain .txt file ? if so how?
    The easiest way is to use sscanf into a char array, and then fputs - That way you can reuse a lot of that code.

    Your scanf's from the file will also need to change, because you are now reading strings, not numbers.



    However, there are a lot of practices that are bad in the code you are learning from, and a few errors.

    From here

    Using goto instead of using regular loops
    Using exclusively global variables


    And a few errors such as...
    Code:
    else if (choice==2)
        {   printf("Enter the name:");
            scanf("%s",&check.name);
    
    ... Should be ...
    else if (choice==2)
        {   printf("Enter the name:");
            scanf("%s",check.name);    //check.name is already the address, does not need '&'
    strcmpi will only work on older Windows machines... You can write your own using strcmp and toupper/tolower in ctype.h

    'close()' has no prototype

    When dealing with time there is a time.h header file that would be much better than asking the user for the date/time...

    Code:
    while (fscanf(old,"%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d",&add.acc_no,add.name,&add.dob.month,&add.dob.day,&add.dob.year,&add.age,add.address,add.citizenship,&add.phone,add.acc_type,&add.amt,&add.deposit.month,&add.deposit.day,&add.deposit.year)!=EOF)
    This function returns the number of input items successfully matched and assigned, not EOF.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing file data in a structure of format shown
    By Tripswitch in forum C Programming
    Replies: 11
    Last Post: 08-05-2014, 07:28 AM
  2. Replies: 4
    Last Post: 08-25-2013, 05:09 AM
  3. Storing data in file
    By purestr999 in forum C Programming
    Replies: 10
    Last Post: 12-09-2010, 10:00 PM
  4. Replies: 3
    Last Post: 02-26-2008, 02:12 PM
  5. Storing Data into a file
    By Sridar in forum C++ Programming
    Replies: 2
    Last Post: 05-29-2005, 07:03 PM

Tags for this Thread