Hi,
I've read various topics on this particular thing and have got nothing working, I just can't seem to get my head around it.

I've got a file with two strings and a decimal, separated by commas.
I want to read them in to a linked list so that they can be processed etc. from user input.

I can get the streamreader bit.

The only bit of code worth posting here is only one line as I do not know where to go for lines with multiple parts. The streamreader is fairly easy:

StreamReader fromFile = new StreamReader("../../convert.txt");

Any help would be appreciated, I hope I don't seem overly lazy or anything but I realy don't know where to go from here.

Recommended Answers

All 6 Replies

The exact solution (means an effective one) depends on what you want to do with the data.
Sample code:

private void button4_Click(object sender, EventArgs e)
        {
            LinkedList<string> list = new LinkedList<string>();
            // path to your file... in my example I placed the file in program directory
            string path = Environment.CurrentDirectory + "\\convert.txt";
            FileStream file = null;
            StreamReader reader = null;
            string fileContent = String.Empty;
            // that array consists of only 1 character as you have your data separated by commas
            char[] delimiters = { ',' };

            // here I read data from the file
            if (File.Exists(path))
            {
                file = new FileStream(path, FileMode.Open, FileAccess.Read);
            }
            else
            {
                MessageBox.Show("The file with data does not exist!");
                return;
            }
            try
            {
                reader = new StreamReader(file);
                fileContent = reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }

            // as you've got the fileContent you can split it into tokens (parts)
            string[] tokens = fileContent.Split(delimiters);
            // as the tokens array contains the needed parts you can add them to your list
            for (int i = 0; i < tokens.Length; i++)
            {
                list.AddLast(tokens[i]);
            }

            // do something usefull with the list
        }

Thank you so much for the prompt reply, I don't know how much of the code this changes but I forgot to mention that I'm doing a console application, not a forms application. I know that shouldn't change much but I thought I'd metion that.

I say this because some of the syntax that I think is forms application specific is confusing me slightly. And yes, my file is in the project directory along with Program.cs

The fact that you create console app only change two things. Firstly you have to create a method to put in the code (you can also leave all of the code in the main() but in that way, probably, it will become very long - difficult to understand and to track errors).
Then you have to replace MessageBox.Show() with Console.WriteLine

MessageBox.Show("An error occurred: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

Console.WriteLine("An error occurred: " + ex.Message);

In the end of the Main() you can add Console.ReadLine() to see the results.
btw Environment.CurrentDirectory gets/sets the fully qualified path of the current WORKING directory so of the directory from which you start your app (so where the exe file is placed)... so if you use F5 or Ctrl+F5 in Visual to debug or start without debugging you refer to "...ProjectName\bin\Debug" (of course assuming you did not change anything from standard settings).

Again thank you for the reply, I will try this and let you know the results.

Ok so I've looked around, tried a lot of things and looked back at your code and see the thing that's missing, any easy way of incorporating the "delimiters" aspect of things into my following code?

if (File.Exists("convert.txt"))
            {
                StreamReader srFromFile = new StreamReader("convert.txt");
                string strUnitName1 = srFromFile.ReadLine();
                do
                {
                    conversioninfo newCI = new conversioninfo(strUnitName1, srFromFile.ReadLine(), Double.Parse(srFromFile.ReadLine()));
                    alConversions.Add(newCI);

                    strUnitName1 = srFromFile.ReadLine();
                } while (strUnitName1 != null);
                srFromFile.Close();
            }

I don't want anything over complicated and my tutor taught us very similar code to that, simply reading through the lines whilst accounting for comma delimiters as well is what I'm looking for.

Thanks for any further help and I really appreciate the help thus far. =]

To be honest I'm a little bit lost... I'm not sure what you're heading to... I only guess that you've got a file with data organized as follows:
string1,string2,double\n
string1,string2,double\n
...
string1,string2,double
then you've got a class conversioninfo which should hold infos from file and a list of its instances...
if so probably you want to do something like this:

public class conversionInfo
        {
            string name;
            string type;
            double version;

            public conversionInfo(string name, string type, double version)
            {
                this.name = name;
                this.type = type;
                this.version = version;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            LinkedList<conversionInfo> list = new LinkedList<conversionInfo>();
            string path = Environment.CurrentDirectory + "\\convert.txt";
            StreamReader reader = null;
            char[] delimiters = { ';' };

            if (!(File.Exists(path)))
            {
                MessageBox.Show("The file with data does not exist!");
                return;
            }

            try
            {
                reader = new StreamReader(path);
                string line = String.Empty;
                while ((line = reader.ReadLine()) != null)
                {
                    string[] tokens = line.Split(delimiters);
                    conversionInfo conInfo = new conversionInfo(tokens[0], tokens[1], Double.Parse(tokens[2]));
                    list.AddLast(conInfo);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }

            // do something usefull with the list
        }
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.