loop and filestreams problem

line 47 is the line true?.this program get endless loop.sorry my english
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
/////////////////////////////
class person
{
      protected:
                char name[80];
                int age;
      public:
             void getData()
             {
                  cout << "\nEnter name: "; cin >> name;
                  cout << "\nEnter age: "; cin >> age;
             }
             
             void showData()
             {
                  cout << "\nName: " << name;
                  cout << "\nAge: " << age ;
             }
};
int main(int argc, char *argv[])
{
    char ch;
    person pers;
    
    fstream file; 
    
    file.open("GROUP.DAT", ios::app | ios::out | ios::in | ios::binary); 
    
    do
    {
                           cout << "Enter person's data: ";
                           pers.getData();
                           
                           file.write(reinterpret_cast<char*>(&pers),sizeof(pers) ); 
                           cout << "Enter another person(y/n)? ";
                           cin >> ch;
    }while(ch == 'y');
    
    file.seekg(0); 
    
    file.read( reinterpret_cast<char*>(&pers),sizeof(pers) ); 
    while(!file.eof() )
    {
           cout << "\nPerson:";
           pers.showData();
           file.read( reinterpret_cast<char*>(&pers),sizeof(pers) );  
    }
    
    cout << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Last edited on
I would do while(file) instead, so in case the file breaks, you will exit.

And also:
file.write(reinterpret_cast<char*>(&pers),sizeof(pers) ); file.read( reinterpret_cast<char*>(&pers),sizeof(pers) );

Wtf is this?? You can't serialize stuff like this >_> (at least not in a readable format)
thanks.i solve problem.but file.write(reinterpret_cast<char*>(&pers),sizeof(pers) ); it's normal.and what does suggestion to me ? i have one choice.this is
1
2
file.write(reinterpret_cast<char*>(&pers),sizeof(pers) );
file.read( reinterpret_cast<char*>(&pers),sizeof(pers) );

i solve this look:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//i don't what the hell:D?
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
/////////////////////////////
class person
{
      protected:
                char name[80];
                int age;
      public:
             void getData()
             {
                  cout << "\nEnter name: "; cin >> name;
                  cout << "Enter age: "; cin >> age;
             }
             
             void showData()
             {
                  cout << "Name: " << name;
                  cout << "\nAge: " << age ;
             }
};
int main(int argc, char *argv[])
{
    char ch;
    person pers;
    
    fstream file; //create I/O stream
    
    file.open("GROUPS.DAT",ios::binary | ios::in | ios::out | ios::out);//open for add
    
    do
    {
                           cout << "Enter person's data: ";
                           pers.getData();
                           
                           file.write(reinterpret_cast<char*>(&pers),sizeof(pers) ); //write file
                           cout << "Enter another person(y/n)? ";
                           cin >> ch;
    }while(ch == 'y');
    file.close();
    
   
file.open("GROUPS.DAT",ios::binary | ios::in | ios::out | ios::out);
         file.read( reinterpret_cast<char*>(&pers),sizeof(pers) ); //read firs person
    while( !file.eof() )
    {
           cout << "\nPerson:";
           pers.showData();
           file.read( reinterpret_cast<char*>(&pers),sizeof(pers) ); //other person read
    }
    
    cout << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
check the last post of http://www.cplusplus.com/forum/beginner/11512/
I hope you will learn useful stuff from my code there...
thank u.your topic is very useful.but i must solve this program with my last code :(. i'm very tired today.life is hard but c++ more hard than life :).I don't know where's my problem
i'm sleeping now and please i hope you are solve my problem tomarrow.anything don't working.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
/////////////////////////////
class person
{
      protected:
                char name[80];
                int age;
      public:
             void getData()
             {
                  cout << "\nEnter name: "; cin >> name;
                  cout << "Enter age: "; cin >> age;
             }
             
             void showData()
             {
                  cout << "Name: " << name;
                  cout << "\nAge: " << age ;
             }
};
int main(int argc, char *argv[])
{
    char ch;
    person pers;
    
    fstream file;
    
    file.open("hak.DAT",ios::binary | ios::in | ios::out | ios::out);
    
    do
    {
                           cout << "Enter person's data: ";
                           pers.getData();
                           
                           file.write(reinterpret_cast<char*>(&pers),sizeof(pers) ); 
                           cout << "Enter another person(y/n)? ";
                           cin >> ch;
    }while(ch == 'y');
   
         // file.close();
    file.seekg(0);
    
          
         //file.open("hak.DAT",ios::binary | ios::in | ios::out | ios::out);
         
         file.read( reinterpret_cast<char*>(&pers),sizeof(person) ); 
    while(file)
    {
           cout << "\nPerson:";
           pers.showData();
           file.read( reinterpret_cast<char*>(&pers),sizeof(person) );
    }
    
    cout << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}  

now i see problem.between line 43 and 51
Topic archived. No new replies allowed.