read integer from txt file

Dear all, I am learning C++ and I got a problem on reading int numbers from a file.
I wrote codes that can read data as char from a file. But when I try to convert those char data into an int array's elements, the program run and displays the output as before but crushes immediately and I get a Windows error saying: " the program has stopper working" in which I have to chose either "check online for a solution" and "close the program".
Can someone help me to handle that? I am using windows vista as OS, is it the problem.
Here are the codes of my program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    ifstream infile("C:\\Cpp\\table.txt");//to open the text file
    if(!infile) cout<<"Error: File not found"<<endl;//to display an error message once the file does not exist
    char word[3];//this will contains read character from file
    int i=0;
    int ar[]={};//an int array to contain converted characters
    while(infile.getline(word,100,'\t'))
    {
                                        if(i>=1) //to skip the first tab character
                                        {
                                                 cout<<"element["<<i-1<<"] = "<<word<<" \n";//to display read characters
                                                 ar[i-1]=int(*word);//convert character into int
                                                 }
                                        i++;
                                        }
    
    system("PAUSE");
}


Here is the content of the file that I want to read. it is called "table.txt". Numbers are separated by a tab.

00 01 02
10 11 12
20 21 22
30 31 32
40 41 42
50 51 52
60 61 62
70 71 72
80 81 82
90 91 92
closed account (zb0S216C)
I think line 11 might be an issue. I've never seen an array defined like that, so I don't know if that's legal or not. Come to think of it, I think you problem resides on line 12. You specify that you want to extract 100 characters from the input stream. The problem is that the buffer is gave( the first parameter of getline( ) ) is only 3 characters long. This results in a buffer overflow, I believe.
Topic archived. No new replies allowed.