1

I have a problem that requires me to calculate synthetic student marks from a text file. It gives me the weights of the marks in the first row, the number of students to evaluate in the next row, and then the next rows are the students' marks. This pattern repeats through the file without major separation.

For clarity, the text file and problem are here:

I've tried making a new object with streamreader using the following code:

using (StreamReader sr = new StreamReader("DATA10.txt")) {
    blahblahblah;
}

DATA10.txt is in the same folder as the program.

But I get "Cannot convert from 'string' to 'System.IO.Stream'", even though in the examples on MSDN and everywhere else use that exact code just fine. What am I doing wrong?

Eventually what I'll be doing is taking the value from the second line and using streamreader to read that amount of lines. Then repeating the whole process on the next set of data.

I really don't think it's a duplicate of that question, and the answers here are expressed in an easier to understand way.

  • In your code snippet, you are currently missing a `)`. Should be `using (StreamReader sr = new StreamReader("DATA10.txt"))` – Timothy G. Apr 08 '17 at 12:55
  • Sorry, I'm used to VS adding the closing braces for me. I have been using the proper line you pointed out. Sorry. – James Vaughan Apr 08 '17 at 12:56
  • Did you put this file where the .exe file is? – titol Apr 08 '17 at 13:06
  • It's currently just a build in VS, and the strange thing is that `System.IO.File.ReadAllText("DATA10.txt")` works, but I'm not sure if it'll suit my needs. – James Vaughan Apr 08 '17 at 13:08
  • Your project is a .NET Core project and some of the full .NET Framework API's are not available in these projects. If you want to base your code on the samples you link to on MSDN I suggest that you create projects that are labelled as _Windows Classic Desktop_. – Martin Liversage Apr 08 '17 at 13:19
  • I didn't realize, thanks. – James Vaughan Apr 08 '17 at 13:21

2 Answers2

0

You must also set the "Copy to output directory" property of "DATA10.txt" in Solution Explorer to "Copy Always"

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

    namespace _07___ReadTextFileWhile
    {
        class Program
        {
            static void Main(string[] args)
            {
                StreamReader myReader = new StreamReader("DATA10.txt");

                string line = "";

                while (line != null)
                {
                    line = myReader.ReadLine();
                    if (line != null)
                        Console.WriteLine(line);
                }

                myReader.Close();
                Console.ReadKey();

            }
        }
    }

enter image description here

Luís Chaves
  • 661
  • 7
  • 15
0

StreamReader is suppose to take in a Stream as its parameter can also take in a Stream as a parameter and you will also have to specify the FileMode.

Instead, try something like this:

public static void Main() 
{
    string path = @"c:\PathToFile\DATA10.txt";

    try 
    {        
        using (FileStream fs = new FileStream(path, FileMode.Open)) 
        {
            using (StreamReader sr = new StreamReader(fs)) 
            {
                 //blahblah    
            }
        }
    } 
    catch (Exception e) 
    {
        Console.WriteLine("The process failed: {0}", e.ToString());
    }
}

MSDN Reference

Timothy G.
  • 6,335
  • 7
  • 30
  • 46