none
streamwriter for txt output file RRS feed

  • Question

  •  hi all

    im using streamwriter to output to text file fixed length

    i have:

    L="{0,-7}{1,-10}"

    and the text with spaces

    R="12345  1234567   "

    when i run

    streamwriter.writeline (L,R)

    i get error        INDEX ZERO MUST BE GREATER....ERROR

    but when i run just block is ok

    L="{0,-7}"

    and the text with spaces

    R="12345  "

          

     

     

     

     


    oren t

    Wednesday, November 6, 2019 3:11 PM

Answers

  • The `L` variable is using string formatting and therefore requires 2 arguments: {0} and {1}.

    The `R` variable is a single value so when you call WriteLine with `(L, R)` you're missing the second argument the format string requires and it crashes. You have to pass a separate argument for each specifier {} you use in the format string.

    //Split value by spaces
    var tokens = R.Split(' ').Trim();
    
    //How you can get the individual values but you should ensure that there are actually that many values in the array first
    var value1 = tokens[0];
    var value2 = (tokens.Length > 1) ? tokens[1] : "";
    
    //How you might call WriteLine
    streamwriter("{0,-7}{1,-10}", value1, value2);


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, November 6, 2019 4:44 PM
    Moderator
  • Did you already try an approach like this: WriteLine("{0,-7} {1,-10}", 12345, 1234567)? You should provide a value for each ‘{ }’ placeholder.

    Wednesday, November 6, 2019 3:38 PM

All replies

  • Did you already try an approach like this: WriteLine("{0,-7} {1,-10}", 12345, 1234567)? You should provide a value for each ‘{ }’ placeholder.

    Wednesday, November 6, 2019 3:38 PM
  • The `L` variable is using string formatting and therefore requires 2 arguments: {0} and {1}.

    The `R` variable is a single value so when you call WriteLine with `(L, R)` you're missing the second argument the format string requires and it crashes. You have to pass a separate argument for each specifier {} you use in the format string.

    //Split value by spaces
    var tokens = R.Split(' ').Trim();
    
    //How you can get the individual values but you should ensure that there are actually that many values in the array first
    var value1 = tokens[0];
    var value2 = (tokens.Length > 1) ? tokens[1] : "";
    
    //How you might call WriteLine
    streamwriter("{0,-7}{1,-10}", value1, value2);


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, November 6, 2019 4:44 PM
    Moderator
  • I think Michael has hit on your problem.  "R" is what the RESULT of your formatting would be, if you passed in two integers.  Your format (in L) doesn't expect a string.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Wednesday, November 6, 2019 9:14 PM