I'm new in C language (my 6th day learning C), and this is my 2nd question in here, so I'm trying to save the inputed data from my program to a .csv file using command fwrite below (see the code). But when i opened the csv file, my data only saved to the first cell and its kinda repetitive ( You can see it in the pic below ).
I'm trying to each data to be saved into one cell Like this :
Samuel
Jakarta
13/01/2011
0185-13553131
How can i do it? Thanks For your help :3
int main () {
struct identitas arr_identitas[max];
struct identitas*ptr;
int x,i,choice,f,pilih,a=0,b,r,y,c,d;
char apusNama [20];
char apusAlamat [20];
char newnama [20];
char newalamat[20];
char newtgl_lahir[20];
char newno_tlp[20];
char nullStr[20] = {"\0"};
FILE *outfile, *infile; //initiation outfile (to write) infile (to open)
outfile = fopen ("Tugas2-5.csv","w"); //name of file, w = write
if (outfile == NULL){
fprintf(stderr, "\nError opening file\n\n"); //if file not found
exit(0);
}
infile = fopen ("Tugas2-5.csv","r+"); //name of file, r = read
if (infile == NULL){
fprintf(stderr, "\nError opening file\n\n"); //if file not found or empty
exit(0);
}
while (1) {
printf ("\n \t -*-*-*-*-* WELCOME TO MENU -*-*-*-*-* \t \n"); // MAIN MENU
printf("\n");
printf ("1. Memasukkan/Menambahkan Data \n"); // 1. INPUT INITIAL DATA
printf("2. Menampilkan data \n"); // 2. SHOWING DATA (ALL)
printf("3. Menampilkan 1 data \n"); // 3. SHOWING DATA (1/SINGLE)
printf("4. Menghapus data \n"); // 4. DELETING DATA
printf ("5. Menginput tambahan \n"); //5. INPUTING ADDITIONAL DATA
printf ("6. Mengedit data \n"); // 6. EDIT EXISTING DATA
printf("7. Sorting\n"); // 7. SORTING DATA
printf ("8. Read Data\n");
printf("9. Exit\n\n\n"); // 8. EXITING DATA
printf("Enter your choice : "); scanf("%d",&choice);
switch (choice)
{
case 1: // ==> Inputing data from user
printf ("Masukkan Jumlah Data : "); scanf("%d", &x); // Input max number of data
ptr = calloc (x, sizeof (arr_identitas[max])) ;
if (ptr == 0) {
printf("Memory not allocated.\n"); //calloc successfukk
exit(0);
}
else {
printf("Memory array successfully allocated \n");
}
while (i < x ) {
printf ("Data ke = %d \n ",i+1); //data n input
bool err=true;
bool frr=true;
int counter = 0;
while (counter == 0) {
fflush(stdin);
printf ("Masukkan nama anda : "); gets((ptr+i)->nama); //input name
if ( strlen ((ptr+i)->nama ) == 0 ) {
printf("# ERROR,TERMINATE PROGRAM # \n"); //if name is empty terminate program
exit(0);
}
else {
counter = 1;
}
}
printf ("Masukkan alamat anda : "); gets((ptr+i)->alamat); //input address
printf ("Masukkan kode telp anda (xxxx-xxxxxxxxx) : "); gets ((ptr+i)->nomor_telepon); //input phone number
while(err==true){
if((ptr+i)->nomor_telepon[3]!='-'&&(ptr+i)->nomor_telepon[4]!='-'){
printf("EROR COBA LAGI.\n");
printf("Masukkan kode telp anda (xxxx-xxxxxxxxx): "); gets((ptr+i)->nomor_telepon);
}
else err=false;
}
printf ("Masukkan tanggal lahir anda (dd/mm/yyyy) : "); gets ((ptr+i)->tanggal_lahir); //input date of birth
while(frr==true){
if((ptr+i)->tanggal_lahir[2]!='/'||(ptr+i)->tanggal_lahir[5]!='/'){
printf("ERROR COBA LAGI.\n");
printf("Masukkan tanggal lahir anda (dd/mm/yyyy) : \n\n"); gets((ptr+i)->tanggal_lahir);
}
else frr=false;
}
b = i;
c = i;
i++;
counter = 0;
fwrite (ptr, sizeof(struct identitas), x, outfile); //writing data to outfile
}
break;