Author Topic: Limiting number of characters per line when writing to text file.  (Read 1510 times)

0 Members and 1 Guest are viewing this topic.

Offline IronMan

  • Newbie
  • Posts: 38
Does anyone know how to limit the width pf a plain text file to allow 65 characters max per line?

I am reading a text file, and once I Print # it back to a text file after making changes to it, the lines are too long and it don't wrap to the next line but instead the lines inside are very long in width. Perhaps a way to do word wrap?

I'm open a file and modifying it to remove "." at the front of each line, and to put a space in between each day.

Example:

CLS
OPEN "forecast.txt" FOR INPUT AS #1
OPEN "test.txt" FOR OUTPUT AS #2
WHILE NOT EOF(1)
    INPUT #1, LineTxt
    IF LEFT$(LineTxt, 1) = "." THEN
        PRINT CHR$(13); CHR$(13)
        PRINT #2, CHR$(13); CHR$(13)
        PRINT MID$(LineTxt, 2); " ";
        PRINT #2, MID$(LineTxt, 2); " ";
    ELSE
        PRINT #2, LineTxt; 'write output to new file test.txt
        PRINT LineTxt; 'prints output to screen
    END IF
WEND
CLOSE #1
CLOSE #2


The problem is, It open the file for input and make the changes, but when the output changes a written to the new file test.txt, the lines for each line are too long. I need to limit them to no more than 65 characters per line for what I'm needing to do. I have tried several thing using the col% and LEFT$ but can't get it to use no more than 65 characters per line before starting a new line.

I am attaching the text file for reference.

If anyone know how to resolve this please share any ideas you may have.

Any help is appreciated.
Thank you.
Kent

 [ You are not allowed to view this attachment ]  

 [ You are not allowed to view this attachment ]  


Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Limiting number of characters per line when writing to text file.
« Reply #1 on: March 24, 2020, 03:29:05 pm »
You know the lines already look OK in forecast.txt maybe wordwrap isn't needed, try this:
Code: QB64: [Select]
  1. DIM lineTxt AS STRING
  2. OPEN "forecast.txt" FOR INPUT AS #1
  3. OPEN "test.txt" FOR OUTPUT AS #2
  4.     INPUT #1, lineTxt
  5.     IF LEFT$(lineTxt, 1) = "." THEN
  6.         PRINT CHR$(13); CHR$(13)
  7.         PRINT #2, CHR$(13)
  8.         PRINT MID$(lineTxt, 2); " ";
  9.         PRINT #2, MID$(lineTxt, 2)
  10.     ELSE
  11.         PRINT #2, lineTxt 'write output to new file test.txt
  12.         PRINT lineTxt; 'prints output to screen
  13.     END IF
  14.  

Offline IronMan

  • Newbie
  • Posts: 38
Re: Limiting number of characters per line when writing to text file.
« Reply #2 on: March 24, 2020, 04:40:33 pm »
You know the lines already look OK in forecast.txt maybe wordwrap isn't needed, try this:
Code: QB64: [Select]
  1. DIM lineTxt AS STRING
  2. OPEN "forecast.txt" FOR INPUT AS #1
  3. OPEN "test.txt" FOR OUTPUT AS #2
  4.     INPUT #1, lineTxt
  5.     IF LEFT$(lineTxt, 1) = "." THEN
  6.         PRINT CHR$(13); CHR$(13)
  7.         PRINT #2, CHR$(13)
  8.         PRINT MID$(lineTxt, 2); " ";
  9.         PRINT #2, MID$(lineTxt, 2)
  10.     ELSE
  11.         PRINT #2, lineTxt 'write output to new file test.txt
  12.         PRINT lineTxt; 'prints output to screen
  13.     END IF
  14.  

Yes, By removing the ; WordWrap from the #2 does put it in the correct spacing from left to right as needed.
However, the first three entries OVERNIGHT..., TUESDAY..., and TUESDAY NIGHT... do not format correctly and adds additional lines. The rest of the days in the file looks fine. I think it because the first three days have more lines of text.

But yes, bplus, you are correct and that fixes it. Just bring on another thing to have to figure out now to get the first three days to look right.

Thanks for your help.
- Kent

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Limiting number of characters per line when writing to text file.
« Reply #3 on: March 24, 2020, 05:44:51 pm »
As a word of encouragement, if you can get it looking right on screen, you should be able to duplicate it in the file, PRINT and PRINT #2 work about the same. CHR$(13) is carriage return (on type writer) starts line at beginning, CHR$(10) is line feed, starts new line, together they start at beginning of next line. Some .txt editors just use one or other. CHR$(9) is a tab that might come in handy.
« Last Edit: March 24, 2020, 05:48:53 pm by bplus »