Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ADD LINE BREAKS TO SINGLE ROW OF TEXT

Status
Not open for further replies.

TMurdach

IS-IT--Management
Apr 18, 2003
61
US
Hi all,

I was wondering if somebody already had code that would read a single line of text from FILE-A, and add a line break after every 10th item (each separated by comma) and write this to FILE-B?

I have an application that writes its output to a single line of text and need to format this data.

Thank you,

Thomas
 
If your data is seperated by comma's already, look into using 'Split'

Assign each element into an array, and step through the array by 10.

Shouldn't be to difficult. If you need a sample of code, let me know.
 
kodr - if you can give me a hint of the code I would be most gracious!!!

Thank you!
 
Doh! Sorry, I thought I was on a different topic. My mistake. I'll post a sample of code for you in a bit.
 
Forget what I said about split. I thought I was on a VB board.

You're probably going to need to look at 'strtok'
Here's an example straight from the help files.

Code:
proc main
   string DataStr                 ; String to contain data.
   string ItemStr                 ; String to contain extracted item.

   DataStr = "4D,61,72,6B,75,73"  ; Assign hex values to data list.
   while 1                        ; Loop forever, or until EXITWHILE.
      strtok ItemStr DataStr "," 1 ; Get first item from data list.
      if nullstr ItemStr          ; Check to see if item is null.
         exitwhile                ; If so, exit loop.
      endif

      usermsg "Data item is `"%s`"." ItemStr
   endwhile
endproc
[\code]

So what you end up with will look something like:

[code]
string sData
string sTemp
string sTemp2
integer iIndex
integer iEnd

iEnd = 1
if fopen 0 "C:\test.txt" READ TEXT

while not feof 0
     fgets 0 sTemp
     while iEnd = 1
          strtok sTemp2 sTemp "," 1
          if nullstr sTemp2 
               exitwhile
          endif
          strcat sData sTemp2
     endwhile
     ; code here to write to new file
endwhile
[\code]

Haven't tested this, but it should get you started.
 
I don't think this will work because the single line of text is greater than 255 characters. If you have any suggestions to work around that limitation I would be glad to hear them.

Thanks!
 
Is each line the same size, or would each line vary in its length? Actually, now that I look at your original message again, the file only has one long line. In that case, you can use multiple fget commands to read 256 characters at a time. You could then use kodr's script above, and instead of using fputs to write the output, use fwrite so that no carriage return or linefeeds are added. Then, when you get to the tenth element and write that, follow it up with an fwrite "`n`r" to add the carriage return/linefeed to the end of that line, then pick up again with the data remaining from the previous fgets command.

In my opinion, it might be easiest just to read a chunk at a time, then use strextract to pull out each piece of data. You could then write each piece (followed by a comma) to the new file as you parse it from the old file and write the carriage return/line feed as needed (i.e. keep a counter of how many strings you have written to the new file, then zero it when it hits 10). When you get to the end of the data in the string you read, just read more into a temporary variable (or copy the leftovers from the previous read into a temp. variable).

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top