Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I have a text file that has hundreds of lines of text. I need to read all the lines in the text file and then switch the 2 lines around.
So i have Text A and Text B in this text file called peters.txt that has many lines of text. I want to put Text B in place of Text A and Text A in place of Text B.
The example text i want it to swap is the lines below:
0PW222222PeterPiper PIP Peterpan, Panland
0PW222222PeterPaner PAN Peterpan, Panland
I want it to switch it in the same file like below so the second line is where the first line was and vice versa:
0PW222222PeterPaner PAN Peterpan, Panland
0PW222222PeterPiper PIP Peterpan, Panland
Can you help please?

TRIPLE POST!!!!!!!!! please be patient, you are flooding the forum.
if you really want to do it in C/C++, then its probably school assignment. i cant help you to graduate with no skill.
if not, then..
use notepad. click Edit-Replace:
1. Replace TEXTA->TEMP
2. Replace TEXTB->TEXTA
3. Replace TEMP ->TEXTB
done.

Hi Sorry,
Its not a school assignment.
I need to automate it so i can add the command for it to run from a batch file so when the other procedures are run, i can just add this one in as it needs to be done every 2 week cycles.
So i cannot use notepad, I have had a try in vb6 and C++ but have failed, i am a newcomer trying to learn but cannot figure it out. The data provided is dummy data.
My try in vb6 is displayed below:
Private Sub Command1_Click()
Dim sTemp1
Dim sTemp2
sTemp1 = "0PW222222PeterPiper PIP Peterpan, Panland"
sTemp2 = "0PW222222PeterPaner PAN Peterpan, Panland"Dim sTemp$
Dim fso As New FileSystemObject
Dim fsoTS As TextStreamOpen "C:\tester\testnew\test.txt" For Input As #1
While Not EOF(1)
If sTemp$ = sTemp1 Then
'not sure what goes here
End If
Line Input #1, temp$
WendClose #1
End SubI dont mind vb6, c++ or in a batch file. Thanks

Are the two lines adjacent to each other?
or can they be spaced n lines apart.
Does line A always come before line B?

Line A is always directly on top of Line B, like below:
0PW222222PeterPiper PIP Peterpan, Panland
0PW222222PeterPaner PAN Peterpan, PanlandWhen i run the exe i would be looking for it switch the lines like below so they are both present but the other way around:
0PW222222PeterPaner PAN Peterpan, Panland
0PW222222PeterPiper PIP Peterpan, PanlandThere are around 1000 lines in the file that these lines are present in. Thank you for your response Fred

If line b always follows line a:
pseudo code:
(I don't use VB but other free (open source)
BASICS)test$ = "find"
open #1 input file
open #2 output filewhile not eof(1)
line input #1, A$
if test$ = A$ then
hold$ = A$
line input #1, A$
print #2, A%
print #2, hold$
goto continue
endif
print #2, A$continue:
wendclose #2 output
close #1 input

If you're a newbir to all these languages, it would be a real good idea to go back to 'hello world' nf work your way along.
=====================================
If at first you don't succeed, you're about average.M2

hey M2. You're always hard at it with the batch. I'm a 40 year user of BASIC and this was a relatively easy problem that the OP started.

here is a hint: (tested on vb6)
read file:
ff = FreeFile Open filename For Input As ff SwitchString = Input$(LOF(ff), #ff) Close ffwritefile:
ff = FreeFile Open filename For Output As ff Print #ff, SwitchString Close ffReplace function
SwitchString = Replace(SwitchString, Text2, Text1)

Do i replace filename with the name of my file?I still cant get it to work, it keeps giving me an errors.
Thank you all for your responses, especially reno and fred.

I am using sequential input and output. The input is your file nqme. Output is a temp name. If you need the original name you have to delete input and rename output.

"I'm a 40 year user of BASIC"
Did you invent it?
:(
=====================================
If at first you don't succeed, you're about average.M2

Hi I have figured it out, thanks guys. I did it in vb6. How do i check if the output file already exists and if it does i want to delete it?

Public Function FileExist(ByVal path As String, Optional flag As Integer) As Boolean On Error GoTo ErrHandler FileExist = False If (flag = vbDirectory) Then path = Dir(path, vbDirectory) Else path = Dir(path) End If If Not IsNull(path) And path <> "" Then FileExist = True ExitHere: Exit Function ErrHandler: FileExist = False If Err <> 0 Then MsgBox "Error " & Err.Number & ":(FileExist) -->" & Err.Description, , "Error" Resume ExitHere End Function

M2 I first used basic on a tty terminal with punched tape for program backup. Before the PC on a HP mainframe time share system.

Hi, i have got both lines switched, and they should be separated by this ascii character: � I want to enter this in the InStr that i have that writes the switched line out, what do i need to do to add that in? Does anyone know? After googling it i am getting that it equals , how do i add that symbol into vb6 so it outputs that before the line is switched.

Hi, now i have another problem with the same swap, basically, the line symbol that separates the two lines seems to be a unix stlye carriage return that looks like a small rectangle, how can i insert this in either the vb string or even outside of the string. thanks

Hi, the one i finally used was vblf which displayed the correct symbol. Thanks for you reply reno.
Final problem to this application i have is that when i run the executable i have hard coded the file path in, where as i really want the user to insert the path in and then run the exe in that location. how do i do this, i have google it and have had some attempts but its not bringing up the cmd window to allow me to enter the path.
If Command$ <> Empty Then
'// we have a file to open
OpenFile Command$ '// call your openfile procedure
End If

better way is to have a button that open up a browse dialog, that call windows api "GetOpenFileNameA".
here is the declaration code:Declare Function GetOpenFileName Lib "comdlg32.dll" _ Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean
google for it.i am using the code originally written by Ken Getz with little modification.
SOURCE: http://www.mvps.org/access/api/api0...
i believe there is a wrapper code somewhere written for vb6, you just have to google it out.errr, btw, vb6 dont have access to cmd window.
something similar to cmd window is console api, similar but not same.as for command line argument:
i dont see any error in it. try msgbox it, eg. MsgBox "'" & Command$ & "'"

I dont want to open the file, I just want to set the path for the filename.
Pathname = "d:\peterpan.txt"
The pathname code is hardcoded as you can see, i just want the user to set the path as it is different on every system. Is there anyway i can do this without using a button control. thanks

hmm, i dont quite understand, maybe this one what you are looking for. i though you are looking for passing file name from command-line.
filename = app.path & "\peterpan.txt"or is it you are looking to get the pathname of "d:\peterpan.txt"?? so the result is "d:\"

The question i had was that i want to pass in the pathname and filename into the application as arguments when it is run from the command line.
the way i have done this is by using the command$ in my application, and then just adding the argument in the cmd window. thanks for your help it was most useful

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |