flat assembler
Message board for the users of flat assembler.
![]() |
Author |
|
lilsid
i need help in making a simple database in c language for a program that read the file then writes to it and more but please help me with this part for now.....
|
|||
![]() |
|
Tyler
Do you have an option of C or C++? C++ would be so much easier.
Assuming you must use C: First, you need to implement an associative array in C. After that, make a function to read a file into a map, and another to write a map to a file. That should get you to the point at which you said you want to be. |
|||
![]() |
|
lilsid
its c
|
|||
![]() |
|
lilsid
so this is ion the database basically customer’s ID
customer’s name product ordered and product id balance (whether outstanding or clear) |
|||
![]() |
|
lilsid
how should i implement an associative array am sadly lost ...and this is just the first part
|
|||
![]() |
|
cypher
does have to create an abstract data in order to make an associative array?
|
|||
![]() |
|
vid
use sqlite3
|
|||
![]() |
|
Tyler
vid wrote: use sqlite3 ![]() ![]() |
|||
![]() |
|
vid
He still can paste 3MB of sqlite sources to his test.c and search-replace "sqlite3" with "MyGreatDB" or something. I'm pretty sure no one will read through it
![]() |
|||
![]() |
|
typedef
Use string tokens.
You can store data in files like this. LEGEND: @N : Name @ID: Id @B : Balance @N@typedef@ID@67@B@3 @N@lilsid@ID@60@B@9 now in your code Code: const char* delimiters = "@N@ID@B@" FILE *file = fopen("data.dat","r"); char buff[1024]; fread(&buff,0,sizeof(buff),file); char *token = strtok(buff,delimiters); whille(token != 0) { token = strtok (NULL,delimiters); // do what you want with the names, ID and Balance here..... } free(&token); // That should get you going |
|||
![]() |
|
vid
Best approach depends on what exactly you need to do with the file.
|
|||
![]() |
|
Enko
As it seems for me, you can use *.txt files.
For each line you can store a database row. similar to excel CVS FILE (coma separated file) Code: **** my database.txt ID, First Name, Second Name, Age, Color 1,John, Smith, 32, Blue 2,Martha, James, 45, Red 3,David,,45,Green //No Last name To search in the database you can use linked list (if you alredy learned the use of pointers) Or you can use a huge array or a dinamic array. This way you read a line of text, separate the content of the line, the ´,´ or ´;´ could be the separator. And the database, by the way, will be compatible with the cvs file of excel or any datasheet program^^ |
|||
![]() |
|
lilsid
thanks guys this helps alot so i use the text files and store the balance etc .now i read to the file then get the datat then print
customer id cutomer name balance payment and do u think i should make a data base to input items i sell or just leave it as it is and forget the ordering part since its like a business where people order from and am basically making a databease to hold customers inforation would that be irrelevant .and thnks again |
|||
![]() |
|
cypher
as Enko suggested, you can use his syntax and collect each line for the customerID, customer name, balance, and payment.
What must happen now is you must tokenize the line and collect each item. Quote:
when you read the file, just tokenize each item. the comma (',') is your delimiter, and it is used to separate each item. Now when tokenizing, you must look about for whitespaces (i.e. space, tabs) |
|||
![]() |
|
typedef
cypher wrote:
No need to look for spaces bro... Here try my code and see how sexy it is make a file called data.txt and put this in it @N@typedef@ID@00000067@B@34.8 @N@lilsid@ID@00000020@B@96.7 @N@james@ID@00000080@B@-94.24 @N@Jake@ID@00000070@B@978.35 @N@Jill@ID@00000020@B@9567.5 @N@Jack@ID@00000450@B@-97.00 Code: #include <windows.h> //blah !!!!!!!.... I just happened to have been using it #include <stdio.h> /** Check if the string is full of integers or char **/ int isName(char* string) { int len = strlen(string); for(int i=0;i<len;i++) { if( string[i]>='A' || string[i]>='a' && string[i]<='Z' || string[i]>='z' ){ return 1; }else { return 0; } } } int isID( char* s_id) { if( strlen(s_id) == 8 ){return 1; }else{return 0;} } int main(int argc, char *argv[]) { FILE *file; file = fopen("data.txt","r"); char buff[1024]; fread(&buff,1,sizeof(buff),file); char* tok = strtok(buff,"@N@ID@B@"); while(tok!=0){ if(isName(tok) ) { printf("Name: %s\t",tok); }else if (isID(tok)){ printf("ID: %d\t",atoi(tok)); }else{ printf(" Balance : $%g",strtod(tok,NULL)); if( strtod(tok,NULL)<0 ) { printf("\t(Overdrawn!)\n"); } else { printf("\n"); } } tok = strtok(NULL,"@N@ID@B@"); } printf("\n=================\n"); fclose(file); free(&buff); return 0; } |
|||
![]() |
|
vid
typedef: using fscanf() might be easier than your method. That's what it is for: "formatted input"
eg. format of file can be same as Enko suggested, and reading one line from file would be: Code: int id, age; char first_name[50], second_name[50], color[50]; if (5 != fscanf(" %d , %s , %s , %d , %s", &id, first_name, second_name, &age, color)) { // error } This way is not perfect, more checks should be done, you can't include spaces in text fields, each field must have value, spaces and EOLs in input file are treated the same (it is not strictly one line per one entry), but it is good enough for school (probably this is exactly what they expect you to do), |
|||
![]() |
|
cypher
quick question:
How do I use the feof function with the fscanf function. For exmaple: Code: while (!feof(ptr)) { fscanf(" %d , %s , %s , %d , %s", &id, first_name, second_name, &age, color)) } Am I using it correctly? Please give me some examples. |
|||
![]() |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.