0

How do I read from a file which has many lines with each one word and then write a file which just extends the words with something?

For example my imports.txt has these lines:

line1
line2
line3

now I want to get this app to write a new file (exported.txt) which exports something like this

line1, price1
line2, price1
line3, price1

all of the text which gets attached to the string is always the same.

const Int32 BufferSize = 128;

using (var FileStream = File.OpenRead("import.txt"))
            {
                using (var streamReader = new StreamReader(FileStream, Encoding.UTF8, true, BufferSize))
                {
                    string line;
                    while((line = streamReader.ReadLine()) != null)
                    {

                    }
                }
            }

is this the right approach?

CunnertA
  • 165
  • 2
  • 12
  • You can iterate [File.ReadLines](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readlines), build a `List` that contains the rebuilt output strings, then [File.WriteAllLines(Your List)](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.writealllines). If the input file is very large, then use two streams: one reads a line, the other writes a line with the modified string. – Jimi Jun 17 '20 at 09:11
  • Is it possible to read the whole file at once with `FileReadAllLines()`, then the "import.txt" would be locked for less time. You can then iterate through the `string[]` which is returned. – RedFox Jun 17 '20 at 09:11
  • Yes, what issue have you faced? Build you other export file and input and save – HichemSeeSharp Jun 17 '20 at 09:11
  • 2
    About the comments suggesting reading all the lines at once and then iterate: be careful that it might not be a good option for performance purposes, if the file is very big. – Pac0 Jun 17 '20 at 09:13
  • @Pac0 Is right, if performance is an issue you should `StreamReader` like suggested here https://stackoverflow.com/questions/8037070/whats-the-fastest-way-to-read-a-text-file-line-by-line – RedFox Jun 17 '20 at 09:17

1 Answers1

1

In your example you are just reading from a file.

The easiest approach would be to read all lines from your "import.txt" by using File.ReadAllLines()

And then iterate through the lines using foreach/for and manipulate the string.

After that you can use File.WriteAllLines() to save your manipulated strings to the export file.

Like @Pac0 mentioned: Consider not using File.ReadAllLines() when performance is an issue see here.

RedFox
  • 457
  • 3
  • 15
  • In my while loop I did this: using (var writer = new StreamWriter("export.txt")) { writer.WriteLine(line + ", test"); } but its only reading the last line of import.txt and attaches ", test" to it. – CunnertA Jun 17 '20 at 09:17
  • 2
    This is the case because you instantiate a new `StreamWriter` for each line, when the `using` block in in the `while` loop. You have to wrap the using of the `StreamWriter` around the `while loop` ```using (var streamWriter = new StreamWriter(@"c:\git\export.txt", false)) { string line; while ((line = streamReader.ReadLine()) != null) { streamWriter.WriteLine(line + ", test"); } }``` – RedFox Jun 17 '20 at 09:29
  • Your current code creates a new `StreamWriter` for each line you write. The default setting for `append` is false, so your previous line get overwritten and only the last one survives. – RedFox Jun 17 '20 at 09:31