For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic Syntax > March 2006 > creating text file useing open stat. #1 for random access









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author creating text file useing open stat. #1 for random access
Hemant_india

2006-03-05, 3:57 am

hi
I have a text file like ;
acNo name amt
acno=space(10)
name=space(25)
amt=space(13)
i want to collect this data in another text file
by using type statement
e.g
type mydata
acno as string*9
and so on
end type
please guide
--
hemu
Norm Cook

2006-03-05, 6:59 pm

You don't say how you want the data formatted in 'another text file'
Also, you state acno=space(10) and then say > acno as string*9 ???
Here's some air code (untested)

Private Type MyData
acno as string*10 '9 ??
custname as string*25 'avoid the use of 'name', it's reserved
amt as string*13
End Type
dim Rec as MyData
dim fNumIn as Long
dim fNumOut as Long
dim i as Long
dim NumberRecords as Long
fNumIn = FreeFile 'input handle
fNumOut = FreeFile 'output handle
'***change these paths to your text file paths
Open "c:\sometextfile.txt" for Random as fNumIn
Open "c:\someothertextfile.txt for Output as fNumOut
'the following call will fail if the the field lengths are wrong
NumberOfRecords = LOF(fNumIn)\Len(Rec)
For i = 1 To NumberOfRecords
Get#fNumIn, , Rec
'now print to the output file in the format you desire, perhaps
Print#fNumOut, Rec.acno
Print#fNumOut, Rec.custname
Print#fNumOut, Rec.amt
Print#fNumOut, ' blank space for ease of reading
Next
Close fNumIn
Close fNumOut

BTW, it is considered good newsgroup etiquette to
post your messages in plain text.

"Hemant_india" <Hemantindia@discussions.microsoft.com> wrote in message
news:4F21EEF9-C650-4920-8BAF-167B1EAECFB4@microsoft.com...
> hi
> I have a text file like ;
> acNo name amt
> acno=space(10)
> name=space(25)
> amt=space(13)
> i want to collect this data in another text file
> by using type statement
> e.g
> type mydata
> acno as string*9
> and so on
> end type
> please guide
> --
> hemu



Hemant_india

2006-03-06, 3:58 am

thanks Mr. Cook
actually my problem is ,
i receive a data in certain format and
the format is as i described, But sometimes in the field of "acno'" I receive
non nu,eric characters . therfore i declared (string *9)
secondly i do not want to store such data in any table.
I may be wrong but i want to manipulate such data e.g total of amt
i have tried what you suggested, but has not worked so far i don't know why
thanks
--
hemu


"Norm Cook" wrote:

> You don't say how you want the data formatted in 'another text file'
> Also, you state acno=space(10) and then say > acno as string*9 ???
> Here's some air code (untested)
>
> Private Type MyData
> acno as string*10 '9 ??
> custname as string*25 'avoid the use of 'name', it's reserved
> amt as string*13
> End Type
> dim Rec as MyData
> dim fNumIn as Long
> dim fNumOut as Long
> dim i as Long
> dim NumberRecords as Long
> fNumIn = FreeFile 'input handle
> fNumOut = FreeFile 'output handle
> '***change these paths to your text file paths
> Open "c:\sometextfile.txt" for Random as fNumIn
> Open "c:\someothertextfile.txt for Output as fNumOut
> 'the following call will fail if the the field lengths are wrong
> NumberOfRecords = LOF(fNumIn)\Len(Rec)
> For i = 1 To NumberOfRecords
> Get#fNumIn, , Rec
> 'now print to the output file in the format you desire, perhaps
> Print#fNumOut, Rec.acno
> Print#fNumOut, Rec.custname
> Print#fNumOut, Rec.amt
> Print#fNumOut, ' blank space for ease of reading
> Next
> Close fNumIn
> Close fNumOut
>
> BTW, it is considered good newsgroup etiquette to
> post your messages in plain text.
>
> "Hemant_india" <Hemantindia@discussions.microsoft.com> wrote in message
> news:4F21EEF9-C650-4920-8BAF-167B1EAECFB4@microsoft.com...
>
>
>

argusy

2006-03-06, 3:58 am

Hemu

What hasn't worked?
Getting the data from the file, or manipulating the 'amt'?
(I get the impression from "e.g total of amt" that you are running a
progressive total of 'amt' as it is pulled in from the data file)

We can help you further if you are manipulating "amt"

Argusy




Hemant_india wrote:

>thanks Mr. Cook
>actually my problem is ,
>i receive a data in certain format and
>the format is as i described, But sometimes in the field of "acno'" I receive
>non numeric characters . therfore i declared (string *9)
>secondly i do not want to store such data in any table.
>I may be wrong but i want to manipulate such data e.g total of amt
>i have tried what you suggested, but has not worked so far i don't know why
>thanks
>
>


Hemant_india

2006-03-11, 3:58 am

here is fragment of of file i'am trying to use
------
03000/000
3670000010000000 11032006
6640036700210Q 15376 UDAYSHANKAR KESKAR
4000390024009039 HSBCBOM 019641943872
000000038390020162957310472974851 HSBC
6640036700210 24421 RADHESHYAM SAXENA
4000390024009039 HSBCBOM 019698638542
000000014780020162975080472924629 HSBC
6640036700210 35040 KRISHNA NAND SHUKLA
4000390024009039 HSBCBOM 019142660526
000000021060020162921320472984391 HSBC
6640036700210 39114 PATIL VAMAN RAMCHAND
4000021074009129BSNL KYN KLYN-22009325102169401
000000008410020162891930471534041 BSNLK
6640036700210 45028 SHARAD HARI GHUGE
4000021074009129BSNL KYN KLYN-22107095102168043
000000026140020162891740471524646 BSNLK
6640036700211 CC-252 DARSHAN AUTO PARTS
4000021074009129BSNL KYN SRVI-281318 5102172807
000000012230020162896660471584384 BSNLK
how shoule i access this using open.........for ...random access
--
hemu


"argusy" wrote:

> Hemu
>
> What hasn't worked?
> Getting the data from the file, or manipulating the 'amt'?
> (I get the impression from "e.g total of amt" that you are running a
> progressive total of 'amt' as it is pulled in from the data file)
>
> We can help you further if you are manipulating "amt"
>
> Argusy
>
>
>
>
> Hemant_india wrote:
>
>
>

argusy

2006-03-11, 3:58 am

whoa, there!!!

I think I can see eight distinct "fields" for a repeating sequence
following your "accno","name", "amt" logic, if I start with a code, there's

alphanumcode1 - 5 or 6 character alphanumerics
name - up to 20 characters
numcode1 - 16 characters (all numbers with a leading "40000")
alphacode1 - 8 characters (some with a space after the 4th one)
alphanumcode2 - (some with a 10 char number, some with a 22
alphanumeric sequence)
numcode2 - 33 characters (all numbers preceded with seven zeros)
alphacode2 - 4 or 5 alpha characters
numcode3 - 13 character number

I think you will have to make sense out of this file first, before you
can extract any data

Argusy


Hemant_india wrote:

>here is fragment of of file i'am trying to use
>------
>03000/000
> 3670000010000000 11032006
>6640036700210Q 15376 UDAYSHANKAR KESKAR
>4000390024009039 HSBCBOM 019641943872
>000000038390020162957310472974851 HSBC
>6640036700210 24421 RADHESHYAM SAXENA
>4000390024009039 HSBCBOM 019698638542
>000000014780020162975080472924629 HSBC
>6640036700210 35040 KRISHNA NAND SHUKLA
>4000390024009039 HSBCBOM 019142660526
>000000021060020162921320472984391 HSBC
>6640036700210 39114 PATIL VAMAN RAMCHAND
>4000021074009129BSNL KYN KLYN-22009325102169401
>000000008410020162891930471534041 BSNLK
>6640036700210 45028 SHARAD HARI GHUGE
>4000021074009129BSNL KYN KLYN-22107095102168043
>000000026140020162891740471524646 BSNLK
>6640036700211 CC-252 DARSHAN AUTO PARTS
>4000021074009129BSNL KYN SRVI-281318 5102172807
>000000012230020162896660471584384 BSNLK
>how shoule i access this using open.........for ...random access
>
>


Hemant_india

2006-03-11, 3:58 am

hi argusy
actually the sequential contains ten different values
a) length 13 num b) length 18 num c) length 40 alpha
d) length 33 alph+num e) length 11 num f) length 9 num
g) length 11 num h) len 9 num i) len 9 num j) len 7 alpha
i have sent only two items as fragment
can you help ?

--
hemu


"argusy" wrote:

> whoa, there!!!
>
> I think I can see eight distinct "fields" for a repeating sequence
> following your "accno","name", "amt" logic, if I start with a code, there's
>
> alphanumcode1 - 5 or 6 character alphanumerics
> name - up to 20 characters
> numcode1 - 16 characters (all numbers with a leading "40000")
> alphacode1 - 8 characters (some with a space after the 4th one)
> alphanumcode2 - (some with a 10 char number, some with a 22
> alphanumeric sequence)
> numcode2 - 33 characters (all numbers preceded with seven zeros)
> alphacode2 - 4 or 5 alpha characters
> numcode3 - 13 character number
>
> I think you will have to make sense out of this file first, before you
> can extract any data
>
> Argusy
>
>
> Hemant_india wrote:
>
>
>

argusy

2006-03-11, 7:57 am



Hemant_india wrote:

>hi argusy
>actually the sequential contains ten different values
>a) length 13 num b) length 18 num c) length 40 alpha
>d) length 33 alph+num e) length 11 num f) length 9 num
>g) length 11 num h) len 9 num i) len 9 num j) len 7 alpha
>i have sent only two items as fragment
>can you help ?
>
>
>

so the data break-out is

6640036700210
15376
UDAYSHANKAR KESKAR
4000390024009039 HSBCBOM
01964194
3872 0000
00038390020
162957310
472974851
HSBC


6640036700210
24421
RADHESHYAM SAXENA
4000390024009039 HSBCBOM
01969863
8542 0000
00014780020
162975080
472924629
HSBC


6640036700210
35040
KRISHNA NAND SHUKLA
4000390024009039 HSBCBOM
01914266
0526 0000
00021060020
162921320
472984391
HSBC


6640036700210
39114
PATIL VAMAN RAMCHAND
4000021074009129BSNL KYN KLYN-220
09325102169
401 0000
00008410020
162891930
47153404
BSNLK


6640036700210
45028
SHARAD HARI GHUGE
4000021074009129BSNL KYN KLYN-221
07095102168
043 0000
00026140020
162891740
471524646
BSNLK


6640036700211
CC-252
DARSHAN AUTO PARTS
4000021074009129BSNL KYN SRVI-281
318 5102172
807 0000
00012230020
162896660
471584384
BSNLK

If that's the case then change Norm's program,
because to get the data you want, you have to get EVERYTHING.

Private Type MyData
field1 as string*13
field2 as string*18
custname as string*40
field3 as string*33
field4 as string*11
field5 as string*9
field6 as string*11
field7 as string*9
field8 as string*11
field9 as string*9
field10 as string*9
field11 as string*7
End Type

I'll assume field2 is the accno, and for the hell of it, field7 is the
amount

Norm's program then would also change to

Print#fNumOut, Rec.field2
Print#fNumOut, Rec.custname
Print#fNumOut, Rec.field7


Argusy


Hemant_india

2006-03-11, 7:57 am

hi argusy
while using norm's code system gives the message "file is in used"
i don't know why
i mean opening two files at a time
& you are right that's the amt
but i have no intentions to swindle any way
i will try norm's code again and will let you know
regards

--
hemu


"argusy" wrote:

>
>
> Hemant_india wrote:
>
> so the data break-out is
>
> 6640036700210
> 15376
> UDAYSHANKAR KESKAR
> 4000390024009039 HSBCBOM
> 01964194
> 3872 0000
> 00038390020
> 162957310
> 472974851
> HSBC
>
>
> 6640036700210
> 24421
> RADHESHYAM SAXENA
> 4000390024009039 HSBCBOM
> 01969863
> 8542 0000
> 00014780020
> 162975080
> 472924629
> HSBC
>
>
> 6640036700210
> 35040
> KRISHNA NAND SHUKLA
> 4000390024009039 HSBCBOM
> 01914266
> 0526 0000
> 00021060020
> 162921320
> 472984391
> HSBC
>
>
> 6640036700210
> 39114
> PATIL VAMAN RAMCHAND
> 4000021074009129BSNL KYN KLYN-220
> 09325102169
> 401 0000
> 00008410020
> 162891930
> 47153404
> BSNLK
>
>
> 6640036700210
> 45028
> SHARAD HARI GHUGE
> 4000021074009129BSNL KYN KLYN-221
> 07095102168
> 043 0000
> 00026140020
> 162891740
> 471524646
> BSNLK
>
>
> 6640036700211
> CC-252
> DARSHAN AUTO PARTS
> 4000021074009129BSNL KYN SRVI-281
> 318 5102172
> 807 0000
> 00012230020
> 162896660
> 471584384
> BSNLK
>
> If that's the case then change Norm's program,
> because to get the data you want, you have to get EVERYTHING.
>
> Private Type MyData
> field1 as string*13
> field2 as string*18
> custname as string*40
> field3 as string*33
> field4 as string*11
> field5 as string*9
> field6 as string*11
> field7 as string*9
> field8 as string*11
> field9 as string*9
> field10 as string*9
> field11 as string*7
> End Type
>
> I'll assume field2 is the accno, and for the hell of it, field7 is the
> amount
>
> Norm's program then would also change to
>
> Print#fNumOut, Rec.field2
> Print#fNumOut, Rec.custname
> Print#fNumOut, Rec.field7
>
>
> Argusy
>
>
>

argusy

2006-03-11, 7:00 pm

Hemu,

Your "record" length is 160 characters (13+18+4-0+33+11+9+11+9+9+7)
By the looks of it, the file also doesn't have line feed/carriage
returns to separate the data,
so your file input will be one long string. I'll let you work that one
out, from VB's 'Help'

Does your file have header info?

If it does, you will have to do a right$ on your input string,
removing the number of characters in the header

( i.e. newstring = right$(datastring, x) ' with x being the header count
time to do some looping
loop
chop up newstring into "records" to fit your data type (mid$ good
for this)
paste into data type
save the accno, name and amt into new file
while there's still data left in string

close files

Argusy

Hemant_india wrote:

>hi argusy
>while using norm's code system gives the message "file is in used"
>i don't know why
>i mean opening two files at a time
>& you are right that's the amt
>but i have no intentions to swindle any way
>i will try norm's code again and will let you know
>regards
>
>
>


argusy

2006-03-11, 7:00 pm

Whoops!!
try
( i.e. newstring = right$(datastring, len(datastring) - x)

Graham

argusy wrote:

> Hemu,
>
> Your "record" length is 160 characters (13+18+4-0+33+11+9+11+9+9+7)
> By the looks of it, the file also doesn't have line feed/carriage
> returns to separate the data,
> so your file input will be one long string. I'll let you work that
> one out, from VB's 'Help'
>
> Does your file have header info?
>
> If it does, you will have to do a right$ on your input string,
> removing the number of characters in the header
>
> ( i.e. newstring = right$(datastring, x) ' with x being the header
> count
> time to do some looping
> loop
> chop up newstring into "records" to fit your data type (mid$ good
> for this)
> paste into data type
> save the accno, name and amt into new file
> while there's still data left in string
>
> close files
>
> Argusy
>
> Hemant_india wrote:
>
>


Hemant_india

2006-03-12, 3:57 am

please elaborate on header row
--
hemu


"argusy" wrote:

> Whoops!!
> try
> ( i.e. newstring = right$(datastring, len(datastring) - x)
>
> Graham
>
> argusy wrote:
>
>
>

argusy

2006-03-12, 3:57 am

Here goes...

A lot of commercial data files have what's called a 'header' row, and it
contains lots of info, normally about the structure of the data in the
file, and probably contains other info such as author, date created,
what database can read it, ad nauseum.
Most database files (such as an access data file) will also contain
passwords, password levels, etc. and could be encrypted.
Open your file in wordpad.
if there is what looks like crap before what you can recognise as the
first "field" in the first "record",
then you have a header row, and you have to remove it before you start
looping to extract the data you want.
If you haven't got any of that, then don't bother with the bit I wrote
about stripping out this header.

Graham







Hemant_india wrote:

>please elaborate on header row
>
>


argusy

2006-03-12, 3:57 am

note that Norm wrote "Air code"
That means he wrote the program from the air in his head (grin - Sorry
Norm)
You will probably have to micro-adjust (???) that code to get it working

Graham

Hemant_india wrote:

>hi argusy
>while using norm's code system gives the message "file is in used"
>i don't know why
>i mean opening two files at a time
>& you are right that's the amt
>but i have no intentions to swindle any way
>i will try norm's code again and will let you know
>regards
>
>
>


Hemant_india

2006-03-14, 3:58 am

hi graham
sequential i am using do nat have header row
it's a flat file ..plain text
but i did able to do my task using 'mid' funtion
like this
do while eof(0
a= readline
for n= 1 to field_count

some array(n,1 to 10) = mid(a,x,len(fld)
loop
and so on
only tell me how to count nmber of line in the file
thanks
--
hemu


"argusy" wrote:

> Here goes...
>
> A lot of commercial data files have what's called a 'header' row, and it
> contains lots of info, normally about the structure of the data in the
> file, and probably contains other info such as author, date created,
> what database can read it, ad nauseum.
> Most database files (such as an access data file) will also contain
> passwords, password levels, etc. and could be encrypted.
> Open your file in wordpad.
> if there is what looks like crap before what you can recognise as the
> first "field" in the first "record",
> then you have a header row, and you have to remove it before you start
> looping to extract the data you want.
> If you haven't got any of that, then don't bother with the bit I wrote
> about stripping out this header.
>
> Graham
>
>
>
>
>
>
>
> Hemant_india wrote:
>
>
>

argusy

2006-03-14, 3:58 am

your "records" are 160 characters long,
your file length should then be a multiple of that, so the number of
records should be
reccount = lof(yourfile)/160
logic, Hemu, Logical

Graham

Hemant_india wrote:

>hi graham
>sequential i am using do nat have header row
>it's a flat file ..plain text
>but i did able to do my task using 'mid' funtion
>like this
>do while eof(0
>a= readline
>for n= 1 to field_count
>
> some array(n,1 to 10) = mid(a,x,len(fld)
>loop
>and so on
>only tell me how to count nmber of line in the file
>thanks
>
>


Hemant_india

2006-03-14, 7:02 pm

ok...graham
see what i wrote
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim mfile
Dim txtarray()
Dim numarray(10)
numarray(1) = 13
numarray(2) = 18
numarray(3) = 40
numarray(4) = 9
numarray(5) = 7
numarray(6) = 33
numarray(7) = 13
numarray(8) = 11
numarray(9) = 9
numarray(10) = 7

mfile = InputBox("input file name")
tfile = mfile
Dim fs, f, ts, s
arr = 1
i = 1
mfile = "g:\devr\01\src\" & mfile

Set fs = CreateObject("Scripting.FileSystemObject")


Set f = fs.getfile(mfile)

Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
Do While ts.atendofline <> True
LineCount = ts.Line
ts.skipline
Loop
ReDim txtarray(LineCount, 10)
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)

Do While ts.atendofline <> True
s = ts.ReadLine
If Len(s) < 160 Then s = s & String(160 - Len(s), Space(1))

For n = 1 To UBound(numarray)

txtarray(i, n) = Mid$(s, arr, numarray(n))
arr = arr + numarray(n)

Next n
arr = 1
i = i + 1


Loop
ts.Close
'===================
Set ts = Nothing
fs.CreateTextFile "c:\test1.txt" 'Create a file
Set f = fs.getfile("c:\test1.txt")
Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)
For n = 1 To UBound(txtarray, 1)
For i = 1 To UBound(numarray)
newline = newline & txtarray(n, i)
Next i


ts.writeLine newline
Next n
ts.Close
regards
--
hemu


"argusy" wrote:

> your "records" are 160 characters long,
> your file length should then be a multiple of that, so the number of
> records should be
> reccount = lof(yourfile)/160
> logic, Hemu, Logical
>
> Graham
>
> Hemant_india wrote:
>
>
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com