-2

This is my code, but everytime i try compiling it i keep getting this error:

System.IndexOutOfRangeException: Index was outside the bounds of the array.

using System;
using System.Collections.Generic;
using System.IO;

namespace nrinfile
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> numbers= new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
            string[] folderLoc = File.ReadAllLines(@"E:\folder\numbers.txt");
            for (int i = 0; i < 12; i++)
            {
                folderLoc[i] = Convert.ToString(numbers[i]);
            }
        }
    }
}`
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 4
    `for (int i = 0; i < folderLoc.Length; i++) {...}`: there's *no guarantee* that `folderLoc` has *at least* `12` items – Dmitry Bychenko Jan 29 '20 at 11:21
  • How many lines does the file E:\folder\numbers.txt have? – Andrew Morton Jan 29 '20 at 11:22
  • Please check how to write to files: https://stackoverflow.com/questions/7569904/easiest-way-to-read-from-and-write-to-files – stl Jan 29 '20 at 11:22
  • There is nothing written in the folder – 22supermario22 Jan 29 '20 at 11:24
  • Does this answer your question? [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – vahdet Jan 29 '20 at 11:24
  • even if you fix `IndexOutOfRangeException` this code doesn't make sens ... it seems like you wana write those integers to the file but this is totally wrong code – Selvin Jan 29 '20 at 11:24

2 Answers2

1

There's no guarantee that folderLoc has at least 12 items

        for (int i = 0; i < 12; i++) {...}

You can put it like this (note folderLoc.Length instead of 12):

        string[] folderLoc = File.ReadAllLines(@"E:\folder\numbers.txt");

        // If you want at most 12 items to be changed put the condition as 
        //   i < Math.Max(12, folderLoc.Length)
        for (int i = 0; i < folderLoc.Length; i++)
        {
            folderLoc[i] = $"{i} {folderLoc[i]}"; //TODO: Apply the correct format here
        }

Or even (no explicit loops, but Linq query)

        using System.Linq;

        ...

        string[] folderLoc = File
          .ReadLines(@"E:\folder\numbers.txt")
          .Select((value, index) => $"{index} {value}")
          .ToArray();

If you want to change top 12 lines only, the Select should be

         ...
         .Select((value, index) => index < 12 ? $"{index} {value}" : value)
         ...

Finally, if you want just to write 0..11 numbers into a file

         File.WriteAllLines(@"E:\folder\numbers.txt", Enumerable.Range(0, 12));
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

As you want to write from your list of numbers to a file, you will need to use a System.IO method which writes to a file.

Something like:

static void Main(string[] args)
{

    string destFile = @"E:\folder\numbers.txt";

    List<int> numbers = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

    using (StreamWriter sw = new StreamWriter(destFile)) {
        for (int i = 0; i < numbers.Count(); i++)
        {
            sw.WriteLine(numbers[i].ToString());
        }
    }
}

The using construct takes care of closing the file when the code has finished writing to it and disposing of any "unmanaged resources" that the StreamWriter used.

Instead of the using part and the code inside it, you could use a different method which takes an array of strings and writes that out as lines:

File.WriteAllLines(destFile, numbers.Select(n => n.ToString()).ToArray());
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84