Im writing a simple program that will ask user to enter his name and age,
store it in a file and then display it from the file... Here's what i got so far....
Edited: see code below....
I dont know what im doing wrong... Help?
Im writing a simple program that will ask user to enter his name and age,
store it in a file and then display it from the file... Here's what i got so far....
Edited: see code below....
I dont know what im doing wrong... Help?
Last edited by Karpaty; 11-23-2007 at 06:18 PM.
- Move the function prototypes outside of and before main() for good form.
- Do NOT use gets(). Use fgets() instead.
- In your add_data() function, age is a pointer to an int, but you try to write to age's address in memory via scanf(). This is wrong. To correct, simply get rid of the & in your scanf() call there.
- Don't declare the function parameter name to be an array of char *'s unless you actually meant that (which you didn't).
After you do these changes, then post the compiler errors.
Ok, i rewritten the program with structures (included changes you suggested, apart from fgets(). when i use it the code doesnt compile)... Here's the updated code:
....although this time it compiles, nothing is written into the test2.dat file... Its still empty..Code:#include <stdio.h> struct data { char name[20]; int age; }; void add_data(data*); void seek_end_db(FILE*); void write_to_db(FILE*, data*); void read_from_db(FILE*, data*); int main() { char name[20]; int age; data person; FILE *fp; fp = fopen("test2.dat", "rb+"); add_data(&person); seek_end_db(fp); write_to_db(fp, &person); read_from_db(fp, &person); } void add_data(data *person) { printf("Enter your name: "); gets(person->name); printf("Enter your age: "); scanf("%d", &person->age); } void seek_end_db(FILE *fp) { fseek(fp, 0, SEEK_END); } void write_to_db(FILE *fp, data *person) { fwrite(person, sizeof(data), 1, fp); } void read_from_db(FILE *fp, data *person) { fread(person, sizeof(data), 1, fp); }
Maybe you should open the file for writing if you plan on writing.
Doesnt "rb+" open the file for reading/writing?
I tried to change it to just "w" (to write into test2.txt), but it didnt work...
If the file doesn't exist, you have to use the "wb+" mode, but if it does exist, use the "rb+" mode instead.
And actually, i don't know which compiler you are using, but in your case, "data" isn't a type but "struct data" is. You should change this, or typedef your struct.
Also, if you want to read what you just wrote in the file, rewind the FILE. Else, you won't read much.
And double also, if you want something to be writen, close (fclose) the FILE before leaving main.
> fwrite(person, sizeof(data), 1, fp);
These should be sizeof(*data)
After doing an fwrite() to an update file such as this, you need to do fflush(fp) before you do an fread() on the file.
You also need to do an fseek() if you hope to read the record you just wrote. There is only one 'location' within a file which is used for both writing and reading.
If you've been trying this for a while now, your current data file might be full of all sorts of rubbish. You might want to consider "wb+" for initial testing so you always start with an empty file.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Don't use gets, it's a very dangerous function.
You can BOF on it also.
----------
Since you don't use typedef to your struct, this is permitted:
You can change it to this:Code:data person;
Or either way, change the struct to to this:Code:struct data person;
So you could use it the way you did.Code:typedef struct { char name[20]; int age; }DATA;
Last edited by eXeCuTeR; 11-24-2007 at 10:37 PM.