CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2006
    Posts
    4

    Question converting string to int from files

    hi
    I'd like to convert a string which contains many numbers into int and then store them in an array.
    the file.txt looks like this:
    54 42 432 121
    0 23 435 32
    43 343 654 45
    12 5 65 23

    the procedure that I'm doing is this:
    ifstream f81;
    ofstream f81r;
    f81.open("f81.txt");
    f81r.open("f81r.txt");
    int i;
    string line;
    while(!f81.eof())
    {
    getline(f81,line);
    for(int k=0;k<32;k++) //my matrix size is 32x32
    {
    f[i][k]=?????? // help needed here.
    }
    i++;
    }
    thanks in advance.

  2. #2
    Join Date
    Dec 2004
    Location
    Paso de Robles
    Posts
    296

    Re: converting string to int from files

    Have a look at stringstream.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: converting string to int from files

    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    May 2006
    Posts
    4

    Re: converting string to int from files

    thanks for the reply...
    I found another method to do it, but I have a little problem.
    I tryed to use a getline function (from #include<string> ) but it give me an error in both C++ and Visual C++
    also if I tryed to use string variable it doesn't work.

    this is the code:

    #include<string> // I tried string.h but also didn't work
    .
    .
    CString line; // I tryed to type it string and give me the same error *
    while(!f81.eof()) // I'm getting the line from a file
    {
    getline(f81,line); // another error **
    *error C2065: 'CString' : undeclared identifier or 'string'
    **error C2065: 'getline' : undeclared identifier

    i tryed to read the faqs but only find the #include <afx.h> and the cstring is working, but still the getline function doesn't work.
    any help is appreicated.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: converting string to int from files

    Use std::string, not CString. Use std::getline, not getline (unless you have a using std::getline statement).

  6. #6
    Join Date
    May 2006
    Posts
    4

    Re: converting string to int from files

    I never used the std:: () statement...and in the book i have it's only string, getline....I'm a bit confused.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: converting string to int from files

    Your book might be outdated. You should read up on namespaces. Anyway, since your book only mentions string, you should use string (i.e. std::string), not CString.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured