Jump to content

String from file to int


Negat1ve
 Share

Recommended Posts

It's all about streams.

#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;

int main()
{
    // Converting using stringstream
    stringstream ss("123");
    int num;
    ss>>num;
    cout << num << endl;

    // Getting line as int directly
    fstream file("somefile.txt",ios::in);
    int num2;
    file >> num2;
    cout << num2 << endl;
    

}

Edit: Beautiful, isn't it? ;)

Edited by monoceres

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Doesnt work =\

Output :

------ Build started: Project: DropCalcDropImport, Configuration: Release Win32 ------

Compiling...

DropCalcDropImport.cpp

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(191) : error C2039: 'ss' : is not a member of 'std'

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(192) : error C2065: 'endl' : undeclared identifier

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(195) : error C2653: 'ios' : is not a class or namespace name

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(195) : error C2065: 'in' : undeclared identifier

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(195) : error C2079: 'file' uses undefined class 'std::basic_fstream<_Elem,_Traits>'

with

[

_Elem=char,

_Traits=std::char_traits<char>

]

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(195) : error C2078: too many initializers

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(197) : error C2039: 'file' : is not a member of 'std'

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(197) : warning C4552: '>>' : operator has no effect; expected operator with side-effect

d:\project\dropcalcdropimport\dropcalcdropimport\Form1.h(198) : error C2065: 'endl' : undeclared identifier

Build log was saved at "file://d:\Project\DropCalcDropImport\DropCalcDropImport\Release\BuildLog.htm"

DropCalcDropImport - 8 error(s), 1 warning(s)

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Link to comment
Share on other sites

You're doing something wrong.

Just the fact that the errors are coming from a .h definitely tells me you're doing something wrong. (generally .h files should contain declarations, not definitions).

Edit: Really!? STL is an incredible powerful and useful addition to C++. It's usually the first thing people learn when they reading about C++.

Edited by monoceres

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

I know that it is managed C++ .. and finaly found simple solution for conversion

It would have been helpful if you had mentioned that earlier. Managed C++ != C++. They are very different. You said and I quote:

his is for autoit .. i need to know how to do it in C++ ( MS Visual C++ 2008 )

That is wrong, you need to know how to do it in Managed C++. A very important detail.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...