Thread: Whats wrong with this program?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    38

    Whats wrong with this program?

    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.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    1. Move the function prototypes outside of and before main() for good form.
    2. Do NOT use gets(). Use fgets() instead.
    3. 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.
    4. 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.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    38
    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:

    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("&#37;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);
    }
    ....although this time it compiles, nothing is written into the test2.dat file... Its still empty..

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Maybe you should open the file for writing if you plan on writing.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    38
    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...

  6. #6
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    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.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yes, you're correct.

    /me slaps self.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    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:
    Code:
    	data person;
    You can change it to this:
    Code:
    	struct data person;
    Or either way, change the struct to to this:
    Code:
    typedef struct
    {
    	char name[20];
    	int age;
    }DATA;
    So you could use it the way you did.
    Last edited by eXeCuTeR; 11-24-2007 at 10:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 05:09 PM