0
public static List<string> items = new List<string>() { "a","b","c","d","e" };

I am trying to change each of those from loading a file and replacing with their current inventory.

if (File.Exists("darColItems.txt") == true)
{
    char c = ',';
    StreamReader st = new StreamReader("darColItems.txt");
    temp = st.ReadToEnd().ToCharArray();
    foreach (c in temp)
    {

    }
    st.Close();
}

Edit: Taking a file such as: iron,bronze,gold,diamond,iron and taking each name and place it into the list for each spot.

File.txt: "string1","string2","string3","string4","string5"

Startup of program:

List inventory (current):

"a","b","c","d","e"

Load inventory....

List inventory (final):

"string1","string2","string3","string4","string5"
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 1
    What is "each smaller string"? Do you want to replace every string in the list with a single `char` from the file in the same order? Then you could use `items=File.ReadAllText("..").Select(c => c.ToString()).ToList()`. – Tim Schmelter Jul 24 '14 at 06:56
  • 1
    Could you please elaborate the question? I don't see the conection between "items" and the rest of the code :/ – Nahuel Ianni Jul 24 '14 at 06:59
  • 2
    Add some sample input and the expected output. question is really unclear at the moment. – Sriram Sakthivel Jul 24 '14 at 06:59
  • 1
    _"Taking a file such as: iron,bronze,gold,diamond,iron"_ That's not a _file_, those are just words without context. Are these strings in the file separated by comma, is this a list where each word is in a different line, do you want to replace `"a"` with `"iron"` because both are at the beginning? The question is still pretty unclear. – Tim Schmelter Jul 24 '14 at 07:03
  • What's the purpose of `"a","b","c","d","e"`; if this list were any different, would you want the output (i.e. `"Iron","Gold","Copper","Diamond","Bronze"`) to be different? – JohnLBevan Jul 24 '14 at 07:03
  • If you're just trying to read values from a csv file into an array: http://stackoverflow.com/questions/5282999/reading-csv-file-and-storing-values-in-to-an-array – JohnLBevan Jul 24 '14 at 07:04
  • I am trying to make it so it loads their current game inventory into the player. from the save files. – Jacob Michael Lovejoy Jul 24 '14 at 07:05
  • @JacobMichaelLovejoy: what does `"a"` do/mean in the above context? My guess is you're familiar with arrays and know they can't be resized, so you're putting these values in to ensure the list has the expected capacity? However FYI lists can grow as required. – JohnLBevan Jul 24 '14 at 07:06
  • it is a placeholder atm I wanted to change it to the loadout of the players inventory. – Jacob Michael Lovejoy Jul 24 '14 at 07:06
  • @JacobMichaelLovejoy: ok, but show the exact structure of the file, specify the requirement understandingly and show the desired result. – Tim Schmelter Jul 24 '14 at 07:07
  • May be this is what you need `var list = File.ReadLines("darColItems.txt").SelectMany(x => x.Split(',')).ToList();` – Sriram Sakthivel Jul 24 '14 at 07:09
  • what is the purpose of using `"a","b","c","d","e"` I think the aim is to convert the text into list,isn't it? – Suji Jul 24 '14 at 07:12

2 Answers2

2

Assuming that you actually want to replace all items in the list with all items in the file in the order of occurence and the delimiter is comma. You could use String.Split:

items = File.ReadAllText("path").Split(new [] { ',' }, StringSplitOptions.None).ToList();

If you have quotes around the words in the file which you want to remove, you can use String.Trim:

items = File.ReadAllText("path")
    .Split(new char[] { ',' }, StringSplitOptions.None)
    .Select(s => s.Trim('"', ' '))  // remove quotes + spaces at the beginning and end
    .ToList();
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0
//keep filename in a constant/variable for easy reuse (better, put it in a config file)
const string SourceFile = "darColItems.txt";

//what character separates data elements (if the elements may contain this character you may instead want to look into a regex; for now we'll keep it simple though, & assume that's not the case
const char delimeter = ',';

//here's where we'll store our values
var values = new List<string>();

//check that our input file exists
if (File.Exists(SourceFile))
{
    //using statement ensures file is closed & disposed, even if there's an error mid-process
    using (var reader = File.OpenText(SourceFile))
    {
        string line;
        //read each line until the end of file (at which point the line read will be null)
        while ((line = reader.ReadLine()) != null)
        {
            //split the string by the delimiter (',') and feed those values into our list
            foreach (string value in line.Split(delimiter)
            {
                values.Add(value);
            }
        }
    }
}
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178