Read from txt file

Discussion in 'C++' started by JrdA, Jan 26, 2004.

  1. JrdA

    JrdA Guest

    Hi NG

    My programs does following:

    - ofstream writes fx. "1234#01/26/04#" to a txt-file

    Now i have a txt-file containing :

    1234#01/26/04#
    424#01/26/04#
    324234#01/26/04#

    I then need ifstream to read the first ints until it reaches the # sign and
    then add them togheter.

    Can anyone help me with som code to do that?

    Thanks
    AHR
     
    JrdA, Jan 26, 2004
    #1
    1. Advertising

  2. "JrdA" <> wrote...
    > My programs does following:
    >
    > - ofstream writes fx. "1234#01/26/04#" to a txt-file
    >
    > Now i have a txt-file containing :
    >
    > 1234#01/26/04#
    > 424#01/26/04#
    > 324234#01/26/04#
    >
    > I then need ifstream to read the first ints until it reaches the # sign

    and
    > then add them togheter.
    >
    > Can anyone help me with som code to do that?


    The usual approach is to read the entire line into a string object,
    then extract the "field" you need (in your case all symbols until
    the first '#' into a separate string) and then work with it (in your
    case convert it into an int).

    There are other solutions, but this one is the most generic.

    To read an entire line use 'std::getline' function. To extract the
    substring use 'substr' member of std::string. To convert a string
    into a number use 'std::istringstream' and its operator >>.

    Victor
     
    Victor Bazarov, Jan 26, 2004
    #2
    1. Advertising

  3. JrdA

    Jon Bell Guest

    In article <401543ba$0$95068$>,
    JrdA <> wrote:
    >
    >Now i have a txt-file containing :
    >
    >1234#01/26/04#
    >424#01/26/04#
    >324234#01/26/04#
    >
    >I then need ifstream to read the first ints until it reaches the # sign and
    >then add them togheter.


    I assume you don't need to do anything with the rest of the data on each
    line.

    When you read an int using >>, it will stop reading when it reaches the
    first character that cannot be part of an int, so it will stop at the
    first # sign. Now now you need to skip over the rest of the line, which
    you can do with ignore().

    ifstream input ("your.file");
    int num;
    while (input >> num)
    {
    // do whatever you need to do with num here

    input.ignore (1000, '\n'); // skips past next newline, or 1000 chars,
    // whichever comes first
    }

    Depending on the maximum posslbie line length in your file, adjust the
    1000 if necessary.

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
     
    Jon Bell, Jan 26, 2004
    #3
  4. JrdA wrote:
    >
    > Hi NG
    >
    > My programs does following:
    >
    > - ofstream writes fx. "1234#01/26/04#" to a txt-file
    >
    > Now i have a txt-file containing :
    >
    > 1234#01/26/04#
    > 424#01/26/04#
    > 324234#01/26/04#
    >
    > I then need ifstream to read the first ints until it reaches the # sign and
    > then add them togheter.
    >
    > Can anyone help me with som code to do that?


    I would do it the following way:

    read an entire line into a std::string using getline
    then search if and where there is a '#' character.
    Split the string into 2 using the found position
    Use the left half in a stringstream to use an op>> to read the int.

    Now that you have that int, use it for whatever you like to do with it.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jan 26, 2004
    #4
  5. JrdA

    Mike Wahler Guest

    "JrdA" <> wrote in message
    news:401543ba$0$95068$...
    > Hi NG
    >
    > My programs does following:
    >
    > - ofstream writes fx. "1234#01/26/04#" to a txt-file
    >
    > Now i have a txt-file containing :
    >
    > 1234#01/26/04#
    > 424#01/26/04#
    > 324234#01/26/04#
    >
    > I then need ifstream to read the first ints until it reaches the # sign

    and
    > then add them togheter.
    >
    > Can anyone help me with som code to do that?


    #include <cstdlib>
    #include <fstream>
    #include <iomanip>
    #include <iostream>
    #include <sstream>
    #include <string>

    int main()
    {
    std::ifstream input("filename");
    if(!input)
    {
    std::cerr << "Cannot open input\n";
    return EXIT_FAILURE;
    }

    int sum(0);
    std::string line;
    const std::streamsize width(10);
    std::streamsize line_no(0);
    bool err(false);

    while(std::getline(input, line))
    {
    ++line_no;
    int value(0);
    std::istringstream iss(line);

    if(!(iss >> value))
    {
    std::cerr << "Line " << line_no << ": "
    << "First field not a valid integer, skipped\n";

    err = true;
    }
    else
    {
    std::cout << " + " << std::setw(width) << value << '\n';
    sum += value;
    }
    }

    std::cout << " " << std::string(width, '-') << '\n'
    << " = " << std::setw(width) << sum << '\n';

    if(!input && !input.eof() || err)
    std::cerr << "(Error(s) occurred reading input)\n";

    return 0;
    }

    -Mike
     
    Mike Wahler, Jan 26, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ruca

    Read txt File to a DataSet

    ruca, Feb 26, 2004, in forum: ASP .Net
    Replies:
    15
    Views:
    8,024
    Paul Clement
    Feb 27, 2004
  2. Sameen
    Replies:
    2
    Views:
    483
    Victor Bazarov
    Aug 29, 2005
  3. King

    Read .txt file like .py file

    King, Jul 27, 2008, in forum: Python
    Replies:
    2
    Views:
    264
  4. Jochen Brenzlinger
    Replies:
    7
    Views:
    5,874
    Roedy Green
    Sep 15, 2011
  5. Ram
    Replies:
    3
    Views:
    283
    Tad McClellan
    Apr 26, 2007
Loading...

Share This Page