Question
Say I had a large number like: 23523235243546345634123346352352345 in a .txt file and I wanted to read it in NUMBER BY NUMBER I tried something like this: ifstream number("number.txt"); int num[num_length]; for (int i = 0; i < num_length; i++) { char b; number >> b; num[i] = b; cout << num[i]; } And I get an extremely out of order, extremely random assortment of numbers. What is an easy way to do this? I feel stupid :/
I was hoping there was a like "int char" or something I could use :/
@myininaya
When you read in from a text file, you'll get text. A string would be appropriate to hold the number at this point: http://www.cplusplus.com/reference/string/string/ You can iterate over the string and get the characters individually easily enough and convert them integers http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/
i would shove each number in a slot in an array then loop through the array and output it or you could write a delimiter such as : between each number and split it using a split method for Strings I choose the delimiter way because it would use more processor and less memory to accomplish the task which is a good thing especially if you target a quadcore or greater processor to run your app on and maybe your program is ram hungry you know nice balance between processor and ram usage is alway ideal in my view. --calmchess
If you use a space as a delimiter, you can use one "myint << myIfstream" for each number
Join our real-time social learning platform and learn together with your friends!