Computing.Net > Forums > Programming > swap lines of text in a text file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

swap lines of text in a text file

Reply to Message Icon

Name: peterpan
Date: February 19, 2009 at 05:43:45 Pacific
OS: Windows 2000 SP4
Subcategory: C/C++
Comment:

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?



Sponsored Link
Ads by Google

Response Number 1
Name: reno
Date: February 19, 2009 at 06:08:36 Pacific
Reply:

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.


0

Response Number 2
Name: peterpan
Date: February 19, 2009 at 08:59:31 Pacific
Reply:

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 TextStream

Open "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$
Wend

Close #1
End Sub

I dont mind vb6, c++ or in a batch file. Thanks


0

Response Number 3
Name: wizard-fred
Date: February 19, 2009 at 14:48:26 Pacific
Reply:

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


0

Response Number 4
Name: peterpan
Date: February 19, 2009 at 15:12:06 Pacific
Reply:

Line A is always directly on top of Line B, like below:

0PW222222PeterPiper PIP Peterpan, Panland
0PW222222PeterPaner PAN Peterpan, Panland

When 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, Panland

There are around 1000 lines in the file that these lines are present in. Thank you for your response Fred


0

Response Number 5
Name: wizard-fred
Date: February 19, 2009 at 18:12:11 Pacific
Reply:

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 file

while 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:
wend

close #2 output
close #1 input



0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: February 19, 2009 at 20:32:25 Pacific
Reply:

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


0

Response Number 7
Name: wizard-fred
Date: February 20, 2009 at 00:04:09 Pacific
Reply:

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.


0

Response Number 8
Name: reno
Date: February 20, 2009 at 00:55:16 Pacific
Reply:

here is a hint: (tested on vb6)

read file:

    ff = FreeFile
    Open filename For Input As ff
    SwitchString = Input$(LOF(ff), #ff)
    Close ff

writefile:

    ff = FreeFile
    Open filename For Output As ff
    Print #ff, SwitchString
    Close ff

Replace function

    SwitchString = Replace(SwitchString, Text2, Text1)


0

Response Number 9
Name: peterpan
Date: February 20, 2009 at 01:29:36 Pacific
Reply:

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.


0

Response Number 10
Name: wizard-fred
Date: February 20, 2009 at 03:01:12 Pacific
Reply:

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.


0

Response Number 11
Name: Mechanix2Go
Date: February 20, 2009 at 03:47:42 Pacific
Reply:

"I'm a 40 year user of BASIC"

Did you invent it?

:(


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 12
Name: peterpan
Date: February 20, 2009 at 04:15:59 Pacific
Reply:

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?


0

Response Number 13
Name: reno
Date: February 20, 2009 at 04:27:25 Pacific
Reply:


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


0

Response Number 14
Name: wizard-fred
Date: February 20, 2009 at 10:51:46 Pacific
Reply:

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.


0

Response Number 15
Name: peterpan
Date: February 20, 2009 at 14:50:41 Pacific
Reply:

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.


0

Response Number 16
Name: wizard-fred
Date: February 20, 2009 at 15:24:24 Pacific
Reply:

Use the ascii value of the character. Print
chr$(nn).


0

Response Number 17
Name: peterpan
Date: March 4, 2009 at 03:15:28 Pacific
Reply:

thank you for your help, finally got it working.


0

Response Number 18
Name: peterpan
Date: March 5, 2009 at 08:33:12 Pacific
Reply:

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


0

Response Number 19
Name: reno
Date: March 5, 2009 at 09:16:32 Pacific
Reply:

1. vbnewline
2. vbcrlf
3. chr(13)&chr(10) or is it chr(10)&chr(13)

feel free to choose


0

Response Number 20
Name: peterpan
Date: March 9, 2009 at 05:20:04 Pacific
Reply:

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


0

Response Number 21
Name: reno
Date: March 9, 2009 at 06:42:34 Pacific
Reply:

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$ & "'"


0

Response Number 22
Name: peterpan
Date: March 9, 2009 at 07:26:27 Pacific
Reply:

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


0

Response Number 23
Name: reno
Date: March 9, 2009 at 09:21:21 Pacific
Reply:

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:\"


0

Response Number 24
Name: peterpan
Date: March 10, 2009 at 06:02:04 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: swap lines of text in a text file

Batch file to extract lines of text from .ini www.computing.net/answers/programming/batch-file-to-extract-lines-of-text-from-ini/19089.html

splitting a text file www.computing.net/answers/programming/splitting-a-text-file/14804.html

Batch to del first x lines of text www.computing.net/answers/programming/batch-to-del-first-x-lines-of-text/16887.html