954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading text into integer vector

Hi everybody,

I really need that code,

I have a text file

32 4
12 13 4 12
0 0 0 0 0 3 2 3 4

and I want to convert it vector or array

like A[2]={32,4} B[4]={12,13,4,12} C[9]={0,0,0,0,0,3,2,3,4}

The size of the vector or array can be changed everytime.

airerdem
Newbie Poster
14 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

:icon_rolleyes:

frogboy77
Posting Pro in Training
481 posts since Aug 2010
Reputation Points: 100
Solved Threads: 39
 

>I really need that code
Then write it. That's what I do when I really need code for something.

>I have a text file
>and I want to convert it vector or array

I'm sure you've used cin's >> operator to read integers from standard input. What you did was read a stream of text and convert it to int, though most of that work happened under the hood. You can do the same thing with your file:

#include <fstream>

int main()
{
    std::ifstream in("myfile");
    int x;

    if (in && in>> x)
        std::cout<< x <<" squared is "<< x * x <<'\n';
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

If you are having problem with converting text '1' into integer 1 then look at the ASCII table. There you see the decimal value of character '1' is 49.

There if you read '1' into an integer variable you are holding 49. If you just decrease that by 48 you get 1. It goes same for 2 and 3 and .. too.

Tellalca
Posting Whiz in Training
209 posts since Mar 2010
Reputation Points: 29
Solved Threads: 24
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: