Reading and writing files

cswen44

New member
Joined
Jun 15, 2020
Messages
1
Programming Experience
Beginner
I know this is probably a very simple problem, with most likely a very simple solution, but I have been banging my head against a wall for a while now. For some reason this code is not writing to the text files I would like it to and I cannot figure out why
C#:
namespace Application1
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = @"C:/Users/cswenson/Desktop/TestProgram/test.txt";
            var outputp = @"C:/Users/cswenson/Desktop/TestProgram/preout.txt";
            var outputm1 = @"C:/Users/cswenson/Desktop/TestProgram/match1.txt";
            var outputm2 = @"C:/Users/cswenson/Desktop/TestProgram/match2.txt";

            using (var sr = new StringReader(input))
            using (var psw = new StreamWriter(outputp, true))
            using (var o1sw = new StreamWriter(outputm1, true))
            using (var o2sw = new StreamWriter(outputm2, true))
            {
                foreach (string line in File.ReadLines(input))
                {
                    string preout = line.Substring(1, 8) + " " + line.Substring(374, 329) + " " + line.Substring(4937, 639);
                    psw.WriteLine(preout);
                    string match1 = line.Substring(1, 8) + " " + line.Substring(9, 365) + " " + line.Substring(703, 1786);
                    o1sw.WriteLine(match1);
                    string match2 = line.Substring(1, 8) + " " + line.Substring(2489, 2448);
                    o2sw.WriteLine(match2);
                }
            }
        }
    }
}
There's not much to it, but I can't figure out where I am going wrong. It builds successfully, but nothing gets written.

Thanks
 
Last edited by a moderator:
First things first, while I don't think it actually matters, you don't use forward slashes in file paths. They are used in URLs but file paths use bac slashes. That's the whole reason that you use the verbatim string literal prefix (@) on file paths: because the back slash is the escape character and you want them interpreted as literal characters. This:
C#:
var input = @"C:/Users/cswenson/Desktop/TestProgram/test.txt";
var outputp = @"C:/Users/cswenson/Desktop/TestProgram/preout.txt";
var outputm1 = @"C:/Users/cswenson/Desktop/TestProgram/match1.txt";
var outputm2 = @"C:/Users/cswenson/Desktop/TestProgram/match2.txt";
should be this:
C#:
var input = @"C:\Users\cswenson\Desktop\TestProgram\test.txt";
var outputp = @"C:\Users\cswenson\Desktop\TestProgram\preout.txt";
var outputm1 = @"C:\Users\cswenson\Desktop\TestProgram\match1.txt";
var outputm2 = @"C:\Users\cswenson\Desktop\TestProgram\match2.txt";
Also, given that you are using the same folder each time, you really ought to specify the folder path just once:
C#:
var folderPath = @"C:\Users\cswenson\Desktop\TestProgram";
var input = Path.Combine(folderPath, "test.txt");
var outputp = Path.Combine(folderPath, "preout.txt");
var outputm1 = Path.Combine(folderPath, "match1.txt");
var outputm2 = Path.Combine(folderPath, "match2.txt");
 
I'm not sure whether this is your issue specifically but you are opening the input file twice. This opens the file:
C#:
using (var sr = new StringReader(input))
and then this opens the file again:
C#:
foreach (string line in File.ReadLines(input))
You only need one or the other. As the second one is simpler, that's the one you should go with:
C#:
using (var psw = new StreamWriter(outputp, true))
using (var o1sw = new StreamWriter(outputm1, true))
using (var o2sw = new StreamWriter(outputm2, true))
{
    foreach (var line in File.ReadLines(input))
    {
        psw.WriteLine($"{line.Substring(1, 8)} {line.Substring(374, 329)} {line.Substring(4937, 639)}");
        o1sw.WriteLine($"{line.Substring(1, 8)} {line.Substring(9, 365)} {line.Substring(703, 1786)}");
        o2sw.WriteLine($"{line.Substring(1, 8)} {line.Substring(2489, 2448)}");
    }
}
This, of course, assumes that each line in the input file is actually long enough for those substrings. I suggest that you set a breakpoint and step through the code to see that it actually what you expect at each line.
 
Just out of curiosity is Reading and writing files still practical today?
where is it used? isnt everything more database orientated these days?
is it something I should really focus on as a beginner?
 
Just out of curiosity is Reading and writing files still practical today?
where is it used? isnt everything more database orientated these days?
is it something I should really focus on as a beginner?
Text files are still fairly widely used for smaller amounts of data.
 
Back
Top Bottom