Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How i will contact 2 a server

realloc

  • Please log in to reply
1 reply to this topic

#1 pc_magas

pc_magas

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 07 January 2010 - 06:30 AM

Dear coleagues for a educational project I had to create a code that creates a dicitonary:
/*Please compile me in Linux*/
/*kali hronia*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 10
#define Z 20
void find(int);/*Function eyresis stoixeiou dexetai thn teleutaia thesi tou pinaka lummatwn*/
void insect(char [],char []);/*Function eisagwgis stoixeiwn kai epistefei to telos kathe pinaka lymmatwn leksi (vlepetai parakatw)*/
void getstring(char *string,int buffer);/*Function eisagwgis frasis logv oti h gets pithanws na kanei overflow kai h scanf eisagei dedomena mono mehri na synantisei keno h pathsei o hristis enter*/
int checkifnull();/*Eleghoume an oi DYNAMIKOI pinakes apo pointers pou the kanoume parakatw einai "adeioi"*/
char **leksi=NULL,**epeksigisi=NULL;/*dio pointers gia metatropi se dynamikous pinakes apo pointers GIA NA MHN EHOUME RPOBLEMS TIS ARHIKOPOIOUME ME THN TIMH NULL dhladi den deihnei kapou*/
char menu();/*Funtion menou epilogis*/
char menu(){
char option;
do{
        printf("Welcome! the following menu is based on ncurses\n");
        printf("What do you want?\n");
        printf("I)nsect\n");
        printf("S)earch\n");
	printf("Please type the letter of the name of funtion you want which is before ')'\n");
        scanf("%s",&option); getchar();//H getchar pairnei ta skoupidia tou buffer
 if(option!='I'&& option!='S'){printf("Wrong option you should re-enter your option\0");}
   }while(option!='I'&& option!='S');
}
void getstring(char *string,int buffer){
int i=0;
fgets ( string, buffer, stdin );
for ( i = 0; i < buffer; i++ ){
if ( string[i] == '\n' ){
string[i] = '\0';
break;
}
}
}

void insect(char lymma[N],char orismos[Z]){
printf("Dwste to lymma:\n");
getstring(lymma,15);
printf("Now etner the definition of %s :\n",lymma);
getstring(orismos,200);
}
void find(int size){
int i,done;//Metavliti gia prospelasi tou pinaka
char lymma[N];
printf("Dwste to lumma pou theletai na anazitisetai:\n");
scanf("%s",lymma);//Eisagwgi stoixeiou pros anazitisi
getchar();//getchar "skoupidiaris" tou buffer
for(i=0;i<size;i++){//Loop anazitisis
        if(strcmp(lymma,leksi[i])==0){
            printf("The word found:\n");
            printf("%s: %s",leksi[i],epeksigisi[i]);
            done=1;
            break;//an to lymma eurethei stamatv tin anazitisi
	}
        else{done=0;}
	
    }
    if(done==0){printf("Den yparhei ayto to lymma\n");}
}
int checkifnull(){
if(leksi==NULL&&epeksigisi==NULL)
{return 1;}
else {return 0;}
}

int main(void){
    char opt,endinsect='y',error;/*opt metavliti epilogis eksodou endinsect metavliti epilogis syneheias eisodou error metavliti eleghou an einai adeioi oi pinakes (vlepetai parakatw)*/
int i=0,size;
do{
if(opt=='y'){i=0;}//se kathe loop mhdenizw to loop
	do{
        switch(menu()){

            case 'I': 
		while(endinsect=='y'||endinsect=='Y')
		{
		leksi=(char **)realloc(leksi,(i+1)*sizeof(char *));
		epeksigisi=(char **)realloc(epeksigisi,(i+1)*sizeof(char *));
		leksi[i]=(char*)malloc(N*sizeof(char));
		epeksigisi[i]=(char*)malloc(Z*sizeof(char));
		insect(leksi[i],epeksigisi[i]);	
		i++;
		size=i;/*Apotikeuw to megethos tou pinaka to i tha auksithei kata 1 xaris thn prohgoumenh entoli*/       
		printf("Continue of insecting lymmata Y/N?\n");
		scanf("%c",&endinsect); getchar();/*O hristis epilei to telos h getchar pairnei oti skoupidi apo ton buffer*/
		}
	     
		break;
            case 'S':
                    if(checkifnull()){//Gia anazitisi den prepi oi pinakes apo pointers na einai adeioi
                        printf("Den yparhoun lymmata the preepi na dwsetai ta lymmata\n");
			printf("Going back to menu:\n");
			error='y';
			break;
      			}
                     find(size);
			error='n';
                     break;
		        }
	}while(error=='y');		
		do{
			printf("\nContinue to run Y/N?");
			scanf("%c",&opt); getchar();
		}while(opt!='Y'&&opt!='y'&&opt!='n'&&opt!='N');
}while(opt=='y'||opt=='Y');
return 0;
}
And I thoght that it was a good idea to be able to be connected with a mysql databaze in order to store and search data so it wouldn't be nessasary to store in table that it would "live" while the program runs.
Do u have an idea how the program will communicate with sql databaze?

eg.Sorry the explanations in code are in greeklish.
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 07 January 2010 - 06:35 AM

MySQL :: MySQL 5.0 Reference Manual :: 20.8 MySQL C API
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download