How can I open a file, skip first 2 lines and get the 50th character?

Discussion in 'C++' started by FrancisC, Oct 10, 2003.

  1. FrancisC

    FrancisC Guest

    How can I open a file, skip first 2 lines and get the 50th character?

    EXP 0 R:\000\un\111\e00\e00noLog\1006\bdry_arc.e00
    ARC 2
    1 1 0 0 0 0 7

    i.e., I want to get the "7" in the third line, how can I do that?

    thx!!
    FrancisC, Oct 10, 2003
    #1
    1. Advertising

  2. FrancisC

    Moonlit Guest

    Hi,

    #include <fstream>
    #include <string>
    using namespace std;

    int main()
    {
    int RetVal = 0;
    ofstream Input( "Filename" );
    if( !Input.is_open() )
    {
    // Could use exceptions, but since it is so small...
    RetVal = 1;
    }
    else
    {
    string Line;
    getline( Input, Line );
    getline( Input, Line );
    getline( Input, Line );
    cout << Input[ 49 ] << endl;
    }

    return RetVal;
    }

    TODO add some more error checking.

    Regards, Ron AF Greve.


    "FrancisC" <> wrote in message
    news:bm52r3$2vac$...
    > How can I open a file, skip first 2 lines and get the 50th character?
    >
    > EXP 0 R:\000\un\111\e00\e00noLog\1006\bdry_arc.e00
    > ARC 2
    > 1 1 0 0 0 0 7
    >
    > i.e., I want to get the "7" in the third line, how can I do that?
    >
    > thx!!
    >
    >
    Moonlit, Oct 10, 2003
    #2
    1. Advertising

  3. FrancisC

    Moonlit Guest

    Oops in my previous post that should be ifstream of course.

    Regards, Ron AF Greve.


    "FrancisC" <> wrote in message
    news:bm52r3$2vac$...
    > How can I open a file, skip first 2 lines and get the 50th character?
    >
    > EXP 0 R:\000\un\111\e00\e00noLog\1006\bdry_arc.e00
    > ARC 2
    > 1 1 0 0 0 0 7
    >
    > i.e., I want to get the "7" in the third line, how can I do that?
    >
    > thx!!
    >
    >
    Moonlit, Oct 10, 2003
    #3
  4. "FrancisC" <> writes:

    > How can I open a file, skip first 2 lines and get the 50th character?
    >
    > EXP 0 R:\000\un\111\e00\e00noLog\1006\bdry_arc.e00
    > ARC 2
    > 1 1 0 0 0 0 7
    >
    > i.e., I want to get the "7" in the third line, how can I do that?


    The 7 is the 70th character on this line, not the 50th.

    Some things are better done with standard unix tools than reinvented:

    head -3 bla | tail -1 | cut -b 50

    kind regards
    frank

    --
    Frank Schmitt
    4SC AG phone: +49 89 700763-0
    e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
    Frank Schmitt, Oct 10, 2003
    #4
  5. "FrancisC" <> wrote in message news:<bm52r3$2vac$>...
    > How can I open a file, skip first 2 lines and get the 50th character?


    std::ifstream in("file.name");
    std::istreambuf_iterator<char> beg(in), end;

    beg = std::find(std::find(beg, end, '\n'), end, '\n');
    for (int i = 0; i < 50 && beg != end; ++beg)
    ;
    if (beg != end)
    std::cout << "the 50th character on the third line is '"
    << *beg << "'\n";
    else
    std::cout << "some error occured while looking for the character\n";
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
    Dietmar Kuehl, Oct 10, 2003
    #5
  6. FrancisC

    Moonlit Guest

    Hi

    Now without the typo's ;-)

    #include <fstream>
    #include <string>
    using namespace std;

    int main()
    {
    int RetVal = 0;
    ifstream Input( "Filename" );
    if( !Input.is_open() )
    {
    // Could use exceptions, but since it is so small...
    RetVal = 1;
    }
    else
    {
    string Line;
    getline( Input, Line );
    getline( Input, Line );
    getline( Input, Line );
    cout << Line[ 49 ] << endl;
    }

    return RetVal;
    }
    Moonlit, Oct 10, 2003
    #6
  7. FrancisC

    Default User Guest

    Re: How can I open a file, skip first 2 lines and get the 50thcharacter?

    FrancisC wrote:
    >
    > How can I open a file, skip first 2 lines and get the 50th character?




    I recommend you decide which language you are working in. This same
    question appeared in comp.lang.c.




    Brian Rodenborn
    Default User, Oct 10, 2003
    #7
  8. FrancisC

    Howard Guest

    "FrancisC" <> wrote in message
    news:bm52r3$2vac$...
    > How can I open a file, skip first 2 lines and get the 50th character?
    >
    > EXP 0 R:\000\un\111\e00\e00noLog\1006\bdry_arc.e00
    > ARC 2
    > 1 1 0 0 0 0 7
    >
    > i.e., I want to get the "7" in the third line, how can I do that?
    >
    > thx!!
    >
    >


    one way:

    1) Open the file for reading
    2) Read the first line into a string
    3) Read the next line into the same string
    4) Read the third line into the same string
    5) The character you want is in the string...just use it! (i.e.,
    myString[49])

    -Howard
    Howard, Oct 10, 2003
    #8
    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. FrancisC
    Replies:
    16
    Views:
    726
  2. scad
    Replies:
    4
    Views:
    942
    James Kanze
    May 28, 2009
  3. Richard Schneeman
    Replies:
    16
    Views:
    460
    Daniel Bush
    Aug 27, 2008
  4. Replies:
    5
    Views:
    213
    Nico Coetzee
    Jul 8, 2005
  5. Jie
    Replies:
    6
    Views:
    144
    Peter Makholm
    Jun 28, 2007
Loading...

Share This Page