Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving the pointer many lines within a TXT file

Status
Not open for further replies.

Riegg

Programmer
Mar 20, 2003
29
CA
I need to move the pointer many lines within a text file.

Example:

test.txt
-------------------
Bob
26
Programer
John
30
User
Kelly
44
Boss
-------------------

Say I want to replace entry two's info. There will always be 3 lines for each entry but the names/ages/job titles will never be the same. fseek only moves me one character at a time and i don't know the length of each line. I know how to use the fput command I just don't know how to move the pointer "x" number lines.
 
If you are reading a standard text file that is opened with the TEXT argument on the fopen command, then you can use the fgets command to read the contents of the file line by line. Since you aren't interested in the contents (up to a certain point at least) you don't need to worry about saving that data.

I have an example at that may help you a bit. One difference between that script and your situation is that you are adding strings with unknown (when you're writing the script) lengths. If you look at the sample above, the line finsblock 1 9 would be replaced by something like finshblock 1 iLen. iLen would be an integer value in your script that holds the value of the string you are about to write, this value being found with the strlen command. aspect@aspectscripting.com
 
Well... that saves me from beating my head against my screen. :) I was hoping for a better way to do it. Oh well I will have it do the fgets to the spot that I need to change. At least I have a integer var for the location I am looking for so I can just do a while loop till that number comes up. Thanks!!
 
Hello,

I've done some work with the Fseek Family of Commands. Questions: Do the End of the Three Records always end with Programmer, User and Boss ? How Long ( Number of Records in the File ) is the File. Let me know and I'll see what I can come up with.

Hank
 
Hello,

Here's a Thought....

Use some of the Sring Commands to Change the Original:

Bob
26
Programer
John
30
User
Kelly
44
Boss

To:

Bob 26 Programer
John 30 User
Kelly 44 Boss

Then do a Strreplace Command to Sub the Old to New Values and then reformat the File and Output it in a Single Column as the Original File ?? Just a Thought !!

Hank

 
Hank,

The file is for "meta key" information for the script I am working on. I have 40 soft keys that the user can define with commands that can have popup dialogs within them. I want to have it so the user can edit one button and have the information saved back to the same file so that it can be reloaded at a later date.

It will contain the following information and there will be 40 buttons worth of data:

Button Label (Text)
First part of the command (Text)
Set Popup Box 1 (1 or 0)
Middle part of the command (Text)
Set Popup Box 2 (1 or 0)
End of the command (Text)
Set Popup Box 3 (1 or 0)
Add a ^M to the end of the command (1 or 0)

(The example I gave was just a simple sort of thing to get my point across. Sorry there is no Boss, User, or Programer :) )

I am doing somthing like this right now: (ignore all var type i.e. string, integer....) If you can think of a better way please tell me.

; event: the button selected to edit
; 8: because there are 8 records for each entry
; - 7: to get me to the first line of the record

fopen 0 "metakey.txt"
fseek 0 0 0
if currentlocation < (event * 8) - 7
fgets 0 linebuffer
currentlocation++
endif
fputs 0 label
fputs 0 command1
fputs 0 popup1
fputs 0 command2
.
.
.
fputs 0 addenter
fclose 0










 
What I can do is a strcmp on the old button label to seach through the file. I can save the old value before the edit dialog box comes up. Hummmmm......
 
The fgets to &quot;null&quot; seems to work very well. It's quick and dirty. The only things that I had to change was from subtract 7 to subtract 8 lines and it brings me to the correct line, and I had to add:

if feof
fputs &quot;&quot;
endif

So that it would add &quot;&quot; to the lines that do not have anything and my button info would be in the correct place in the file.

Thanks for letting me bounce ideas off you guys.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top