Click here to Skip to main content
15,881,719 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can someone tell me what I am doing wrong here?
One of the issues I face w/ my code is that strings are overwriting each other.

C++
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int month, date, year;
int main_exit;
// Note that the structure to be nested has to be declared first.
struct date
{
 int month, date, year
};
struct
{
 char name[60];
 char address[60];
 char citizenship[15];
 double phone;
 float amt;
 char acc_type[10];
 struct date dob;
 struct date deposit;
 struct date withdraw;
 int acc_no;
 int age;

} add, check;
int main()
{
    FILE *fptr;
    fptr = fopen("record_data.txt", "a+");
account_no:
    printf("\n\n\t\tADD RECORD ");
    printf("\n\n\t\tEnter today's date : ");
    scanf("%d/%d/%d", &add.deposit.date, &add.deposit.month, &add.deposit.year);
    printf("\n\n\t\tEnter AccountNumber :  ");
    scanf("%d", &check.acc_no);

 while (fscanf(fptr, " %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.date, &add.dob.year, &add.age, &add.address, &add.citizenship, &add.phone, &add.amt, &add.acc_type) != EOF)
    {
 if (check.acc_no == add.acc_no)
        {
            printf("Account no. already in use!");
 goto account_no;
        }
    }
    add.acc_no = check.acc_no;
    printf("\n\n\t\tEnter the name : ");
    fgets(add.name,sizeof(add.name) , stdin);

    printf("\n\n\t\tEnter the age : ");
    scanf("%d", &add.age);

    printf("\n\n\t\tEnter address : ");
    scanf("%s", add.address);

    printf("\n\n\t\tEnter citizenship number : ");
    scanf("%s", &add.citizenship);

    printf("\n\n\t\tEnter phonenumber ; ");
    scanf("%lf", &add.phone);

    printf("\n\n\t\tEnter the amount to deposit : ");
    scanf("%f", &add.amt);

    printf("\n\n\t\tType of account : ");
    scanf("%s", &add.acc_type);

    fprintf(fptr, "%d %s %d/%d/%d %d %s %s %lf %s %f %d/%d/%d", add.acc_no, add.name, add.age, add.address, add.citizenship, add.phone, add.amt, add.acc_type);
    fclose(fptr);
    printf("Account created successfully!");


What I have tried:

I don't know why but my strings are overwriting each other because of that I am not able to run other part of my program.

Also , lemme know if u see errors related to file handling.
Posted
Updated 12-Apr-22 17:43pm
v3
Comments
Shao Voon Wong 13-Apr-22 1:48am    
You need to call fflush(fptr); before fclose(fptr);
Rajeev Jayaram 13-Apr-22 5:01am    
Explain the issue in title, don't just say, "What am I doing wrong?" etc.

1 solution

This should help: C library function - fopen()[^]

I've moved away from C years ago .. but for "a+" you may need to read/seek to end of file before appending... change to "a" and it should fix it.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900