Creating Tab Delimited Output File
-
I'm using a streamwriter to output 4 columns of data into a text file. Unfortunately the columns aren't lining up correctly. I've tried using Controlchars.tab & vbTab which do not work. Is there any way to create tabs in the writeline method or would I need to switch to a rich text file for this? If anyone can help it would be greatly appreciated! Oh yeah I'm using Visual Basic 2003. Swish
-
I'm using a streamwriter to output 4 columns of data into a text file. Unfortunately the columns aren't lining up correctly. I've tried using Controlchars.tab & vbTab which do not work. Is there any way to create tabs in the writeline method or would I need to switch to a rich text file for this? If anyone can help it would be greatly appreciated! Oh yeah I'm using Visual Basic 2003. Swish
Where don't they line up ? Tabs only line up if your strings are similar lengths, a long one will skip several tab stops.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
I'm using a streamwriter to output 4 columns of data into a text file. Unfortunately the columns aren't lining up correctly. I've tried using Controlchars.tab & vbTab which do not work. Is there any way to create tabs in the writeline method or would I need to switch to a rich text file for this? If anyone can help it would be greatly appreciated! Oh yeah I'm using Visual Basic 2003. Swish
Horizontal alignment is bound to fail unless one of two conditions is met: - all characters have the same width, i.e. the font is non-proportional such as "Courier New"; - you take into account the exact width of each character On top of that, a TAB character most of the time indicates jumping to the next multiple of 8 (or 4 or whatever) character positions, again assuming a non-proportional font. [added]And not: moving to the next column, as would be the case in Word or Excel table[/added] :) -- modified at 20:57 Tuesday 9th October, 2007
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
-
I'm using a streamwriter to output 4 columns of data into a text file. Unfortunately the columns aren't lining up correctly. I've tried using Controlchars.tab & vbTab which do not work. Is there any way to create tabs in the writeline method or would I need to switch to a rich text file for this? If anyone can help it would be greatly appreciated! Oh yeah I'm using Visual Basic 2003. Swish
What you're describing is a report text file, not a tab delimited file. A delimiter is a character or string of characters that seperates fields in a data file. What you're describing is a text file laid out like a report.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
What you're describing is a report text file, not a tab delimited file. A delimiter is a character or string of characters that seperates fields in a data file. What you're describing is a text file laid out like a report.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007Thanks guy's for replying. The alignment is thrown off by the strings length. And right now padding is still throwing off the alignment. I'm using "Arial" with a font size of "8" and pagesettings.landscape = True. Dave your right, but I'm having trouble with the layout of the report. Is there a better method of creating reports that have correct alignment in a text, rtf or doc file? Or have the app write to a specific location within the file to better align the report? Swish
-
Thanks guy's for replying. The alignment is thrown off by the strings length. And right now padding is still throwing off the alignment. I'm using "Arial" with a font size of "8" and pagesettings.landscape = True. Dave your right, but I'm having trouble with the layout of the report. Is there a better method of creating reports that have correct alignment in a text, rtf or doc file? Or have the app write to a specific location within the file to better align the report? Swish
You have two problems. The first is that different application interpret tabs differently. One may replace a tab character with a bunch of spaces and another might add a bunch of whitespace to hit the next tabstop in it's rendering engine, while another might do something different and add a different amount of whitespace for each tab. The second is that Arial is a proportional font, meaning that each character is rendered in a cell reletive to the size width of the character. Spaces can be very narrow compared to all the captial letters. Mono-spaced fonts have every character, even spaces, rendered in cells of the exact same width for every character. This means that a space is exactly the same width as, say, a captial W. If you're writing to a text file (*.TXT), the font doesn't matter since it's not used in the file. What screws up the formatting is the application that is rendering the text. For instance, if you open the file in Notepad and configure Notepad to use the Arial font, the report layout will be severly messed up. Change the font Notepad uses to something like Lucida Console, which is mono-spaced, and column alignment changes. But now you have a problem with the size of the columns. Assume that you have two lines of text int the file, laid out like this:
sometext[TAB]some more text
someotherlongertext[TAB]even more textSay Notepad uses a tabstop every one inch. The "sometext" in the first line of the file is shorter than than one inch. The tab will move the cursor to the one mark and then the rest of the line shows up. Now, for the second line, the first part of it, "someotherlongertext", is longer one inch. Well, the tab will move the cursor to the two inch mark, where the rest of the line will be rendered. And if you haven't figured it out yet, the second column will not be lined up. There-in lies the problem with using Tabs to do reporting. Each line will have to know how it's going to be rendered and have a variable number of tabs between columns to line them up. Since your application can't possibly know how the text file is going to be rendered, it's impossible to predict the number of tabs needed between columns for each line in the report. What's the solution?? Use a dedicated reporting tool, like CrystalReports, to generate the report. If it has to be in a text file, replace the tabs with a known number of spaces. You're going to have to do more work like keeping track of how many characters are in each field and how wide (in characters)
-
You have two problems. The first is that different application interpret tabs differently. One may replace a tab character with a bunch of spaces and another might add a bunch of whitespace to hit the next tabstop in it's rendering engine, while another might do something different and add a different amount of whitespace for each tab. The second is that Arial is a proportional font, meaning that each character is rendered in a cell reletive to the size width of the character. Spaces can be very narrow compared to all the captial letters. Mono-spaced fonts have every character, even spaces, rendered in cells of the exact same width for every character. This means that a space is exactly the same width as, say, a captial W. If you're writing to a text file (*.TXT), the font doesn't matter since it's not used in the file. What screws up the formatting is the application that is rendering the text. For instance, if you open the file in Notepad and configure Notepad to use the Arial font, the report layout will be severly messed up. Change the font Notepad uses to something like Lucida Console, which is mono-spaced, and column alignment changes. But now you have a problem with the size of the columns. Assume that you have two lines of text int the file, laid out like this:
sometext[TAB]some more text
someotherlongertext[TAB]even more textSay Notepad uses a tabstop every one inch. The "sometext" in the first line of the file is shorter than than one inch. The tab will move the cursor to the one mark and then the rest of the line shows up. Now, for the second line, the first part of it, "someotherlongertext", is longer one inch. Well, the tab will move the cursor to the two inch mark, where the rest of the line will be rendered. And if you haven't figured it out yet, the second column will not be lined up. There-in lies the problem with using Tabs to do reporting. Each line will have to know how it's going to be rendered and have a variable number of tabs between columns to line them up. Since your application can't possibly know how the text file is going to be rendered, it's impossible to predict the number of tabs needed between columns for each line in the report. What's the solution?? Use a dedicated reporting tool, like CrystalReports, to generate the report. If it has to be in a text file, replace the tabs with a known number of spaces. You're going to have to do more work like keeping track of how many characters are in each field and how wide (in characters)