Thread: database

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    82

    database

    Hi, I am new to this site and I have a question which has been really disturbing me:
    When creating database using file I\O, is it important to dynamically store the data?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    depends on what you mean by "dynamically"
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Quote Originally Posted by Harith View Post
    Hi, I am new to this site and I have a question which has been really disturbing me:
    When creating database using file I\O, is it important to dynamically store the data?
    Agreed. This question is disturbing in that it is grammatically correct, but makes no sense whatsoever.
    Are you creating a Flat file database - Wikipedia, the free encyclopedia ? What exactly are you trying to do?
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Sorry guys,
    I am creating a database that stores information in a file; extension.dat; and when the user wants to see the data it again reads from the file. Now the data is inside a structure. Maybe this code will make things clear:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct marks
    {
           char name[200];
           int  number;
    };
    typedef     struct marks    marks;
    
    char  fname[100];
    marks std;
    
    void enter(char prompt[])
    {
           printf("%s",prompt);
           
           fflush(stdin);
           getchar();
    }
    
    void quit_prog()
    {
          printf("Thanks for using the database\n");
          enter("Press enter to exit");
    }
    
    void input()
    {
           printf("Name: ");
           fflush(stdin);
           scanf ("%[^\n]",std.name);
           
           printf("Phone No: ");
           fflush(stdin);
           scanf ("%d",&std.number);
    }
    
    void output()
    {
            printf("\nName: %s\n",std.name);
            printf("Number: %d\n",std.number);
    }   
    
    int file_exist()
    {
           FILE *fp;
           
           fp = fopen(fname,"rb");
           if(fp == NULL)
                 return 0;
                 
           return 1;
    }
    
    int yes_no(char prompt[])
    {
           char      answer;
           
           for(;;)
           {
                printf("%s",prompt);
                scanf ("%c",&answer);
                
                answer = toupper(answer);
                if(answer == 'Y')
                {
                    return 1;
                    break;
                }
                if(answer == 'N')
                {
                    return 0;
                    break;
                }
           }
    }           
    
    int main()
    {
          atexit(quit_prog);
          
          for(;;)
             menu();
          
          return 0;
    }     
    
    menu()
    {
          int       choice;
          for(;;)
          {
                 printf("Welcome. Please enter the number of your choice:\n"
                 "      1. Read contacts from existing file\n"
                 "      2. Add conatct(s) to a new file\n"
                 "      3. Add contact(s) in existing file\n"
                 "      4. Exit\n" );
                 fflush(stdin);
                 scanf ("%d",&choice);
                 
                 if (choice == 1)
                 {
                    file_read();
                    break;
                 }
                 else if (choice == 2)
                 {
                    file_write();
                    break;
                 }
                 else if (choice == 3)
                 {
                    file_append();
                    break;
                 }
                 else if (choice == 4)
                 {
                    exit(0);
                    break;
                 }
          }
    }
    
    file_read()
    {
          FILE   *fptr;     
          
          printf("Please enter the filename: ");
          scanf ("%s",fname);
               
          fptr = fopen(fname,"rb");
          if(fptr == NULL)
          {
                  perror("Could not find the file");
                  return;
          }        
               
          while(fread(&std, sizeof std, 1, fptr) == 1)
          {
                  output();
          }
          
          fclose(fptr);
          enter("File read successfully. Press enter to continue...\n");
    }
    
    file_write()
    {
          int   count,i;
          FILE   *fptr; 
          
          printf("Please enter the filename: ");
          scanf ("%s",fname);
          
          if(file_exist() && !yes_no("File exits. Would you like to overwrite it? "))
                          return;
          
          fptr = fopen(fname,"wb");
          if(fptr == NULL)
          {
                  perror("Could not write to file");
                  return;
          }   
          
          printf("How many numbers would you like to store? ");
          scanf ("%d",&count);
          
          for(i = 0; i < count; ++i)
          {
                input();
                
                fwrite(&std, sizeof std, 1, fptr);
          }
          
          fclose(fptr);
          enter("File written successfully. Press enter to continue...\n");
    }
    
    file_append()
    {
          int   count,i;
          FILE  *fptr;
          
          printf("Please enter the filename: ");
          scanf ("%s",fname);
          
          fptr = fopen(fname,"ab");
          if(fptr == NULL)
          {
                  perror("Could not find the file");
                  return;
          }   
          
          printf("How many numbers would you like to store? ");
          scanf ("%d",&count);
          
          for(i = 0; i < count; ++i)
          {
                input();
                
                fwrite(&std, sizeof std, 1, fptr);
          }
          
          fclose(fptr);
          enter("File written successfully. Press enter to continue...\n");
    }
    Do I have to store the structure dynamically, or make it array? If yes, then why?
    Last edited by Harith; 09-02-2013 at 11:39 PM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    First thing I see - FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    I do not see a reason for using global variables...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    since it is impossible to know the size of the file at compile time, you will definitely need to use dynamic (malloc/free) memory allocation. there is no way around this.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by Elkvis View Post
    since it is impossible to know the size of the file at compile time, you will definitely need to use dynamic (malloc/free) memory allocation. there is no way around this.
    Actually the code works. And I also found the reason; if you want to refer back to the database and you want to specify which number you want, like say you want number 5 of array in the database then you can get that. So in short, in the database the structure will also be stored as array.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    if you're only grabbing one record from the file at a time, and you know the size of the record, then a non-dynamic solution will work just fine.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Ya, but you can't jump from one record to another.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Harith View Post
    Actually the code works. And I also found the reason; if you want to refer back to the database and you want to specify which number you want, like say you want number 5 of array in the database then you can get that. So in short, in the database the structure will also be stored as array.
    Actually, it doesn't work. Or maybe it does, but by sheer dumb luck:
    Code:
    fflush(stdin);
    That is wrong. If you bothered to read and understand the link vart provided in post #5, you would realize that it results in undefined behavior. That means your program can do anything. It could print "Hello world" the first time you run it, and reformat your hard drive the next, and it would be correct in doing so. Realistically, it probably does nothing. If it actually flushes the input buffer, you're probably using Turbo C, which you should throw away and replace with a modern compiler that doesn't suck, e.g. Code::Blocks with MinGW or Pelles C. Either way, remove all those lines, since with them in there, your program doesn't really work. If you want a working alternative, see here: http://faq.cprogramming.com/cgi-bin/...&id=1043284392.

    Quote Originally Posted by Harith View Post
    Ya, but you can't jump from one record to another.
    Umm...fseek? That is, assuming you have fixed record sizes and you are using fread/fwrite, which it seems you are. It also requires knowing the offset in the file, which, depending on how you store the records, may not be possible, without also storing some index information somewhere.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by anduril462 View Post
    That means your program can do anything. It could print "Hello world" the first time you run it, and reformat your hard drive the next, and it would be correct in doing so.
    That... might be a bit of a stretch. I'm sure they'll get your point, though. You might want to see a specialist if you see a giant toilet with a giant fang-filled mouth chasing a guy with stdin written on his shirt down an endless corridor every time you close your eyes, though.
    Sent from my iPad®

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Database to Database Transfer?
    By draggy in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2007, 10:50 AM
  2. Database API?
    By taelmx in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-12-2007, 11:26 PM
  3. Database
    By sreetvert83 in forum C++ Programming
    Replies: 6
    Last Post: 09-14-2005, 10:06 AM
  4. which database do u use ??
    By moemen ahmed in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 08-27-2003, 11:12 AM
  5. Replies: 1
    Last Post: 10-09-2001, 10:20 PM