Making a flat file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hey,

it must be very simple but I am struggeling.

I want to make a simple flat file.

I do now (I also tried with string)

foreach(Object obj in collection)
{
recordline = new StringBuilder();
recordline.Capacity = 200;

recordline.append("R01",1,3);
recordline.append("TEST",4,4);
recordline.append(obj.textfield,8,10);

recordline = recordline + "\n"; // New line is it nessecary?

streamwriterCubic.Writeline(recordline);

}
streamwriterCubic.Close.


It don'work. Receive an error on startindex in append (also when I used
Insert). I also tried with a foreach too fill up the recordline witj 200
blanc's. But I think that is not a good idea. There must be something simple.

Thanks for showing me the best way.
Jac
 
Hi Jac,

I think you are getting a runtime error
(System.ArgumentOutOfRangeException?) because the count in your append goes
beyond the character position in the string you are trying to append. For
example, in the first Append, the starting position parameter refers to
"R01", not the StringBuilder. You said start at position 1, which is '0'.
You also specified the third parameter to count 3 characters. Well, there
aren't 3 characters from '0' in "R01" -- only two. Since you already know
the text you want to append, you don't need the Append overload you're
using. Try this:

recordline.Append("R01");

BTW, it helps if you provide code that compiles. ;)

Joe
 
In addition to what Joe mentioned:

The StringBuilder.Capacity property does not set the length of the
string. It just sets how much memory that will initially be allocated
for the character data. If the string should become larger than the
capacity during the StringBuilder operations, the capacity will be
increased.

So there is no need to fill the StringBuilder with blanks.

Regards,
Joakim
 
Back
Top