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.

Edited by airerdem: n/a

7 Years
Discussion Span
Last Post by Tellalca

>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';
Votes + Comments
sometimes i think you're almost human.(sometimes)

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.