0

I've been given a few different sets of procedures for various different things in C# under file manipulation.

I have forgotten basically how to call or use the procedures and so they are pretty much useless to me until I figure out how. Apologies for sounding stupid but I've done as much searching as I can and I can't relate other sources to my problem.

Here's a procedure I've been given:

void readFromTextFile(string path)
    {

        StreamReader sr = new StreamReader(path);

        //Read the first line of text
        string line = sr.ReadLine();

        //Continue to read until you reach end of file
        while (line != null)
        {
            //write the line to console window
            Console.WriteLine(line);
            //Read the next line
            line = sr.ReadLine();
        }

        //close the file
        sr.Close();
    }

Now I understand completely what this and all the other procedures do, yet I forget how to use them in main.

Here's what I currently have in main:

string path = "C:\\Users\\Joe\\Documents\\General\\College\\Computer Science\\Coding\\TextFileWork\\textFile.txt";
string readFile;
readFile = readFromTextFile(path);

Now the problem I'm having is understanding how to use the procedure back into main to return the read file. The string readFile is what I am trying to append the read text into, however I don't know how I should call the function in order to append it. Some basic help should suffice, thanks!

EDIT:

Here's the entire code I currently have (C# Console Application)

namespace TextFileWork_03._03._18
{
class Program
{


    static void Main(string[] args)
    {
        string path = "C:\\Users\\Joe\\Documents\\General\\College\\Computer 
Science\\Coding\\TextFileWork\\textFile.txt";
        string readFile;
        readFromTextFile(readFile);

        if (File.Exists(path) == true)
        {
            //Create a file to write to.
            Console.WriteLine(path + " Exists");
        }
        else
        {
            Console.WriteLine(path + " File not found");
        }

        FileInfo fi = new FileInfo(path);
        FileStream fs = fi.Create();

        fs.Close();

        if (File.Exists(path) == true)
        {
            //Create a file to write to.
            Console.WriteLine(path + " Now exists");
        }
        else
        {
            Console.WriteLine(path + " File still not found");
        }









    }
    static void readFromTextFile(string path)
    {

        StreamReader sr = new StreamReader(path);

        //Read the first line of text
        string line = sr.ReadLine();

        //Continue to read until you reach end of file
        while (line != null)
        {
            //write the line to console window
            Console.WriteLine(line);
            //Read the next line
            line = sr.ReadLine();
        }

        //close the file
        sr.Close();
    }

}
}
  • Possible duplicate of [How to make method call another one in classes C#?](https://stackoverflow.com/questions/16226444/how-to-make-method-call-another-one-in-classes-c) – Stefan Mar 03 '18 at 16:36
  • Being new to programming, I'm not entirely sure what any of the things mean in that thread and also have nobody to explain it to me at this current time, which is why I am asking for help. – Joe Podmore Mar 03 '18 at 16:40
  • Hi Joe, I can recommend the excellent (free) Microsoft Virtual Academy course https://mva.microsoft.com/en-us/training-courses/c-fundamentals-for-absolute-beginners-16169 – PhillipH Mar 03 '18 at 16:41
  • I appreciate that and I will look into it whenever I have some free time, however right now I'd just like help with this one single line. – Joe Podmore Mar 03 '18 at 16:42
  • I think, your `void readFromTextFile(string path)` lives in a `class` (lives in means something like is between `{ }` somewhere). You can only call `readFromTextFile` from a) withing the class it lives in, or b) through a concrete object of the class. So, please show us the definition of the `class`, preferably it's name (and possibly constructor). – Stefan Mar 03 '18 at 16:45
  • Alright, I have updated the main question with my entire code, I believe that what you are talking about is the "class" at the top? I'm not entirely sure. – Joe Podmore Mar 03 '18 at 16:50
  • Ok, never mind; you should have posted the whole code to start with... the keyword `static` is crucial in this context... which makes my answer worthless ;-) – Stefan Mar 03 '18 at 16:54

2 Answers2

0

update this answer is not valid, I'll just keep it for a few minutes for reference.

You should have given more of your code to actually make this a valid question. Nevertheless, I'll try to help you out.

Your procedure (we call it methods in C#, (strongly related to functions)), lives in a class, lets call it Foo, but you can look it up in your code. Just scroll up: its the first blue class you'll see.

public class Foo //this is your class
{
    void readFromTextFile(string path)
    {
        StreamReader sr = new StreamReader(path);

        //Read the first line of text
        string line = sr.ReadLine();

        //Continue to read until you reach end of file
        while (line != null)
        {
            //write the line to console window
            Console.WriteLine(line);
            //Read the next line
           line = sr.ReadLine();
        }

        //close the file
        sr.Close();
    }
}

To call it, from your Main method, you'll need an object

void Main()
{
    string path = "C:\\Users\\Joe\\Documents\\General\\College\\ComputerScience\\Coding\\TextFileWork\\textFile.txt";
    string readFile;

    Foo fooObject = new Foo(); //create a new Foo
    readFile = fooObject.readFromTextFile(path);
}
Stefan
  • 17,448
  • 11
  • 60
  • 79
0

To make your code work you'll need a return value from your method, otherwise you cannot say:

mysomthing = readFromTextFile

So, lets return a string value: There is one problem: do you want to return a single line or just the whole file?

Here's the whole file version:

static string readFromTextFile(string path)
{

    StreamReader sr = new StreamReader(path);
    StringBuilder sb = new StringBuilder();
    //Read the first line of text
    string line = sr.ReadLine();

    //Continue to read until you reach end of file
    while (line != null)
    {
        //write the line to console window
        Console.WriteLine(line);
        //Read the next line
        line = sr.ReadLine();
        sb.AppendLine(line);
    }

    //close the file
    sr.Close();
    return sb.ToString();
}
Stefan
  • 17,448
  • 11
  • 60
  • 79
  • Thanks but you mentioning that the keyword static is crucial helped me to completely work out where I went wrong, thank you very much though :) – Joe Podmore Mar 03 '18 at 16:58