-1

This is the text file read by the StreamReader.

500, 120, 60, 0, 350
100, 230, 0, 50, 0
0, 75, 0, 0, 220
3000, 400, 600, 35, 0
350, 200, 100, 80, 250
0, 285, 325, 150, 75

I want to add cities in front of each line, such that it reads like this:

Atlanta:  500, 120, 60, 0, 350
Baltimore: 100, 230, 0, 50, 0
Chicago:0, 75, 0, 0, 220
Denver: 3000, 400, 600, 35, 0
ELY: 350, 200, 100, 80, 250
Fargo: 0, 285, 325, 150, 75

Here's the current code block.

StreamReader sr = new StreamReader("inventory.txt");
String line = sr.ReadToEnd();
Console.WriteLine(line);
Kevin
  • 16,549
  • 8
  • 60
  • 74
  • Have you tried using `ReadLine()` instead? Provided your lines are separated by line breaks. You then could just call `line = "(City) " + line` and write it to console. – Spevacus Sep 27 '19 at 02:24
  • 1
    Basic idea is: read the file line by line; as you do so, for each line modify as desired, and write that line out to a new file. See marked duplicates for examples of that sort of operation. – Peter Duniho Sep 27 '19 at 02:49

1 Answers1

0

If each line is separated by "\n" you could do:

string[] temp = line.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < cityNames.Length; i++)
{
    temp[i] += cityNames[i];
}
string result = string.Join(",\n", temp);
return result.Remove(result.Length - 3, 2);