Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,067 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,596 people online right now. Registration is fast and FREE... Join Now!




Problems in saving structure into txt file

 
Reply to this topicStart new topic

Problems in saving structure into txt file

kwlyamateh
13 Apr, 2008 - 05:28 AM
Post #1

New D.I.C Head
*

Joined: 13 Apr, 2008
Posts: 5

CODE

#include <stdio.h>

struct detail
{
    char name[40];
    int phonenum;
};

void main()
{
    
    struct detail aPerson;
    FILE *fp;
    fp = fopen("C:\\Database.txt", "a+");

    printf("Enter his name: ");
    scanf(" %s", aPerson.name);

    printf("Enter his phone number: ");
    scanf(" %d", &aPerson.phonenum);

    fwrite(&aPerson, sizeof aPerson, 1, fp);
    printf("Details saved!\n");
    fclose(fp);
    
    system("PAUSE");
}


I have a problem here. I could only save the person's name into the text file but not the person's phone number.
Please help ph34r.gif Thank you smile.gif
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Problems In Saving Structure Into Txt File
13 Apr, 2008 - 05:40 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
it should be saving both. Keep in mind that it is not going to write out the text for the structure, it is going to write it out in binary. That means that when you look at the file in say notepad you will see the persons name, but then some strange symbols for the phone number.

Try reading the file back in.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 06:47PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month