Click here to Skip to main content
15,743,609 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have five columns in my file txt and I want to align my text in the 5 columns..I used getPosition and seek, but I obtain position in file and not in my rows

What I have tried:

I used seek but I don't obtain what I want
Updated 28-Jan-20 21:45pm
KarstenK 29-Jan-20 8:25am    
another possible way it include an tab (HT) for export as some csv data. Else see solution 1.

1 solution

Text files don't have "rows", they just have "character positions" and even those can vary according to the type of "characters" you are storing / encoding in them. ASCII characters in an ASCII Text file will be eight bits - one byte - wide, but Unicode ones can be eight or sixteen (or even 32!) bits wide - and the eight bit version can "extend" a character to occupy two bytes.

Assuming you use ASCII for easy of coding and discussion, unless you apply some "format" to your file to establish rows, there are no such distinctions you can work with, especially if your "columns" within each row are not fixed length - the addition of an "end of row" markert such as newline does not help on it's own as changes within a row can necessitate moving the location of that newline, and that affects all rows later in the file which are not automatically moved to "match up" with the new newline position.
So if you set fixed column widths, giving you fixed row lengths, you can use seek to position yourself to the start of row n by seeking to n * rowLength, and to the start of a column m within row n by seeking to n * rowLength + column0Length + column1Length + ... + columnMMinusOneLength

But ... you will be responsible for ensuring that the column sizes are not exceeded, nothing else will do that for you, and you will cause some horrible errors if you get it wrong. And inserting or deleting a row is going to involve copying the whole file up to a point, then adding a row ot skipping a row, then copying the rest of the file, then deleting the original and renaming the new. This will get to be a slow and annoying procedure if you aren't really careful.
Share this answer
Member 14594285 29-Jan-20 5:01am    
I solved in an other way, always using getPosition and seek, thanks anyway

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900