May 02, 2024, 02:08:51 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Writing to a text file

Started by oneplace2u, August 20, 2009, 07:11:38 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

oneplace2u

I have a text file with many Articles and under these Articles there is several lines of texts.
I want to write the lines of texts to another file. All the lines under each Article will be place in seperate
in a file for that article. But I don't want the Article name or line written to the file. There are many more articles than listed below.

Sample of file:


Article1
BH-50BLK/S Wool Felt Top Hat BLACK SMALL  S
BH-50BLK/M Wool Felt Top Hat BLACK MEDIUM  M
BH-50BLK/L Wool Felt Top Hat BLACK LARGE  L
BH-50BLK/XL Wool Felt Top Hat BLACK XL  XL
Article2
BH-51BLK/S Wool Felt Derby Hat BLACK SMALL  S
BH-51BLK/M Wool Felt Derby Hat BLACK MEDIUM  M
BH-51BLK/L Wool Felt Derby Hat BLACK LARGE  L
BH-51BLK/XL Wool Felt Derby Hat BLACK XL  XL
Article2
BH-51BR/S SPECIAL! Wool Felt Derby Hat BROWN SMALL d  S
BH-51BR/M SPECIAL! Wool Felt Derby Hat BROWN MEDIUM d  M
Article3
COS-204BLK/S Black Wool Felt Gangster Hat SMALL  S
COS-204BLK/M Black Wool Felt Gangster Hat MEDIUM  M
COS-204BLK/L Black Wool Felt Gangster Hat LARGE  L
COS-204BLK/XL Black Wool Felt Gangster Hat XL  XL
Article4
COS-204CRY/S CRYSTAL Wool Felt Gangster Hat SMALL  S
COS-204CRY/M CRYSTAL Wool Felt Gangster Hat MEDIUM  M
COS-204CRY/L CRYSTAL Wool Felt Gangster Hat LARGE  L
COS-204CRY/XL CRYSTAL Wool Felt Gangster Hat XL  XL
Article5
COS-220BR/S Indiana Jones Hat - Size SMALL  S
COS-220BR/M Indiana Jones Hat - Size MEDIUM  M
COS-220BR/L Indiana Jones Hat - Size LARGE  L
COS-220BR/XL Indiana Jones Hat - Size XL  XL

Ficko

If understand you right may this can work:


AUTODEFINE "OFF"
'OPENCONSOLE
DEF InputFile,OutPutFile :FILE
DEF StartPath,Row :STRING
DEF Count :INT
StartPath = GETSTARTPATH

IF !(OPENFILE(InputFile,StartPath + "Input.txt","R")) THEN
DO
READ(InputFile,Row)
IF (LEFT$(Row,7) = "Article") THEN
IF (Count) THEN CLOSEFILE OutPutFile
Count = VAL(MID$(Row,8))
OPENFILE(OutPutFile,StartPath + "Article" + USING("0#####",Count)+".csv","W")
ELSE
WRITE(OutPutFile,Row)
ENDIF
UNTIL (EOF(InputFile))
CLOSEFILE OutPutFile
CLOSEFILE InputFile
ELSE
PRINT "Can't open:" + StartPath + "Input.txt"
ENDIF
PRINT "Press <any> key to end"
WAITCON

oneplace2u

How would this work each Article is name different like?
Article1
Article2
Article3

and so on

Barney

Line 11 takes care of that...

Barney

oneplace2u

I have tried this but it will not compile. No errors

Bob

Ficko

Quote
How would this work each Article is name different like?
...

You mean "ArticleX" just stands for something else like "pencil" ?

That case you have several approaches.

1.) If the name of the article is allways shorter than its description you can separate it by "LEN(Row) < threshold"

2.) If You have a separate listing of all possible articles you can compare each element to each readed line (Row) if match and separate by that
To do that you can use the arrays library I posted :http://www.ionicwind.com/forums/index.php/topic,3463.0.html
You just simply load the list into a string array sort it - or not - and use "ArraySearch(S)" to find the match.
Or if this list is in a db use EB database library.

3.) If you can make a slightly different listing with spaces between article and description or between distinct articles you can separate by that emty line.
Like:
Quote
Article1

BH-50BLK/S Wool Felt Top Hat BLACK SMALL  S

or

Quote
BH-50BLK/XL Wool Felt Top Hat BLACK XL  XL

Article2
_______________________________________________
Quote
I have tried this but it will not compile. No errors

I can't help on that.
Just follow the manual of how to compile a source file or project.
There must be a fundamental error in steps you are making.
Even if the code erroneous it has no worm in it to avoid compiler error messages. ;D

Regards
Ficko

oneplace2u

I have no problems on how to compile
Thank you for your help and the source code, I have tried to use the source code but it will not compile.
I don't know what this is "WAITCON" I don't see it listed anywhere. If I remove it, it will compile but will not show or produce any thing when it runs.

You can see I have not changed your code except to leave out the "WAITCON"

DEF InputFile,OutPutFile:FILE
DEF StartPath,Row:STRING
DEF Count:INT
StartPath = GETSTARTPATH

IF !(OPENFILE(InputFile,StartPath + "Input.txt","R")) THEN
   DO
      READ(InputFile,Row)
      IF (LEFT$(Row,7) = "Article") THEN
         IF (Count) THEN CLOSEFILE OutPutFile
         Count = VAL(MID$(Row,8))
         OPENFILE(OutPutFile,StartPath + "Article" + USING("0#####",Count)+".csv","W")
      ELSE
         WRITE(OutPutFile,Row)
      ENDIF
   UNTIL (EOF(InputFile))
   CLOSEFILE OutPutFile
   CLOSEFILE InputFile
ELSE
   PRINT "Can't open:" + StartPath + "Input.txt"
ENDIF
PRINT "Press <any> key to end"

Ficko

OK, let's take it slowly. :)

What you mean by

Quote
it will not compile
??

Do you get an error message?
How you know it dosn't compile?

Quote
I don't know what this is "WAITCON" I don't see it listed anywhere

You right it dosn't show up in the "index" but if you do a "search" it will be listed.

Quote
it will compile but will not show or produce any thing when it runs.

The program suppose you have an "Input.txt" in the start directory - the directory your executable located -
The "Input.txt" should consist of the data you posted in your firs post.

Quote
Article1
BH-50BLK/S Wool Felt Top Hat BLACK SMALL  S
BH-50BLK/M Wool Felt Top Hat BLACK MEDIUM  M
...
After runing it will show nothing except error or key press request.
It generates couple of *.csv files in your start directory as the result I understand you want to have.

billhsln

Sounds like 'oneplace2u' has an older version of EBasic.  WaitCon is a fairly new keyword.   Works like INKEY$, but does not keep the key pressed.

Bill
When all else fails, get a bigger hammer.

Barney

I would suggest oneplace2u to download the latest version of EB. It will include the latest helpfile where the WAITCON is nicely explained.

Barney

Ionic Wind Support Team

He's not a licensed user.  So is probably using the old unsupported freeware version.
Ionic Wind Support Team

oneplace2u

oh, by the way I am a registered user.
I updated to the latest Version, I am sorry but I did find the "waitcon"
It does compile, But when I run the exe file it tells me it cannot find the Input.txt file.
The file is there in the start directory as with the exe file. The "Input.txt" does consist of the data that I posted in my firs post.
I have no idea why it can not find it.

Ionic Wind Support Team

So you are, different email addresses.

What does this line: PRINT "Can't open:" + StartPath + "Input.txt"

Show?  I am interested in the path on your machine.

Ionic Wind Support Team

oneplace2u

I found the problem and it works great. Thank you
It was missing the "!" in front of the (OPENFILE.

Thank you again