CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 1999
    Posts
    27

    Move get pointer in ifstream



    I declared a ifstream object to process IGES file in code. IGES file is a kind of text file which has 80 characters per

    line. I want to move get pointer to a specific line begining, as IGES file is fixed line length format, seekg function

    should be more efficient than ignore function. I know that newline character is translated to and from carriage

    return¨Clinefeed pairs in text mode, but I don't know seekg function thinks of newline character as one character or

    two characters. I tried following code:

    MyFile.seekg(82*nLine,ios::beg); //nLine is number of lines jumped over

    and:

    MyFile.seekg(81*nLine,ios::beg);

    respectively.

    It seemed that newline character is thought of as two characters in some files and one character in some other files.

    Why?

    I also tried the following code:

    MyFile.seekg(0,ios::beg);

    for(short i=0; i<nLine; i++)

    MyFile.ignore(100,'\n');

    int num = MyFile.gcount(); //this line only for check

    As a result the get pointer jumped to correct position in some files while stopped somewhere before correct position

    is reached in some other files, but in both case num always equals 81. Why?

    Your help is greatly appreciated.

  2. #2
    Join Date
    Apr 1999
    Posts
    24

    Re: Move get pointer in ifstream



    What about trying this?


    MyFile.setmode(filebuf::binary);

  3. #3
    Join Date
    May 1999
    Location
    Antwerp, Belgium
    Posts
    136

    Re: Move get pointer in ifstream



    Open your file in binary mode.

  4. #4
    Join Date
    Mar 1999
    Posts
    27

    Re: Move get pointer in ifstream



    Thank you for your help. I have followed your advice. But the problem remains as it was. My code fraction is:

    ifstream InFile;

    InFile.open( "filename", ios::in | ios::nocreate | ios::binary );

    if( !InFile )

    {

    cout << "Open file failed";

    return;

    }

    //The following 4 lines is intended to get number of lines

    // of Start Section of file, which is recorded in the last

    // line of file

    char str[10];

    InFile.seekg( -80, ios::end );

    InFile.getline( str, 10, 'G' );

    short nStartLine = atoi( str );

    //Then move get pointer to begining of the first line of

    //Global Section of file

    InFile.seekg( 82 * nStartLine, ios::beg );

    The result is correct for a.igs, for example, but the pointer jumps to the SECOND character of the first line of Global

    Section of b.igs, NOT the first character(assuming nStartLine equals 1 for both files). If modifying 82 to 81, the

    result is correct for b.igs, wrong for a.igs. I can't discover the difference between two files. Help me again please!



  5. #5
    Join Date
    May 1999
    Location
    Antwerp, Belgium
    Posts
    136

    Re: Move get pointer in ifstream



    Could you send me the files so i can take a look to them.


    Send it to [email protected], because i'm not at the office anymore this week.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured