Hello everyone,
So i was just going by my old college a few days back to meet some of my close professors,when i noticed one of them logging student data on an excel sheet.
Being a inquisitive c learneri asked him to contact the it/compt dept and make them write a code to make logging easier.
He got back to me today informing that they were too busy at the moment,so i offered to do it insteadand need some of your expertise suggestions.
I have already written a first draft and have come across a few difficulties.
The program works fine but i still want to optimize and add a few features.I would be glad to hear your useful tips and suggestions.![]()
Here's the code:
Output(example):Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <process.h> struct mydata { char name[100]; int age,hight; }; int main() { struct mydata bo; char *ptr; char buffer[10000] = "\0",ch[100000] = "\0",option,fname[100] = "\0",name[10000] = "\0",replace_data[10000] = "\0"; char header[100] = "Name,Age,Hight\n"; int num = 0; char flag=0; printf("MENU:\n1.Add entry\n2.Edit Entry\n3.Exit\n"); option = getch(); char submenu1 = 13; memset(bo.name,0,100); bo.age = 0; bo.hight = 0; FILE *fs; fflush(stdin); if(option == '1') { printf("Enter file name."); scanf("%s",&fname); fname[100] = strcat(fname,".csv"); system("cls"); while(submenu1 == 13) { fs = fopen(fname,"r"); if(fs != NULL) { flag=1; } fclose(fs); fs=fopen(fname,"a+"); if(fs == NULL) { printf("Error opening file."); exit(0); } if(flag==0) { fprintf(fs,"%s",header); } fflush(stdin); printf("Enter the name.\n"); gets(bo.name); fflush(stdin); printf("Enter the age.\n"); scanf("%d",&bo.age); fflush(stdin); printf("Enter the hight.\n"); scanf("%d",&bo.hight); fprintf(fs,"%s,%d,%d\n",bo.name,bo.age,bo.hight); fclose(fs); printf("Add another entry.(Hit enter to keep on adding)\n"); submenu1 = getch(); fflush(stdin); if(submenu1 != 13) { exit(0); } } } fflush(stdin); if(option == '2') { system("cls"); printf("Enter file name."); scanf("%s",&fname); strcat(fname,".csv"); FILE *fs,*fp; fs = fopen(fname,"r"); if(fs == NULL) { printf("Could not open file:Read operation failed.\n"); system("pause"); } else { printf("Enter name of entry to edit."); scanf("%s",&name); fflush(stdin); printf("Enter data to edit(name,age,hight)"); scanf("%s",&replace_data); FILE *fs,*fp; fs = fopen(fname,"r"); if(fs == NULL) { printf("Could not open file:Read operation failed."); } fread(ch,1000,1,fs); fclose(fs); ptr = strstr(ch,name); if(!ptr) { printf("\n\nName not found.\nProgram Exiting."); exit(1); } while(*(ptr+num) != '\n') { num++; } strcpy(buffer,(ptr+num)); strcpy(ptr,replace_data); strcat(ch,buffer); printf("\n%s\n",ch); fp = fopen(fname,"w"); if(fp == NULL) { printf("Could not open file:Write operation failed."); } fprintf(fp,"%s",ch); fclose(fp); /*printf("Do you want to view the excel sheet?") if(yes) { open excell/notepad sheet. spawn(somthing)...dont know how?? }*/ } } fflush(stdin); if(option == '3') { exit(0); } return 0; }
Name,Age,Hight
John,19,170
Nickel,21,160
.
.
.
//etc



2Likes
LinkBack URL
About LinkBacks
i asked him to contact the it/compt dept and make them write a code to make logging easier.
and need some of your expertise suggestions.



apologies,will use fgets from now on)