Thread: Not able to read data from text file and convert it to integer

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    5
    Thanks all! it was really helpful. I am a noob.

    @laserlight : I have changed my code as follows, but i don't know how do this -->
    Then, use the return value of getline, not good(), to control the inner loop.
    can you explain me in more details.

    New code:
    Code:
    // reading a text file
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main () {
    
     string line;
     int i,j,c;
     int Ncoil, Ntank;
     int **Tmin, **Tmax;
     
     char str[256];
    
     cout << "Number of coils to be cleaned: ";
     cin >> Ncoil;
    
     cout << "Number of tanks: ";
     cin >> Ntank;
    
    Tmin = new int *[Ncoil];
    ifstream myfile ("Tmin.txt");
    if (myfile.is_open())
        {
            
                for (int i = 0; i <= Ncoil; ++i)
                  {
                     Tmin[i] = new int[Ntank];
                     for(int j = 0; j <= Ntank; ++j)
                        {
                                 getline(myfile, line, ' ');
                                while (???????)
                                {
                                c = atoi(line.c_str());
                                Tmin[i][j] = c;
                                cout<< Tmin[i][j]<<endl;
                                }
                        }
                        
                 }
                myfile.close();
        }
    
    else cout << "Unable to open Tmin file";
        
          
      
      return 0;
    }
    Last edited by pratras; 03-25-2013 at 02:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read text file, scan and store data into variables?
    By wisdom30 in forum C Programming
    Replies: 8
    Last Post: 04-18-2011, 11:23 PM
  2. Converting integer data from text file
    By tkecanuck341 in forum C++ Programming
    Replies: 2
    Last Post: 03-25-2010, 06:29 AM
  3. read txt file as binary then convert to text
    By 911help in forum C Programming
    Replies: 2
    Last Post: 01-04-2008, 06:29 AM
  4. read text file, convert character numbers to int?
    By hyaku_ in forum C Programming
    Replies: 6
    Last Post: 11-06-2006, 05:04 PM