beginner question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jvax

    beginner question

    Hi all,

    I hope I'm posting in the right NG...
    I have a data text file I want to read from a c++ program.
    the data file goes like this:

    90 # number of balls
    33
    42
    13
    45
    23
    ...
    ...

    I want to store the number in the first line in a variable ommitting
    the following comment, then scan the rest and store it in an array.
    The array I could get it done with no problem. The first line is a
    problem. When I delete the "# number of balls" comment, the following
    code works fine, but I need to have the comment after the number.
    here's my code so far:

    while (!valid) {
    cout << "Name of test file: ";
    cin >> filename;
    #define TESTER_FILE filename
    if ((tester = fopen(TESTER_FI LE, "r")) == NULL)
    cout << "Please enter a valid file." << endl;
    else
    valid = 1;
    }
    valid = 0;
    fclose(tester);
    cout << endl;
    int try_with_same_d ata = 1;
    while (try_with_same_ data) {
    ifstream Test_file (TESTER_FILE, ios::in);
    Test_file >> BinSize;
    ...
    ...
    }

    How can I scan the first line, get the number and then go to next
    line?

    thanks for any help.

    --
    jvax
  • Jim Fischer

    #2
    Re: beginner question

    jvax wrote:[color=blue]
    > Hi all,
    >
    > I hope I'm posting in the right NG...
    > I have a data text file I want to read from a c++ program.
    > the data file goes like this:
    >
    > 90 # number of balls
    > 33
    > 42
    > 13
    > 45
    > 23
    > ..
    > ..
    >
    > I want to store the number in the first line in a variable ommitting
    > the following comment,[/color]

    // Read the first number into the variable 'count'
    in >> count;

    // Ignore everything up to and including the next newline
    // character '\n'. [n.b. In this example, each line of input
    // is delimited by a newline character.]
    in.ignore(std:: numeric_limits< streamsize>::ma x, '\n');

    --
    Jim

    To reply by email, remove "link" and change "now.here" to "yahoo"
    jfischer_link58 09{at}now.here. com


    Comment

    • Acacia

      #3
      Re: beginner question

      Call that beginner? If thats beginner......I 'm a complete virgin.

      "jvax" <jvax@hotmail.c om> wrote in message
      news:b77b55da.0 309010957.4d898 48e@posting.goo gle.com...[color=blue]
      > Hi all,
      >
      > I hope I'm posting in the right NG...
      > I have a data text file I want to read from a c++ program.
      > the data file goes like this:
      >
      > 90 # number of balls
      > 33
      > 42
      > 13
      > 45
      > 23
      > ..
      > ..
      >
      > I want to store the number in the first line in a variable ommitting
      > the following comment, then scan the rest and store it in an array.
      > The array I could get it done with no problem. The first line is a
      > problem. When I delete the "# number of balls" comment, the following
      > code works fine, but I need to have the comment after the number.
      > here's my code so far:
      >
      > while (!valid) {
      > cout << "Name of test file: ";
      > cin >> filename;
      > #define TESTER_FILE filename
      > if ((tester = fopen(TESTER_FI LE, "r")) == NULL)
      > cout << "Please enter a valid file." << endl;
      > else
      > valid = 1;
      > }
      > valid = 0;
      > fclose(tester);
      > cout << endl;
      > int try_with_same_d ata = 1;
      > while (try_with_same_ data) {
      > ifstream Test_file (TESTER_FILE, ios::in);
      > Test_file >> BinSize;
      > ...
      > ...
      > }
      >
      > How can I scan the first line, get the number and then go to next
      > line?
      >
      > thanks for any help.
      >
      > --
      > jvax[/color]


      Comment

      • Stuart Golodetz

        #4
        Re: beginner question

        "jvax" <jvax@hotmail.c om> wrote in message
        news:b77b55da.0 309010957.4d898 48e@posting.goo gle.com...[color=blue]
        > Hi all,
        >
        > I hope I'm posting in the right NG...
        > I have a data text file I want to read from a c++ program.
        > the data file goes like this:
        >
        > 90 # number of balls
        > 33
        > 42
        > 13
        > 45
        > 23
        > ..
        > ..
        >
        > I want to store the number in the first line in a variable ommitting
        > the following comment, then scan the rest and store it in an array.
        > The array I could get it done with no problem. The first line is a
        > problem. When I delete the "# number of balls" comment, the following
        > code works fine, but I need to have the comment after the number.
        > here's my code so far:
        >
        > while (!valid) {
        > cout << "Name of test file: ";
        > cin >> filename;
        > #define TESTER_FILE filename
        > if ((tester = fopen(TESTER_FI LE, "r")) == NULL)
        > cout << "Please enter a valid file." << endl;
        > else
        > valid = 1;
        > }
        > valid = 0;
        > fclose(tester);
        > cout << endl;
        > int try_with_same_d ata = 1;
        > while (try_with_same_ data) {
        > ifstream Test_file (TESTER_FILE, ios::in);
        > Test_file >> BinSize;
        > ...
        > ...
        > }
        >
        > How can I scan the first line, get the number and then go to next
        > line?
        >
        > thanks for any help.
        >
        > --
        > jvax[/color]

        #include <algorithm>
        #include <fstream>
        #include <iostream>
        #include <sstream>
        #include <string>
        #include <vector>

        int main()
        {
        std::vector<int > numVec;
        std::string sTemp;
        std::ifstream fs("Test5.txt") ; // put your filename here

        std::stringstre am ss;
        while(std::getl ine(fs, sTemp))
        {
        ss << sTemp;
        int iTemp;
        ss >> iTemp;
        numVec.push_bac k(iTemp);

        ss.str("");
        ss.clear();
        }

        // Output numbers in the format "1 2 3 4 ..."
        std::copy(numVe c.begin(), numVec.end(),
        std::ostream_it erator<int>(std ::cout, " "));

        return 0;
        }

        HTH,

        Stuart.


        Comment

        Working...