seekg in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • icniVaD
    New Member
    • Aug 2007
    • 1

    #1

    seekg in C++

    Hi,

    I'm quite new to C++ and I'm trying to figure out how to do something, I hope someone can help. Basically, I have a text file which I want my program to open. This has a number of data entries in it but I'm only concerned with a couple.

    A few lines start with 'EXP_SHOT_MAP' and I want to seek as far as the first occurance of this string. I then want to seek along that line to '('. There's loads of ('s so I need to go to EXP_SHOT_MAP first. I then want to store the first character after '('. Presumably using getchar().

    How would i do this?
  • ahoyer
    New Member
    • Aug 2007
    • 14

    #2
    Originally posted by icniVaD
    Hi,

    I'm quite new to C++ and I'm trying to figure out how to do something, I hope someone can help. Basically, I have a text file which I want my program to open. This has a number of data entries in it but I'm only concerned with a couple.

    A few lines start with 'EXP_SHOT_MAP' and I want to seek as far as the first occurance of this string. I then want to seek along that line to '('. There's loads of ('s so I need to go to EXP_SHOT_MAP first. I then want to store the first character after '('. Presumably using getchar().

    How would i do this?
    Im not gonna write out the code for you, but some functions from c that you may want to look at are fopen, fclose, fgets, sscanf, strncmp.

    fopen opens a file and assigns it to a file pointer (FILE *) , this can then be used by other functions. fclose is just used to clean up open files, you pass it an open FILE * and it will close it for you.

    fgets will retrieve the actualy lines from your file and store them into variables (char *), you can then use sscanf to parse those lines and retrieve certain values. strncmp can then be used to check those values that you pulled out of those lines.

    both of these functions are part of stdio.h, and im sure there are c++ equivalent functions out there, but this is at least a start.

    Hope that helps!

    Comment

    • RRick
      Recognized Expert Contributor
      • Feb 2007
      • 463

      #3
      Seekg is only useful when you know where you want to go. In your example, you don't know where these strings start, so you're going to have to read the file one character or line at a time.

      I realize this is a C++ forum, but there are other ways to find these values. Perl is one of the best languages for finding data within files, especially text files. Perl has a great regular expression parser that is fast and versatile.

      Typically, you use Perl to parse out the data you need and write it to a file. Then your C/C++ program reads the special file with just the data it needs.

      Comment

      Working...