reading integer from file

how can i put all integers that i read from files into array seems i have a bit trouble here it alwys print the last integer value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 int main() {
	fstream file;
	int array[4];
	int x;
	file.open("o.txt");

	while (file>>x) {
		cout << x << endl;
		for (int i = 0; i < 4; i++) {
			array[i] = x;
		}
		}
	for (int i = 0; i < 4; i++) {
		cout << array[i] <<' ';
	}
	
	

	file.close();
	system("pause");
	return 0;
}


file:
100
90
40
30
121
Your array is too small to hold the entire contents of the file. You should also keep track of how many values have been read from the file - it may not be the same as the array size. (but cannot be greater).

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
int main() 
{

    // make size at least as large as needed
    const int size = 20;
    int array[size];

    
    ifstream file("o.txt");
 
    int count = 0; 
    int x;

    // check that array is not already full
    // and read integer from file, 
         
    while (count < size && file >> x) 
        array[count++] = x;


    // display the values stored in the array
    for (int i = 0; i < count; i++)
        cout << array[i] <<' ';
    
}

Last edited on
tnx alot mate..
i already mange to fix it .
but have another quastion if you can help
how to write object to txt file.
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
class Student {
	string name;
	int score;
public:
	Student() {}
	void setdata() {
		cout << "Enter name" << endl;
		cin >> name;
		cout << "Enter score" << endl;
		cin.ignore(1000, '\n');
		
	}


};
int main() {
	fstream file;
	file.open("student.txt");
	int i = 0;
		Student s;
		s.setdata();
		file << s;    //???

		system("pause");
		return 0;


}
A convenient way to do that sort of thing is to overload the input and output operators >> and << for the Student class:

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

class Student {
    string name;
    int score;
    
public:
    Student() {}
    void setdata() {
        cout << "Enter name" << endl;
        getline(cin,name);
        cout << "Enter score" << endl;
        cin >> score;
        cin.ignore(1000, '\n');
        
    }


    friend ostream & operator << (ostream & os, const Student & s)
    {
        os << s.name << '\t' << s.score;
        return os;
    }


    friend istream & operator >> (istream & is, Student & s)
    {
        getline(is, s.name, '\t');
        is >> s.score;
        is.ignore(10, '\n');
        return is;
    }

};

int main() 
{
    ofstream file("student.txt");
    
    Student s;
    
    for (int i=0; i<3; ++i)
    {
        s.setdata();
        file << s  << '\n';  
    }
      
    file.close();
    
    cout << "\n reading from file:\n";
    
    ifstream fin("student.txt");
    while (fin >> s)
        cout << s << '\n';
    
}

Last edited on
thanks alot man helpd alot
Topic archived. No new replies allowed.