i have defined an array of struct in c by the following struct a { char t[100]; char r[100]; char s[100]; } struct a my[100]; the problem is that i want more than 100 array for my struct, but compile error comes when i define a higher value than 100 in my[]. so wat will i do in this case to increase my capacity to atleast 500. plzz help
struct sample { char t[500], r[500], s[500]; }; Code (markup): struct sample firstSample; Code (markup): You can verify the capacity with sizeof.
Your array of 100 elements occupy only 300 * 100 = 30k memory. It is very small. What compile eror you are getting when you increase? Even your system does not support such small memory, your computer should not report it as error. It is not a compile error, it is a runtime error even that.
thnx huys for the reply.. it is in fact a runtime error as i have no probs in running the code but after a size of 100 the output is junk values. so only upto 100 can be used. currently wat i am doin is tht after 100 arrays are full i am writing the current data to a file and then use the same hundred again for the next set of data. will this create any probs?
Lesson 101: Never ever do this in C/C++. Don't declare arrays with a fixed size. Use new instead and make sure you delete at the end.
#include <stdio.h> #include <string.h> #include <stdlib.h> struct data { char *t; char *r; char *s; int x; } data; int main(void) { int structsize = 100; int i, limit = 0; struct data **my; my = (struct data **) malloc((sizeof(struct data) * (structsize + 1)) ); if(my == NULL) exit(2); //unable to malloc for(i=0;i<1;i++) { my[i] = (struct data *) malloc(sizeof(struct data)); if(my == NULL) exit(2); //unable to malloc } i = 0; limit = 15; my[i]->t = strndup("whatever blah blah blasdfasjdfaskdfj askldfj asdjklf sdfljk asdklfjasklfah blah blah ttttttttttttttttttttttttttttttttttttttttttttttt", limit); my[i]->r = strndup("whatever blah b jaskldfjlaskdfj sdlfkj asdlfjk askldfj lah blah blah blah", limit); my[i]->s = strndup("asdfjasdklf asldkfj asdklfjaskld faskldf askldjf askldjf askldfj whatever blah blah blah blah blah", limit); printf("%s\n%s\n%s\n", my[i]->t, my[i]->r, my[i]->s); //clean up for(i=0;i<(structsize+1);i++) free(my[i]); free(my); return 0; } Code (markup):
In fact,the size of your array is not large in now days. but c compiler will report error if size of array is too large. may your compiler env is a bit special,such as an ancient compiler? the alternative is to malloc it dynamicly instead of declaring it with fixed size.
you should use a linked list instead check out this: http://www.cprogramming.com/tutorial/lesson15.html you can dynamically add elements at runtime
#include<stdio.h> #include<conio.h> #include<string.h> #define LENGTH 100 struct rest { char catid[100]; char name[100]; char desc[200]; char price[100]; char type[100]; } main() { int i,j,k,f,p,q,s,t,u,w,z,cnt,cnt1,cnt2,cnt3,cnt4; char uid[10]; FILE *fp,*fp1; char arr[100],filename[50]; struct rest r[100],b[100],c[100],d[100],e[100]; clrscr(); printf("enter the user id \n"); gets(uid); puts(uid); printf("Enter Source Filename : "); scanf("%s",filename); fp=fopen(filename,"r"); if(fp == NULL) { printf("File %s Doesn't Exist!!!",filename); printf("\nTHANK YOU!!!!\n"); getch(); exit(0); } printf("Enter dest Filename : "); scanf("%s",filename); fp1=fopen(filename,"w"); if(fp1 == NULL) { printf("File %s Doesn't Exist!!!",filename); printf("\nTHANK YOU!!!!\n"); getch(); exit(0); } fprintf(fp1,"insert into menu (user_id,cat_id,item_name,item_desc,price,item_type) values"); k=0; while(fgets(arr,LENGTH,fp)!=NULL) { for(i=0;i<100;i++) { r[k].catid=0; r[k].name=0; r[k].price=0; r[k].type=0; } for(i=0;i<200;i++) { r[k].desc=0; } cnt=0; for(i=0;arr!=NULL;i++) { cnt=cnt+1; if(arr==',') { for(j=0;j<cnt-1;j++) { r[k].catid[j]=arr[j]; } break; } } cnt1=cnt; p=0; for(i=cnt1;arr!=0;i++) { cnt=cnt+1; if(arr==',') { for(j=cnt1;j<cnt-1;j++) { r[k].name[p]=arr[j]; p=p+1; } break; } } cnt2=cnt; q=0; for(i=cnt2;arr!=NULL;i++) { cnt=cnt+1; if(arr==',') { for(j=cnt2;j<cnt-1;j++) { r[k].desc[q]=arr[j]; printf("%c",r[k].desc[q]); q=q+1; } break; } } cnt3=cnt; s=0; for(i=cnt3;arr!=NULL;i++) { cnt=cnt+1; if(arr==',') { for(j=cnt3;j<cnt-1;j++) { r[k].price=arr[j]; s=s+1; } break; } } cnt4=cnt; t=0; for(i=cnt4;arr!=NULL;i++) { cnt=cnt+1; if(arr==',') { for(j=cnt4;j<cnt-1;j++) { r[k].type[t]=arr[j]; //printf("%c",r[k].type[t]); t=t+1; } break; } } k=k+1; if(k==100) { for(i=0;i<100;i++) { fputs("('",fp1),fputs(uid,fp1),fputs("','",fp1),fputs(r.catid,fp1),fputs("','",fp1),fputs(r.name,fp1),fputs("','",fp1),fputs(r.desc,fp1),fputs("','",fp1),fputs(r.price,fp1),fputs("','",fp1),fputs(r[i].type,fp1),fputs("'),",fp1); } k=0; } } for(i=0;i<k;i++) { fputs("('",fp1),fputs(uid,fp1),fputs("','",fp1),fputs(r[i].catid,fp1),fputs("','",fp1),fputs(r[i].name,fp1),fputs("','",fp1),fputs(r[i].desc,fp1),fputs("','",fp1),fputs(r[i].price,fp1),fputs("','",fp1),fputs(r[i].type,fp1),fputs("'),",fp1); } fclose(fp); fclose(fp1); getch(); } this is the entire program.. wat i am tryin to do is accept a csv file with a bunch of values and then generate a sql query to run on my database. at present it is workin.. problem arises when the 'desc' field is quite large and contains more than 50-60 characters. in that case the qery genrated has a prob.. can u guys tel me y and how to solve this. thnx for all ur replys...[/i][/i][/i][/i][/i][/i]
What type of database? BTW This could be done much easier with a simple php command line script: <?php $csv = "somefile.csv"; //input $sql_file = "somefile.sql"; //output $csv_file = file_get_contents($csv); $rows = explode('\n',$csv_file); $sql = ""; foreach($rows as $row){ $items = explode(',',$row); $sql = $sql."insert into menu (user_id,cat_id,item_name,item_desc,price,item_type) values (". $item[0].",". $item[1]",'". $item[2]."',". $item[3].",". $item[4].",". $item[5].");\n"; } if($fh = fopen($sql_file,"w")){ fwrite($fh,$sql); fclose($fh); } ?> Code (markup):
unfortunately there is no explode in c.. but thnx anyways.. will try it in php and let u know. thnx mate
look for my example several post above that use strndup and malloc, it should give you quite good example how to use and take advantage of dynamic memory in C.
You can use dynamic memory in c. or run this code. it works properly at my computer.it may help you............ #include<stdio.h> #include<conio.h> #include<string.h> #define LENGTH 100 #include<stdlib.h> struct rest { char catid[100]; char name[100]; char desc[200]; char price[100]; char type[100]; }; int main() { int i,j,k,f,p,q,s,t,u,w,z,cnt,cnt1,cnt2,cnt3,cnt4; char uid[10]; FILE *fp,*fp1; char arr[100],filename[50]; struct rest r[100],b[100],c[100],d[100],e[100]; clrscr(); printf("enter the user id \n"); gets(uid); puts(uid); printf("Enter Source Filename : "); scanf("%s",filename); fp=fopen(filename,"r"); if(fp == NULL) { printf("File %s Doesn't Exist!!!",filename); printf("\nTHANK YOU!!!!\n"); getch(); exit(0); } printf("Enter dest Filename : "); scanf("%s",filename); fp1=fopen(filename,"w"); if(fp1 == NULL) { printf("File %s Doesn't Exist!!!",filename); printf("\nTHANK YOU!!!!\n"); getch(); exit(0); } fprintf(fp1,"insert into menu (user_id,cat_id,item_name,item_desc,price,item_type) values"); k=0; while(fgets(arr,LENGTH,fp)!=NULL) { for(i=0;i<100;i++) { r[k].catid=0; r[k].name=0; r[k].price=0; r[k].type=0; } for(i=0;i<200;i++) { r[k].desc=0; } cnt=0; for(i=0;arr!=NULL;i++) { cnt=cnt+1; if(arr==',') { for(j=0;j<cnt-1;j++) { r[k].catid[j]=arr[j]; } break; } } cnt1=cnt; p=0; for(i=cnt1;arr!=0;i++) { cnt=cnt+1; if(arr==',') { for(j=cnt1;j<cnt-1;j++) { r[k].name[p]=arr[j]; p=p+1; } break; } } cnt2=cnt; q=0; for(i=cnt2;arr!=NULL;i++) { cnt=cnt+1; if(arr==',') { for(j=cnt2;j<cnt-1;j++) { r[k].desc[q]=arr[j]; printf("%c",r[k].desc[q]); q=q+1; } break; } } cnt3=cnt; s=0; for(i=cnt3;arr!=NULL;i++) { cnt=cnt+1; if(arr==',') { for(j=cnt3;j<cnt-1;j++) { r[k].price=arr[j]; s=s+1; } break; } } cnt4=cnt; t=0; for(i=cnt4;arr!=NULL;i++) { cnt=cnt+1; if(arr==',') { for(j=cnt4;j<cnt-1;j++) { r[k].type[t]=arr[j]; //printf("%c",r[k].type[t]); t=t+1; } break; } } k=k+1; if(k==100) { for(i=0;i<100;i++) { fputs("('",fp1),fputs(uid,fp1),fputs("','",fp1),fputs(r.catid,fp1),fputs("','",fp1),fputs(r.name,fp1),fputs("','",fp1),fputs(r.desc,fp1),fputs("','",fp1),fputs(r.price,fp1),fputs("','",fp1),fputs(r[i].type,fp1),fputs("'),",fp1); } k=0; } } for(i=0;i<k;i++) { fputs("('",fp1),fputs(uid,fp1),fputs("','",fp1),fputs(r[i].catid,fp1),fputs("','",fp1),fputs(r[i].name,fp1),fputs("','",fp1),fputs(r[i].desc,fp1),fputs("','",fp1),fputs(r[i].price,fp1),fputs("','",fp1),fputs(r[i].type,fp1),fputs("'),",fp1); } fclose(fp); fclose(fp1); getch(); return 0; }[/i][/i][/i][/i][/i][/i]