array from file - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


array from file

chienquangchienquang Posts: 1Member
hi there, i have a trouble in my recent project. the problem descrition is:
- a input_file.dat with lines of numbers
2 4 5
7 6 8
7 4 12
notice the number "12"
now i want to read numbers from input_file.dat and load them into array.

void main()
char ch, arr[50];
int i=0;
ifstream infile("input_file.dat");
while (infile.get(ch))
the work is possible in that example, but when i changed the type of arr[] to integer to manipulate data, it can't work because difference of type char-integer. and it treats 12 as arr[x]=1, arr[y]=2 there any function convert char to int?
2.can i get the number 12?
3.can you give the source to load input_file.dat to: integer 2Ds array?



  • stoberstober Posts: 9,765Member ✭✭✭
    you should use an int array, not a char array. cin will make the conversion for you.

    : void main()
    : {
    : int arr[50];
    : int i=0;
    : ifstream infile("input_file.dat");
    : while (i < 50 && infile >> arr[i])
    : {
    : i++;
    : }
    : }

Sign In or Register to comment.