Reading numbers from a text file

This is a discussion on Reading numbers from a text file within the C++ Programming forums, part of the General Programming Boards category; Dear friends, there is a text file ( I copied a few lines of it below), I want to read ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    23

    Question Reading numbers from a text file

    Dear friends,

    there is a text file ( I copied a few lines of it below), I want to read every number in one line and want to insert to a vector. number of columns and lines can change.
    How can I do that with using ifstream or any other method?
    I will really appreciate any help.
    thanks.

    Code:
        8    8   0.3886  0.2993  0.3997  0.3656  0.3048  1.0000  0.2231  0.1664  0.1887  0.1690  0.2214  -0.3670  10.2611     5.37  4
       16    8   0.3410  0.5109  0.7572  0.9174  0.9638  1.0000  0.9823  0.9277  0.7746  0.1978 -1.0000  -0.1390   0.2703     5.07  5
       24    8   0.9683  0.9873  0.9772  0.9798  0.9940  1.0000  0.9933  0.9828  0.9762  0.9731  0.9438  -0.0076   0.0006     5.07  2
       32    8   0.9610  0.9847  0.9661  0.9820  0.9974  1.0000  0.9954  0.9843  0.9777  0.9848  0.9688  -0.0064   0.0009     5.13  0
       40    8   0.9692  0.9759  0.9577  0.9828  0.9935  1.0000  0.9975  0.9829  0.9662  0.9889  0.9751  -0.0059   0.0017     5.33  3
       48    8   0.9537  0.9587  0.9442  0.9810  0.9962  1.0000  0.9940  0.9756  0.9628  0.9852  0.9769  -0.0078   0.0022     5.88  7
       56    8   0.9626  0.9727  0.9399  0.9700  0.9950  1.0000  0.9955  0.9824  0.9585  0.9762  0.9694  -0.0074   0.0032     6.39  7

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Do you know how to do something similar with cin? Using an ifstream is almost the exact same as cin.

    To handle a variable number of lines, just read until the read fails (which will usually be because it reached the end of the file). To handle a variable number of columns, it depends on what the columns mean. Is there any indication of how many columns there will be for a given line? If you don't know, then I would read an entire line into a string with getline. Then put that line into a stringstream. Then you can read from a stringstream just like you read from cin or the ifstream with operator>>.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    23
    I dont know cin exactly,
    for example how can we assign diffrent numbers (entered from console) to the different array elements, and how can we know that how many number entered ?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by wolfindark
    I dont know cin exactly,
    for example how can we assign diffrent numbers (entered from console) to the different array elements
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int i;
    	int array[20];
    	for(i=0;i<20;i++)
    	{
    		cout << "Enter a number: ";
    		cin >> array[i];
    	}
    	
    	for(i=0;i<20;i++)
    	{
    		cout << "array[" << i << "] = " << array[i] << endl;
    	}
    }
    That's a rather simple way of using cin to read into an array.

    Quote Originally Posted by wolfindark
    and how can we know that how many number entered ?
    You don't really need to know since you said you'll be putting it inside a vector. Once you're done reading numbers (ie. ran out of numbers to read), then just find out how big the vector is.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    23
    MacGavyer,
    if user enter numbers less less than 20 ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    if user enter numbers less less than 20 ?
    With a vector that is not a problem. I suspect that you may end up using a vector of vectors, with the inner vector holding the numbers read from a line.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    The loop terminates and so does the program.

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    The code I provided was just an example of how to read from stdin using cin.

    Don't use an array and don't hardcode the number of times to read.

    Have one float or double and one vector. Have a loop, where inside the loop, you read one value, and then put it in your vector. Keep going until it can't read anymore, where you either hit EOF or some other way of telling you that you hit the end of the file.

    I suggest you read up on the C++ Vector and file handling.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    23
    Ok, we can understand the end-of-file, I can check it.
    but in one line how can I check it? How can I check the end-of-line ? How the for loop will stop reading that line ? and without stopping the run it has to pass to the following line.
    as beene says, if program stops , it is not good. I can read only one line....

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> How can I check the end-of-line ?
    As I said earlier, just use getline to read a line into a string (getline can find the end of the line). Then put that line into a stringstream and read each value from that stringstream.

    >> Ok, we can understand the end-of-file, I can check it.
    You should not check for end-of-file. While it is possible to do that correctly, it is also commonly done wrong. It is better to use the return value of the read. So if you use getline to read in a line, you would put that into the while control:
    Code:
    while (std::getline(my_file, line))

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    23
    I am sorry for my questions , they may be so silly

    Ok, I read the line with get line, and I assigned that line to istringstream.

    Code:
         ifstream ccinp;
         string line;
         double number;
         vector<double> cc;
    
         while(getline(ccinp,line)){
           cc.clear(); 
           istringstream terr(line);
           for(int i=0;i<20;i++){
             terr>>number;
             cc.push_back(number);
           }
        
        int M= cc.size; 
        cout<< M;
    in this code, my vector size becomes 20; but there is not 20 columns. How can I know define that? I have to reach the vector elements by using like cc[M-3], cc[M-1]...

  12. #12
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    my vector size becomes 20; but there is not 20 columns.
    Then why do you tell the loop to add 20 columns to the vector? Try this:
    Code:
    while( terr>>number){
      cc.push_back(number);
    }

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    23
    Thanks Noir, thank you all. now is working well.
    regards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. reading text file to struct help needed!
    By werdy666 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2009, 11:37 AM
  3. Reading in an array of text from a file?
    By suzakugaiden in forum C++ Programming
    Replies: 6
    Last Post: 01-04-2006, 03:17 PM
  4. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 07:13 PM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21