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?
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?
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?
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
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:
Do I have to store the structure dynamically, or make it array? If yes, then why?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"); }
Last edited by Harith; 09-02-2013 at 11:39 PM.
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
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?
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.
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?
Ya, but you can't jump from one record to another.
Actually, it doesn't work. Or maybe it does, but by sheer dumb luck:
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.Code:fflush(stdin);
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.
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®