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!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.