Hello,

I am trying to write a simple program which should read the first line from the file s1,
and display it. When I run it , I get the message

cannot implicitly convert 'System.IO.Streamreader 'to 's1.Program'
which happens for the line Program re = new StreamReader("s1.txt");

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

namespace s1
{
    class Program
    {
        static void Main(string[] args)

        {
            Program re = new StreamReader("s1.txt");

            s = re.Readline();

            System.Console.WriteLine(s);
            System.Console.WriteLine("Hello, World!");


        }
    }
}

Program is a class, StreamReader is a constructor that constructs a class of type StreamReader. The Program class is not a StreamReader class.
After you figure that out, you'll get an error in line 4 :)

One great benefit of this site is the great number of questions that have already been answered that you can search through.
If you were to use the word StreamReader in the search window, you would probably get a multitude of examples.

Hmm, here is much work to be done.
You are really messing things.
This code does not even complie.

To do your simple program, you can do:

class Program
{
    static void Main(string[] args)
    {
        string filePath = @"C:\YourFolder\testFile.txt";
        StreamReader sr = new Streamreader(filePath);
        string line = "";
        for (int i = 0; i < 1; i++)
            line = sr.ReadLine();
        sr.Dispose();
        Console.WriteLine(line);
        Console.ReadLine();            
    }
}

thank you for all the suggesions.

I made the change but, C# still got the error.
to go around it I have changed the code abit.
Now I am getting System.String[] when i run it.

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

namespace s1
{
    class Program
    {
        static void Main(string[] args)

        {

            string[] lines = System.IO.File.ReadAllLines(@"C:\Users\sp\Documents\Visual Studio 2010\Projects\store1\st1\input_1.txt");
            System.Console.WriteLine(lines);



        }
    }
}

ok, it seems that C# can only read some lines from the file.

Now I am trying to figure out how to get the first word, from the first line.
for example if my line is rock your body 1233

how can I get the first word rock or the last word 1233 ?

I tried

System.Console.WriteLine(lines[0][0]);

but that will give the first letter from the first word

I also tried this way. but

for some reason the line s = line.Split(" "); seems not to work

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

namespace s1
{
    class Program
    {
        static void Main(string[] args)

        {

            string[] lines = System.IO.File.ReadAllLines(@"C:\Users\sp\Documents\Visual Studio 2010\Projects\store1\s1\s1.txt");
            System.Console.WriteLine(lines);
            System.Console.WriteLine(lines[0]);
            System.Console.WriteLine(lines[0][0]);

            foreach (string line in lines)
            {

               string s  = " ";
               s = line.Split(" ");
               System.Console.WriteLine(s);
            }





        }
    }
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}

String.Split returns an array, not a single string

foreach (String line in lines) {
    String[] parts = line.Split(' ');
    Console.WriteLine(parts[0]); // first part
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.