First off this is not for homework, I'm not taking classes.
What I'm trying to accomplish is how to read a text file that has numbers separated by spaces like this:
1 3 5 7 ect..
Split it up into the different numbers and change them into Integers.
For some reason I have having a hard time doing this.
I can read the file in but have no Idea how to break up the number into separate numbers.
can anyone point me in the right direction.
Thanks
Converting number in text file to ints?
Started by Djanvk, Aug 11 2008 10:30 PM
12 replies to this topic
#1
Posted 11 August 2008 - 10:30 PM
|
|
|
#3
Posted 12 August 2008 - 10:14 AM
Read the whole file into a buffer. Then use strtok() with ' ' as the delimiter, and then use atoi() to convert them into integers.
#4
Posted 12 August 2008 - 08:09 PM
MeTh0Dz|Reb0rn said:
Read the whole file into a buffer. Then use strtok() with ' ' as the delimiter, and then use atoi() to convert them into integers.
But as to the original question scanf or fscanf (not recommended for newbs) will mostly cover it using %d. Or if you mean C++ let us know. Also, there are very many ways to do this, each with its own nuance. Post an attempt and we'll comment.
#5
Posted 13 August 2008 - 10:44 AM
Interesting, I did not know this. Thanks!
Edited by Jordan, 14 August 2008 - 04:25 AM.
Quit arguing about everything
#6
Posted 13 August 2008 - 08:59 PM
Au contraire, <removed>. They were all mine.
Edited by Jordan, 14 August 2008 - 04:25 AM.
Watch the language...
#7
Posted 17 August 2008 - 12:45 AM
Thanks for the answers, this bit of code here worked
while (inFile >> x) {
cardnums[i] = x;
i++;
}
#8
Posted 17 August 2008 - 01:29 AM
Also if anyone can explain how this works?
Thanks
Thanks
#9
Posted 17 August 2008 - 07:10 AM
I'll use your code for the explanation. Hope you don't mind :)
For me, I'll actually recommend a shorter code.
This actually eliminate the use of the buffer(x), hence using less resource and computing power.
while (inFile >> x) //Checks if there's something to read, and reads it in the process
{
cardnums[i] = x; //Makes cardnums[i] the integer that had been read.
i++; //Increase counter, to prepare for next cardnums.
}
For me, I'll actually recommend a shorter code.
while (inFile >> cardnums[i])
{
i++;
}
This actually eliminate the use of the buffer(x), hence using less resource and computing power.
Edited by telboon, 17 August 2008 - 09:54 AM.
#10
Posted 17 August 2008 - 09:50 AM
You run the risk of buffer overrun with that code. I'd suggest using a for loop, or a while loop that dynamically reallocates the buffer with each read, which is pretty slow.
And use CODE tags, please
And use CODE tags, please
#11
Posted 17 August 2008 - 01:47 PM
thanks for the reply's.
How exactly does it know to separate the file at the spaces with this code?
How exactly does it know to separate the file at the spaces with this code?
#12
Posted 17 August 2008 - 02:15 PM
It does it automatically, because a space isn't part of a valid number. Don't worry about it at this point.


Sign In
Create Account

Back to top









