473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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?
Aug 29 '07 #1
2 2573
ahoyer
14
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!
Aug 29 '07 #2
RRick
463 Expert 256MB
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.
Aug 30 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Kapil Khosla | last post by:
Hi, I am trying to move the file pointer back to the original location, but not able to using seekg, can someone help ? Thanks, Kapil #include "stdafx.h" using std::cerr; using std::endl;...
1
by: wtnt | last post by:
Hello. I've searched all over and haven't seen another thread with this problem. Please bear with me as I try to explain. thanks. :) I have some programs that need to be cross-platform...
4
by: Dylan | last post by:
Hi again, In the following program I expected step 3 to assign the values 1 and 2 to iVal1 and iVal2 yet it appears ss.seekg(0, std::ios::beg) does not move the read position back to the...
2
by: Brian Ronk | last post by:
I'm having a strange error. I have a Debian stable server and I'm using gcc 3.3, but it looks like I have 3.4 installed as well. I recently made a change to a program that takes data from two...
6
by: Tom | last post by:
Given: A binary data file of records. Task: Random access using seekg(). I've never found any documentation stating that the offset from begin file marker is safe on a fragmented file. I've...
2
by: Assertor | last post by:
Hi, All. (VC++6.0) I found some strange thins when using getline() and seekg() of std::ifstream. After the file position of an open file was shift to the end of the file, seekg() did not...
1
by: pantatkau | last post by:
i am trying to use the seekg(offset, base) function to move the read position but cant seem to do it correctly. When i use tellg() to know if the seekg operation is successful or not , the tellg()...
3
by: nomad5000 | last post by:
Hi I'm trying to read a textfile and print it's content twice to the std::cout but it won't work the second time. I think it has something to do with the seekg(0) command but I don't know. here is...
12
by: Julian | last post by:
Hi, I am having problems with a function that I have been using in my program to read sentences from a 'command file' and parse them into commands. the surprising thing is that the program works...
1
by: fmufti | last post by:
I would like to check if the f_count is a valid position for seekg in the file or not. What I mean is if I pass a position to seekg and that does not exist in the file then e.g if...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.