Thread: extracting numbers using ifstream

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    2

    Question extracting numbers using ifstream

    Please forgive me if this is a stupid question . . . I am a new programmer and I've read through a "C++ For Beginners" book, but I'm obviously not an expert.

    I'm trying to write a program that will: 1) access my file that has data in it, 2) extract the numbers that I need from this file, and 3) put the numbers into a column in another file. I've tried to follow examples in my C++ book to do this, but it just seems like it should be more simple than what I've done:

    char lambda[(int)1000][(int)1000];

    ifstream input("betalogfile",ios::binary | ios::in);

    input.unsetf (ios::skipws);

    ofstream output("betalogfile.dat");

    int l = 0;

    double pos4 = 782; // this number is the cursor position in my file of the first "digit" of the first number that I need to extract

    do
    {
    i = 0;
    do
    {
    input.seekg((pos4 + i),ios::beg);
    input >> lambda[i][l];
    output << lambda[i][l];
    i++;
    }while(i<12); // the numbers I want to extract is 11 digits long
    output << "\n";
    pos4 = pos4 + 1432; // this number adds on the number of spaces from the first "digit" of the previous number to the first "digit" of the next number I need.
    l++; }while(l<N); // N is the number of numbers I need to extract and put into a column

    input.close();
    output.close();

    This outputs all of the numbers I need to a column in a separate file. The problem is, that I need to add other numbers to these numbers and treat them as numbers, but I don't know how to extract the numbers more than one digit at a time. Is there a way to do that? Thank you for reading this and especially if you can answer (or understand) my questions.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    68

    You have to specify the numbers

    Is there any algo that u are going to use to specify the number or are u going to calculate as soon as you get from the file. Can you please Attach the data file and be more specific about what u need. Sorry but i don't see where u are getting at....

    Thanks

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    2
    I've attached the file that I want my program to open up and get numbers out of. For example, the first number that I want the program to extract is:
    4860.6245
    This number is on the 27th line on the left side. Then, the second number I want to extract is on the 73rd line:
    4860.6318
    and so on with a similar pattern until I pick up all 166 of these numbers.

    I hope this clears up what I meant a little bit. I'm not sure what you mean by "algo" so hopefully this answers your questions. I appreciate any help that you can give.

    ps. I had to chop off most of the file that I am attaching because it was too large, but hopefully you will get the idea from what is there. What I cut off is just more of the same.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
       int i, j;
       double lambda[N];
       char line[130];
    
       ifstream input("betalogfile.txt");
       ofstream output("betalogfile.dat");
    
       for (j=0; j<26; j++)
          input.getline(line,130);
       input >> lambda[0];
       output << lambda[0] << endl;
       for (i=1; i<N; i++)
       {
          for (j=0; j<36; j++)
             input.getline(line,130);
          input >> lambda[i];
          output << lambda[i] << endl;
       }
    
       input.close();
       output.close();
    I used an array, but if the value is only outputted to a file, the array isn't needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  2. trying to reverse numbers with sizeof operator
    By JoelearningC in forum C Programming
    Replies: 13
    Last Post: 03-09-2008, 11:53 AM
  3. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM