flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > c coding laguage

Author
Thread Post new topic Reply to topic
lilsid



Joined: 11 Apr 2011
Posts: 10
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.....
Post 11 Apr 2011, 03:08
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
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.
Post 11 Apr 2011, 03:17
View user's profile Send private message Reply with quote
lilsid



Joined: 11 Apr 2011
Posts: 10
lilsid
its c
Post 11 Apr 2011, 03:28
View user's profile Send private message Reply with quote
lilsid



Joined: 11 Apr 2011
Posts: 10
lilsid
so this is ion the database basically customer’s ID
customer’s name
product ordered and product id
balance (whether outstanding or clear)
Post 11 Apr 2011, 03:35
View user's profile Send private message Reply with quote
lilsid



Joined: 11 Apr 2011
Posts: 10
lilsid
how should i implement an associative array am sadly lost ...and this is just the first part
Post 11 Apr 2011, 03:37
View user's profile Send private message Reply with quote
cypher



Joined: 07 Apr 2011
Posts: 51
Location: The cave
cypher
does have to create an abstract data in order to make an associative array?
Post 11 Apr 2011, 03:56
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
use sqlite3
Post 11 Apr 2011, 07:23
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler
vid wrote:
use sqlite3
I'm pretty sure it's an assignment. Wink Maybe he could use sqlite3's code for ideas. Smile
Post 11 Apr 2011, 07:54
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
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 Smile
Post 11 Apr 2011, 12:57
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
lilsid



Joined: 11 Apr 2011
Posts: 10
lilsid
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
Post 11 Apr 2011, 14:21
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
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
Post 11 Apr 2011, 19:34
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Best approach depends on what exactly you need to do with the file.
Post 11 Apr 2011, 20:13
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Enko



Joined: 03 Apr 2007
Posts: 678
Location: Mar del Plata
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^^
Post 11 Apr 2011, 22:12
View user's profile Send private message Reply with quote
lilsid



Joined: 11 Apr 2011
Posts: 10
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
Post 12 Apr 2011, 01:04
View user's profile Send private message Reply with quote
cypher



Joined: 07 Apr 2011
Posts: 51
Location: The cave
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:
Code:
ID, First Name, Second Name, Age, Color     
this is your syntax

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)
Post 12 Apr 2011, 01:33
View user's profile Send private message Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
cypher wrote:

Now when tokenizing, you must look about for whitespaces (i.e. space, tabs)


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;
}



    
Post 12 Apr 2011, 05:11
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
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),
Post 12 Apr 2011, 09:58
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
cypher



Joined: 07 Apr 2011
Posts: 51
Location: The cave
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.
Post 18 Apr 2011, 17:09
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.