Thread: Files problem

  1. #1
    Unregistered
    Guest

    Question Files problem

    I use Turbo c++ 3.0

    Recently I made a structure and wrote it to the file and when I tried to read from it I get errors.

    For eg : - I get this

    Name : Ron%$##$$
    Age : #3
    Email : moderato#@hotmail.com

    Instead of :
    Name : Ronaldson
    Age : 13
    Email : [email protected]

  2. #2
    Akilla
    Guest

    Here's how I wrote a similar program

    Here's how I wrote a similar program

    PHP Code:
    // learning structures//

    #include <stdio.h>
    #include <conio.h>

    typedef char def_name[15];

    struct tag_name
    {
        
    def_name firstname;
        
    def_name lastname;
    };

    struct tag_age
    {
        
    int years;
    };

    struct tag_number
    {
        
    int number;
    };

    struct tag_record
    {
        
    tag_name name;
        
    tag_age age;
        
    tag_number phonenumber;
    record[2];

    void getdata (void)
    {
        
    int i;
        
        for (
    i=0i<=1i++)
        {
            
    printf("Enter Name: ");
            
    scanf("%s %s"record[i].name.firstnamerecord[i].name.lastname);
            
    printf("Enter Age: ");
            
    scanf("%d", &record[i].age.years);
            
    printf("Enter Phone Number: ");
            
    scanf("%d", &record[i].phonenumber.number);
        }

    }

    void showdata (void)
    {
        
    int i;
        for (
    i=0i<=1i++)
        {
            
    printf("Record: %d\n"i);
            
    printf("Name  : \t%s %s\n"record[i].name.firstnamerecord[i].name.lastname);
            
    printf("Age   : \t%d\n"record[i].age.years);
            
    printf("Phone : \t%d\n\n"record[i].phonenumber.number);
        }
    }
        
    main()
    {
        
    int i;

        
    getdata();
        
    printf("\n");
        
    showdata();
        

        
    getchar();
        
    getchar();

    I unnecessarily put some typedefs ('cause i was learning those
    things a few days ago). ..
    you can simply declare directly if you want if you don't like typedefs..

    hope this'll be useful.

    COOL PROGRAMS @ www.akilla.tk

  3. #3
    Akilla
    Guest

    By the way

    By the way.... the problem you're getting is maybe due
    to improper use of variables... (like mixing up of char
    with string, etc)

    See my code and see how I used the variable types..

    **If you're confident you won't get confused**, see my
    C++ code below ....

    PHP Code:
    //program to learn classes

    #include <iostream.h>
    #include <stdio.h>

    class person
    {
        
    char name[30];
        public:
        
    void getdata();
        
    void showdata();
        
    int age;
    };

    void person :: getdata()
    {
        
    cout << "Enter name : ";
        
    cin >> name;
        
    cout << "Enter age : ";
        
    cin >> age;
    }

    void person :: showdata()
    {
        
    cout << "Name: "<< name << endl;
        
    cout << "Age: " << age << endl;
    }

    main()
    {
        
    int i;

        
    person someone[2];

        for(
    i=0i<=1i++)
        {
            
    someone[i].getdata();
        }

        for(
    i=0i<=1i++)
        {
            
    someone[i].showdata();
        }
        
    getchar();

    That's C++ syntax.

    COOL PROGRAMS @ www.akilla.tk

  4. #4
    Akilla
    Guest

    and.... :-)

    ah.. looks like I'm talking too much.. :-)

    anyway, if you want to write it to a file..

    use
    FILE *fp

    fp = fopen("FILENAME.TXT", "w");

    and whenever you're writing to the file, use

    fprintf(fp, variablenamehere);

    variablename will be
    any variable from your struct (like the name, email address, etc)

    hope this helps..

    COOL PROGRAMS @ www.akilla.tk

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem compiling files that store functions
    By tiachopvutru in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2008, 05:42 PM
  2. Problem with Header files.
    By chakra in forum C Programming
    Replies: 5
    Last Post: 05-10-2008, 01:30 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. problem with sending files to a class method..
    By utoots in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2003, 01:38 AM
  5. problem with resource files
    By nickeax in forum Windows Programming
    Replies: 1
    Last Post: 03-14-2003, 02:32 AM