int main() { struct Person p1; strcpy(p1.name, "홍길동"); p1.age = 30; strcpy(p1.address, "서울시 용산구 한남동"); setPerson(&p1); // 함수를 호출할 때 구조체 변수의 메모리 주소를 전달 // setPerson에서 변경한 값이 출력됨 printf("이름: %s\n", p1.name); // 이름: 고길동 printf("나이: %d\n", p1.age); // 나이: 40 printf("주소: %s\n", p1.address); // 주소: 서울시 서초구 반포동 return 0; }
int main() { FILE *fp; FILE *fout; fout=fopen("Assignment1.txt","w"); fp=fopen("a1.txt","r"); int numLines=0, numEntries=0; struct person personList; struct person MyPerson; printf("How many people are in the file?"); scanf("%d",&numEntries); numLines=fillStruct(fp,personList); setPerson(fout,personList); printStructArray(fout,personList,numLines); fclose(fp); fclose(fout); return 0; }
//BOB THE BUILDER // update person void inputscie(int& numOfSci, QSqlDatabase& db) { bool db_ok = db.open(); if(db_ok) { QString full_info = ""; QString inputPers = "INSERT INTO Person (name, sex, year_birth, year_death) VALUES "; QSqlQuery query(db); NumOfSci(numOfSci); for(int i = 0; i < numOfSci; i++){ Person Sci = setPerson(); full_info.append("(\""); full_info.append( QString::fromStdString(Sci.getname())); full_info.append("\", \""); full_info.append( QString::fromStdString(Sci.getsex())); full_info.append("\", "); full_info.append( QString::number(Sci.getbirth())); full_info.append(", "); full_info.append( QString::number(Sci.getdeath())); full_info.append(")"); if(i >= numOfSci-1){ full_info.append(";"); } else full_info.append(", "); cout << endl; } inputPers.append(full_info); //cout << endl << inputPers.toStdString() << endl; bool prepared = query.prepare(inputPers); if(prepared){ query.exec(); } else cout << query.lastError().text().toStdString() << endl; //report error } else cout << "db.open() returned false" << endl; db.close(); }
int main(void) { char listName[] = "./data/list3.txt"; printf("START TEST by loading the list in %s.\n",listName); /* open the file */ FILE *fp; if (!(fp = fopen(listName,"r"))) { fprintf(stderr,"ERROR (%s) cannot open file %s.\n",__func__,listName); exit(EXIT_FAILURE); } /* create the lists */ dbl_list *list = dbl_list_init(sizeof(struct Person)); /* fill it with each line of data */ int ret; size_t line = 0; while (!feof(fp)) { struct Person person; char name[PERSON_NAME_LENGTH]; unsigned int nbvals; /* read the name and number of values (first 2 fields). * NOTE: we do NOT read the '\n' */ ret = fscanf(fp,"%s%u",name,&nbvals); printf("Line: %u contains <%s><%u>\n",line,name,nbvals); if (ret != 2) { fprintf(stderr,"WARNING (%s) Line %u is invalid. ret = %d\n",__func__,line,ret); continue; } if (nbvals > PERSON_VALUES_NBMAX) { fprintf(stderr,"WARNING (%s) Line %u. Only %u values will be read.\n",__func__,line,PERSON_VALUES_NBMAX); nbvals = PERSON_VALUES_NBMAX; } /* dinamically allocate an array of values */ double *values = malloc(sizeof(*values)*nbvals); if (!values) { fprintf(stderr,"ERROR (%s) Failed to allocate memory.\n",__func__,line); exit(EXIT_FAILURE); } /* read each value */ size_t fields = 0; while (fields != nbvals) { ret = fscanf(fp,"%lf",&(values[fields])); if (ret != 1) { fprintf(stderr,"WARNING (%s) Line %u. Could not load value #%d.\n",__func__,line,fields); values[fields] = -99; } fields++; } fscanf(fp,"\n");line++; setPerson(&person,name,nbvals,values); /* add in the list */ if (dbl_list_addNode(list,dbl_node_createCopyContent(list,&person,©Person),LIST_POSITION_LAST)) { fprintf(stderr,"WARNING (%s) Did not manage to add the new node (%s).\n",__func__,"struct Person"); continue; } /* free the local memory (has been copied into the list) */ free(values); } fclose(fp); /* Use the list */ /* sort the list (by number of values) */ printf("BEFORE SORTING: \n"); ret = dbl_list_browse(list,&printPerson); printf("START SORTING: \n"); dbl_list_sort(list,&compareNbValues,©Person,&deletePerson); printf("AFTER SORTING: \n"); ret = dbl_list_browse(list,&printPerson); /* copy the list */ printf("START COPYING: \n"); dbl_list *copy = dbl_list_copy(list,©Person); dbl_list_delete(list,&deletePerson); ret = dbl_list_browse(copy,&printPerson); /* Delete the list */ dbl_list_delete(copy,&deletePerson); exit(EXIT_SUCCESS); }