How to convert a string to int

I am saving an int to a text file and reading it back but I am unable to reuse that int saved in file as an integer.
Please help.
C:
1
2
3
4
5
6
7
int a;
//read int from file
fscanf(file, "%d",&a);
//read int from string
sscanf(string, "%d", &a);
//'convert' string to int:
a = atoi(string)


C++
1
2
3
4
5
6
7
8
9
int a;
//read int from file
file >> a;
//read int from string
stringstream ss;
ss << string;
ss >> a;
//'convert' string to int:
a = atoi(string.c_str());


Last edited on
Stream operations allow reading into integers just as with any other basic type

1
2
3
ifstream file("yourfile");
int your_int;
file >> your_int;
Topic archived. No new replies allowed.