-1

I am unsure of what I am doing wrong here. It compiles and runs, and acts ok, but it is not writing the line into the text file. I have my FileStream and read on MSDN to try and figure this out on my own. When I did get something to work, it overwrote the entire file with what I put into it. So I am hoping maybe a programmer who has done this for more than 4 months (such as me) can explain. Here is a piece of my code that I think is relevant.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication33
{
class Program
{
    static void Main(string[] args)
    {
        FileStream FireBall = new FileStream("Students.txt", FileMode.Open, FileAccess.Read);
        StreamReader inFile = new StreamReader(FireBall);
        string inValue;
        string[] values;
        double GPA;
        double total = 0;
        double counter = 0;
        double count = 0;
        double counti = 0;
        double counterr = 0;
        List<string> Anderson = new List<string>(); //Anderson
        List<string> gpa = new List<string>(); //GPA
        List<string> noemail = new List<string>(); // email
        List<string> lines = new List<string>();


        string newLastName = "'Constant";
        string newRecord = "(LIST (LIST 'Constant 'Malachi 'D ) '1234567890 'mdconstant@mail.usi.edu 4.000000 )";
        string line;
        string lastName;
        bool insertionPointFound = false;

        for (int i = 0; i < lines.Count && !insertionPointFound; i++)
        {

            line = lines[i];
            if (line.StartsWith("(LIST (LIST "))
            {
                values = line.Split(" ".ToCharArray());
                lastName = values[2];
                if (newLastName.CompareTo(lastName) < 0)
                {
                    lines.Insert(i, newRecord);
                    insertionPointFound = true;
                }
            }
        }
        if (!insertionPointFound)
        {

            lines.Add(newRecord);
        } 
Zoro
  • 695
  • 2
  • 7
  • 23
  • 1
    -1: I'm not exactly sure what you read on MSDN but you sample missing any traces of writing to a file. Please check out results of following search [msdn c# write to file](http://www.bing.com/search?q=msdn+c%23+write+to+file), especially [How to: Write to a Text File (C#)](http://msdn.microsoft.com/en-us/library/vstudio/8bh11f1k.aspx). Also please try to make your sample more compact (check out http://www.sscce.org for guidance). – Alexei Levenkov Oct 20 '13 at 01:52

2 Answers2

2

You don't do any writing here. Note that you are using FileAccess.Read in

FileStream FireBall = new FileStream("Students.txt", FileMode.Open, FileAccess.Read);

It should be replaced with FileAccess.ReadWrite

You can use StreamWrite to write to the file. And you can read and write easily with File.ReadAllText and File.WriteAllText

Jerry
  • 4,258
  • 3
  • 31
  • 58
  • FileStream FireBall = new FileStream("Students.txt", FileMode.Open, FileAccess.ReadWrite); Still does nothing? – Zoro Oct 20 '13 at 01:49
  • 1
    Take a look at this question: http://stackoverflow.com/questions/605685/how-to-both-read-write-file-in-c-sharp – Jerry Oct 20 '13 at 01:54
2

What I don't see is any code that writes to the file itself. If you are reading the contents of the file into the lines List, writing to that list is not the same as writing to the actual file.

So you need to explicitly write to the file, with something like a StreamWriter. See here: http://msdn.microsoft.com/en-us/library/6ka1wd3w.aspx

canhazbits
  • 1,664
  • 1
  • 14
  • 19