flat assembler
Message board for the users of flat assembler.
Index
> High Level Languages > c coding laguage |
Author |
|
Tyler 11 Apr 2011, 03:17
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. |
|||
11 Apr 2011, 03:17 |
|
lilsid 11 Apr 2011, 03:28
its c
|
|||
11 Apr 2011, 03:28 |
|
lilsid 11 Apr 2011, 03:35
so this is ion the database basically customer’s ID
customer’s name product ordered and product id balance (whether outstanding or clear) |
|||
11 Apr 2011, 03:35 |
|
lilsid 11 Apr 2011, 03:37
how should i implement an associative array am sadly lost ...and this is just the first part
|
|||
11 Apr 2011, 03:37 |
|
cypher 11 Apr 2011, 03:56
does have to create an abstract data in order to make an associative array?
|
|||
11 Apr 2011, 03:56 |
|
vid 11 Apr 2011, 07:23
use sqlite3
|
|||
11 Apr 2011, 07:23 |
|
Tyler 11 Apr 2011, 07:54
vid wrote: use sqlite3 |
|||
11 Apr 2011, 07:54 |
|
vid 11 Apr 2011, 12:57
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
|
|||
11 Apr 2011, 12:57 |
|
lilsid 11 Apr 2011, 14:21
Thanks guys
Sqlite seems to complicated as i am a beginner. Well I think 3 mb of sqlite's source will be top much. It a small assignment. I just need a database to for storing the information. My problem is that I don't know how to structure the database so that it can be read from. I have an idea to use the same form of something like a script where I can read each line do do some interpreting or something smilar to that. But I need some ideas please. Sorry if I'm asking for too much but it's do friday |
|||
11 Apr 2011, 14:21 |
|
typedef 11 Apr 2011, 19:34
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 |
|||
11 Apr 2011, 19:34 |
|
vid 11 Apr 2011, 20:13
Best approach depends on what exactly you need to do with the file.
|
|||
11 Apr 2011, 20:13 |
|
Enko 11 Apr 2011, 22:12
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^^ |
|||
11 Apr 2011, 22:12 |
|
lilsid 12 Apr 2011, 01:04
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 |
|||
12 Apr 2011, 01:04 |
|
cypher 12 Apr 2011, 01:33
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) |
|||
12 Apr 2011, 01:33 |
|
typedef 12 Apr 2011, 05:11
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; } |
|||
12 Apr 2011, 05:11 |
|
vid 12 Apr 2011, 09:58
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), |
|||
12 Apr 2011, 09:58 |
|
cypher 18 Apr 2011, 17:09
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. |
|||
18 Apr 2011, 17:09 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.