Making a flat file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JAc

    Making a flat file

    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.Capa city = 200;

    recordline.appe nd("R01",1,3);
    recordline.appe nd("TEST",4,4) ;
    recordline.appe nd(obj.textfiel d,8,10);

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

    streamwriterCub ic.Writeline(re cordline);

    }
    streamwriterCub ic.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
  • Joe Mayo

    #2
    Re: Making a flat file

    Hi Jac,

    I think you are getting a runtime error
    (System.Argumen tOutOfRangeExce ption?) 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.Appe nd("R01");

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

    Joe
    --
    Welcome to C# Station!  This is a community site for people interested in applying .NET using the C# programming language.  We’ve been around since July 4th 2000 and have continued to grow over the years.  Items of interest include Articles, Books, Links, Documentation,  and Tutorials. More… Source Code If you would like to see an […]


    "JAc" <JAc@discussion s.microsoft.com > wrote in message
    news:EC4A9930-2A53-4848-9B44-5A6A41EAD3F4@mi crosoft.com...[color=blue]
    > 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.Capa city = 200;
    >
    > recordline.appe nd("R01",1,3);
    > recordline.appe nd("TEST",4,4) ;
    > recordline.appe nd(obj.textfiel d,8,10);
    >
    > recordline = recordline + "\n"; // New line is it nessecary?
    >
    > streamwriterCub ic.Writeline(re cordline);
    >
    > }
    > streamwriterCub ic.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[/color]


    Comment

    • Joakim Karlsson

      #3
      Re: Making a flat file

      In addition to what Joe mentioned:

      The StringBuilder.C apacity 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

      Comment

      Working...