I am trying to convert a string to a signed int from a file:
-1
2
3
4
5
6
7
8
9
Trying to use atoi with no luck.
val = atoi(line); did not work .
Any ideas?
int main () {
string line;
int val;
line = "-10";
val = atoi(line);
ifstream myfile ("int9.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
atoi() expects a C-style string, so you need to pass it one of those instead of an std::string. You can get one using the c_str() method of std::string, though, so just add that in and it should work.
My code reads one integer at a time, but since your file has only one integer per line, the code effectively reads one line at a time. Formatted stream input.