1

I'm new to C# i need help on reading a file that currently has 7 lines of text but I need it to write "Line PlaceHolder" after those 7 lines until it reaches line 100 in the text file. This is what i have so far and i know it's my failed attempt: EDIT: It's good but only issue is an exception is throw that a process is already using the text file, how do I solve this to read/write that file at the same time??

  public void ReadFile()
    {
        if (File.Exists(AccountsFile))
        {
            using (StreamReader Reader = new StreamReader(AccountsFile))
            using (StreamWriter Writer = new StreamWriter((AccountsFile)))
            {
                for (int i = 0; i < 100; i++)
                {
                    string line;
                    if ((line = Reader.ReadLine()) == null)
                    {
                        Writer.WriteLine("Line Placeholder");

                    }
                }
            }
        }
        else
        {
            File.Create(AccountsFile);
        }
    }
userafg
  • 13
  • 5

3 Answers3

0

Looks like you are just missing an else:

    public void ReadFile()
    {
        if (File.Exists(AccountsFile))
        {
            using (StreamReader Reader = new StreamReader(AccountsFile))
            using (StreamWriter Writer = new StreamWriter((AccountsFile)))
            {
                for (int i = 0; i < 100; i++)
                {
                    string line;
                    if ((line = Reader.ReadLine()) == null)
                    {
                        Writer.WriteLine("Line Placeholder");
                    }
                    else
                        Writer.WriteLine(line);
                }
            }
        }
        else
        {
            File.Create(AccountsFile);
        }
    }
Ron Beyer
  • 10,378
  • 1
  • 15
  • 33
  • 1
    Though true, it probably would be better for the _code_ to determine when the 7th line was reached rather than the file – MickyD Feb 19 '18 at 01:43
  • Hey guys it says process already using the accounts file and I know it's the streamreader / writer, so how do I solve this and read/write at the same time ? – userafg Feb 19 '18 at 02:07
0

You could first read the file contents into an array using File.ReadAllLines, get the array .Length (representing the number of lines in the file), and subtract that number from 100 to see how many lines you need to write. If the number is greater than zero, then create a List<string> with that many empty lines and write those lines to the end of the file using File.AppendAllLines:

// See how many lines we need to add
var newLinesNeeded = 100 - File.ReadAllLines(AccountsFile).Length;

// Add them if needed
if (newLinesNeeded > 0)            
{
    // Create a list of empty lines
    var blankLines = new List<string>();
    for(int i = 0; i < newLinesNeeded; i++)
    {
        blankLines.Add("");
    }

    // Append them to our file
    File.AppendAllLines(AccountsFile, blankLines);
}
Rufus L
  • 32,853
  • 5
  • 25
  • 38
0

this may work if you do not mind opening the file as Read/Write

using (FileStream fileStream = File.Open(AccountsFile, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
    var streamWriter = new StreamWriter(fileStream);
    var streamReader = new StreamReader(fileStream);
    var i = 0;

    // read and count the lines
    while (streamReader.ReadLine() != null){
        i++;
    }

    // if any more lines are needed write them
    while (i++ < 100)
    {
        streamWriter.WriteLine("Line Placeholder");
    }
    streamWriter.Flush();
}
MEC
  • 1,322
  • 1
  • 15
  • 22