CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2010

    seekg function --- plese help

    hi all
    this is my code

    #include <iostream>
    #include <fstream>
    using namespace std;
    int main () {
    	char a ;
    	int length ;
    	ifstream infile ;
    	ofstream outfile ;
    	infile.open("hm.txt",ios::in) ;
    	outfile.open("output.txt" , ios::out);
    int a1;  
    cout << " please enter the ID " <<endl; 
    cin>> a1; 
    	infile>> a ;
    	while ( ! infile.fail())
    		outfile<< a;
    		infile >> a ;
    i have this text file with users and if from 1 to 10 , and all the name is in same length

    1 ken1
    2 ken2
    3 ken3
    4 ken4
    5 ken5
    6 ken6
    7 ken7
    8 ken8
    9 ken9
    the user should enter an ID from 1 to 10 and then the app should search for the id and if it found some thing it should print the user of the ID without without id ..

    how i can do it ?????

    thanks in advance

  2. #2
    Join Date
    Jun 2010

    Re: seekg function --- plese help

    Your names do not all have the same length unless the kens before ken10 have a space at the end of the line which of course is invisible in the listing.

    AFAIK seekg() does not work reliably with files opened in text mode, so you may want to open the file in binary mode. However, in binary mode the >> operator may not work correctly, so you may need to use read() instead.

    To get the offset to seek to, you need to multiply the ID minus 1 (assuming one-based IDs) with the record length. The record length in this file is 9 (assuming Windows line breaks and one space at the end of each line containing a ken before 10).

    EDIT: Oh, and of course a char is not enough to hold all possible IDs (the ID may be 10) or even an entire record from the file.
    Last edited by Eri523; October 7th, 2011 at 03:32 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)