Jump to content

Converting number in text file to ints?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
Djanvk

Djanvk

    Newbie

  • Members
  • Pip
  • 6 posts
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

#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
You need some way to detect the spaces as you read the fıle, then just do a conversıon (parsıng).
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#3
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Read the whole file into a buffer. Then use strtok() with ' ' as the delimiter, and then use atoi() to convert them into integers.

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

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.
Ignoring all characters - Dev Shed - atoi is the low man on the totem pole, strtok should be used wisely. [Some reasons as to why I don't care for getting too chummy with strtok.]

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
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Interesting, I did not know this. Thanks!

Edited by Jordan, 14 August 2008 - 04:25 AM.
Quit arguing about everything


#6
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Au contraire, <removed>. They were all mine.

Edited by Jordan, 14 August 2008 - 04:25 AM.
Watch the language...


#7
Djanvk

Djanvk

    Newbie

  • Members
  • Pip
  • 6 posts
Thanks for the answers, this bit of code here worked

while (inFile >> x) {
        cardnums[i] = x;
        i++;
    }


#8
Djanvk

Djanvk

    Newbie

  • Members
  • Pip
  • 6 posts
Also if anyone can explain how this works?

Thanks

#9
telboon

telboon

    Newbie

  • Members
  • PipPip
  • 26 posts
I'll use your code for the explanation. Hope you don't mind :)

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
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
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

#11
Djanvk

Djanvk

    Newbie

  • Members
  • Pip
  • 6 posts
thanks for the reply's.

How exactly does it know to separate the file at the spaces with this code?

#12
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
It does it automatically, because a space isn't part of a valid number. Don't worry about it at this point.