Reading information from a text file help

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 1223 Views - Last Post: 30 November 2015 - 09:47 AM Rate Topic: -----

#1 King Silver   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 28-November 15

Reading information from a text file help

Posted 28 November 2015 - 02:55 AM

So I've been learning a lot about C# recently and learned a lot from making several practice programs based on mathematical equations and I wanted to try something else involving text files and C#.
I've written up some code but it is giving me a LOT of errors and I don't really understand why.
I'm using the "using System.IO;" which I initially forgot about presuming it would fix my problem. I'll post what I am trying to do and then along with my code of what I am doing.

1: Read in a text file [any one on any computer] (from the debug location).
2: Read each value separated by a comma and turn each of these values into a double that I can use in a calculation or just anything.
3: For example, add the 8th value in the text file to the 11th value, just to test if it works.

Text file content and exact layout/format:
11
0.0,23.0,15.0
1.0,23.0,14.0
2.0,23.0,14.0
3.0,22.0,14.0
8.0,20.0,14.0
9.0,20.0,15.0
10.0,19.0,14.0
12.0,12.0,7.0
18.0,11.0,7.0
23.0,10.0,7.0
29.0,10.0,6.0


My code (which isn't doing what it is supposed to be doing for some reason and its driving me insane)

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

namespace Assignment_3
{
    class Program
    {
        static void Main(string[] args)
        {
            {
                string s;
                System.IO StreamReader inputFile = new System.IO StreamReader(DataFile);

                s = inputFile.ReadLine();
                int noDataLines = int.Parse(s);
                //this is just to test out the files
                Console.WriteLine("the file {0} contains {1} lines of data", DataFile, noDataLines);

                double[][] data = new double[noDataLines][];
                double[] xy;

                string[] ss;
                for (int = 0, i < noDataLines; i++)
                {
                    s = inputFile.ReadLine();
                    s.Split(',');
                    Console.WriteLine(s);
                    xy = new double[] { double.Parse(ss[0], double.Parse(ss[1])};

                }

                while (!inputFile.EndOfStream)

                {
                    s = inputFile.ReadLine();
                    Console.WriteLine(s);
                }
                inputFile.Close();
            }

        }
    }
}



What I've done to rectify this:
-Originally left out a semi colon, fixed this but code still represented errors
-Reformatted code to make it more readable
-Added "using System.IO;"

What I think the problem may be:
-I want this to work for any text file, the current text file I have in my debug folder is "riverdata1".txt
-Code may be in wrong set of {} brackets, but I don't think this is the case
-Missing small parts of code.


Once I manage to read in the file and convert the numbers in the text file to actual doubles I'll be able to manage the rest myself. Any help? I can't seem to advance in this current state.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading information from a text file help

#2 King Silver   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 28-November 15

Re: Reading information from a text file help

Posted 28 November 2015 - 09:32 AM

Anyone? no? Help would be appreciated and I'd love to return the favour sometime.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6912
  • View blog
  • Posts: 28,592
  • Joined: 12-December 12

Re: Reading information from a text file help

Posted 28 November 2015 - 09:57 AM

What happens with your code? What does it do, and not do?

This
s.Split(',');

doesn't do anything; you need to assign the result of this Split to something.

Then
xy = new double[] { double.Parse(ss[0], double.Parse(ss[1])};

you will be over-writing xy each time, not storing all the previous details.

ss doesn't contain anything, presumably this is what you should be storing the result of Split() into.

You also attempt to iterate the file twice, which won't happen because you are already at the end of the file.
Was This Post Helpful? 1
  • +
  • -

#4 King Silver   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 28-November 15

Re: Reading information from a text file help

Posted 28 November 2015 - 10:47 AM

I just want to be able to read in the file, take each value in the text file shown in the original post and convert it to a double or an integer so I can use it for calculations.
What is my code lacking or what is wrong with my code that C# isn't even understanding the very beginning of my code?

 
string s;
                    System.IO StreamReader inputFile = new System.IO StreamReader(DataFile);
   
                    s = inputFile.ReadLine();
                    int noDataLines = int.Parse(s);

                    //just to see if the code works
                    Console.WriteLine("the file {0} contains {1} lines of data", DataFile, noDataLines);

                    double[][] data = new double[noDataLines][];
                    double[] xy;

                    string[] ss;
                    for (int = 0, i < noDataLines; i++)
                    {
                        s = inputFile.ReadLine();
                        Console.WriteLine(s);
                        xy = new double[] { double.Parse(ss[0], double.Parse(ss[1])};

                    }



I am placing the code between this part of the original solution:
static void Main(string[] args)
        {
           }


is this correct to do so? I appreciate any help given

This post has been edited by andrewsw: 28 November 2015 - 10:50 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6912
  • View blog
  • Posts: 28,592
  • Joined: 12-December 12

Re: Reading information from a text file help

Posted 28 November 2015 - 10:51 AM

Note that you don't have to quote the previous post in full, there is a Reply button further down the page, or use the Fast Reply box.

Quote

that C# isn't even understanding the very beginning of my code?

Are you saying that the code doesn't compile? In which case, what errors do you get?
Was This Post Helpful? 1
  • +
  • -

#6 King Silver   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 28-November 15

Re: Reading information from a text file help

Posted 28 November 2015 - 10:52 AM

View Postandrewsw, on 28 November 2015 - 10:49 AM, said:

Note that you don't have to quote the previous post in full, there is a Reply button further down the page, or use the Fast Reply box.


Ah sorry about that!

this is the content of the text file:
11
0.0,23.0,15.0
1.0,23.0,14.0
2.0,23.0,14.0
3.0,22.0,14.0
8.0,20.0,14.0
9.0,20.0,15.0
10.0,19.0,14.0
12.0,12.0,7.0
18.0,11.0,7.0
23.0,10.0,7.0
29.0,10.0,6.0

I want to be able to read that in and turn each number into something I can use for calculations.
Was This Post Helpful? 0
  • +
  • -

#7 King Silver   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 28-November 15

Re: Reading information from a text file help

Posted 28 November 2015 - 11:21 AM

I've decided to take a new approach and completely edit my code.
Here is my new code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace SampleProject3
{
    class Program
    {
        static void Main(string[] args)
        {
            {
                //creates a streamreader that allows lake1data.txt to be read in
                StreamReader testing = new StreamReader("lake1data.txt");
                //creates an empty variable called "line"
                string line = "";
                //while line is not null, read each line
                while (line != null)
                {
                    line = testing.ReadLine();
                    if (line != null)
                        //if line is not null, write "line" to the console.
                        Console.WriteLine(line);

                }
                testing.Close();
                Console.ReadKey();
            }
        }
    }
}



I've also attached what the console output looks like now. Posted Image

Now what I am trying to do is turn each of the numbers in the text file to either 2 things.
1: Each value is an individual double
2: An Array containing all the values that are in the text file.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6912
  • View blog
  • Posts: 28,592
  • Joined: 12-December 12

Re: Reading information from a text file help

Posted 28 November 2015 - 12:28 PM

You were already exploring using a two-dimensional array, you can use one to store all the numbers.

You were also reading (or attempting to) the first value, which is what you need to do.

This post has been edited by andrewsw: 28 November 2015 - 12:29 PM

Was This Post Helpful? 0
  • +
  • -

#9 King Silver   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 28-November 15

Re: Reading information from a text file help

Posted 28 November 2015 - 12:43 PM

View Postandrewsw, on 28 November 2015 - 12:28 PM, said:

You were also reading (or attempting to) the first value, which is what you need to do.


"Attempting to" :ohno: but it wasn't working for me at all. I just read the chapter in my C# book about textfiles and that had no information on it either. This is driving me nuts.
How would I convert the values from the text file into an array in which I could extract the values and do as I please with them? i.e. add them, subtract them, use them in any formula I wished etc.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6912
  • View blog
  • Posts: 28,592
  • Joined: 12-December 12

Re: Reading information from a text file help

Posted 28 November 2015 - 01:10 PM

Actually, you were declaring a jagged [][] rather than two-dimensional [,] array. The differences are discussed here.
Was This Post Helpful? 0
  • +
  • -

#11 King Silver   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 28-November 15

Re: Reading information from a text file help

Posted 28 November 2015 - 01:14 PM

View Postandrewsw, on 28 November 2015 - 01:10 PM, said:

Actually, you were declaring a jagged [][] rather than two-dimensional [,] array. The differences are discussed here.


Interesting, you know how my text file has 3 lines across and 12 lines down (with the exception of the first line of course)

11
0.0,23.0,15.0
1.0,23.0,14.0
2.0,23.0,14.0
3.0,22.0,14.0
8.0,20.0,14.0
9.0,20.0,15.0
10.0,19.0,14.0
12.0,12.0,7.0
18.0,11.0,7.0
23.0,10.0,7.0
29.0,10.0,6.0



Wouldn't a jagged array be more useful here? I understand how to use basic arrays and how to store numbers, what is confusing me is once I have read in the text file how do I make the program understand the values inside that file are what I want to be put into the array? :/
Thank you.
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6912
  • View blog
  • Posts: 28,592
  • Joined: 12-December 12

Re: Reading information from a text file help

Posted 28 November 2015 - 01:23 PM

I'm in two minds. I'd prefer a 2-d array because the dimensions are precisely defined. However, with a jagged array we can do something like this:
            int size = 11;

            double[][] test1 = new double[size][];

            test1[0] = Array.ConvertAll("5.0,4.0,8.0".Split(','), double.Parse);

That is, filling a row in a single statement.

However, I think this is just syntactic sugar, and I'm probably only favouring it because it looks cool.

If you use a 2-d array then you need to first split a line into a string array, then iterate these strings converting them to doubles and inserting the values into the correct array-element. This isn't too onerous and will be clearer (and easier to debug and error check).

This post has been edited by andrewsw: 28 November 2015 - 01:29 PM

Was This Post Helpful? 0
  • +
  • -

#13 King Silver   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 28-November 15

Re: Reading information from a text file help

Posted 28 November 2015 - 01:40 PM

Is this code any better for what I am doing? I've made an array now that works except there is just 2 things I am uncertain about with it.
1: Is it actually taking each of the values in the array.
2: An error on this piece of code: ss = s.Split(new char[] { ' ', ', ' }, StringSplitOptions.RemoveEmptyEntries);
it says: "too many characters in character literal"

string filename = "lake1data.txt";
                //creates a streamreader that allows lake1data.txt to be read in
                StreamReader InputFile = new StreamReader(filename);

                // the string variable to store a line of the file
                string s;
                // declare a string array that will contain the array of sub-strings
                string[] ss;
                // read the file line-by-line until the EOF is reached
                while (!InputFile.EndOfStream)
                {
                    // read in the next line of the file
                    s = InputFile.ReadLine();
                    // use the Split() method to divide the string into sub-strings,
                    // delimited at a space or comma
                    // ignore blank sub-strings
                    ss = s.Split(new char[] { ' ', ', ' }, StringSplitOptions.RemoveEmptyEntries);
                    // write out each sub-string to the console
                    for (int i = 0; i < ss.Length; i++)
                        Console.WriteLine(ss[i]);
                    //testing if the array actually stored the values
                    Console.WriteLine("the 4th value of the text file is {5}", ss[0]);
                }

                InputFile.Close();



As you can see I've written a line to test the values contained in the array, however it doesn't seem to be working.
Was This Post Helpful? 0
  • +
  • -

#14 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6912
  • View blog
  • Posts: 28,592
  • Joined: 12-December 12

Re: Reading information from a text file help

Posted 28 November 2015 - 01:49 PM

A char is a single character, ', ' (comma and space) is two characters, a string.

Quote

it doesn't seem to be working.

That tells us nothing. What happens? What doesn't happen?

This post has been edited by andrewsw: 28 November 2015 - 01:49 PM

Was This Post Helpful? 1
  • +
  • -

#15 King Silver   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 28-November 15

Re: Reading information from a text file help

Posted 28 November 2015 - 01:51 PM

Posted Image

this is the image, also on the console only the number "11" from the text file is being displayed. Is this clearer? :)/>

This post has been edited by andrewsw: 28 November 2015 - 01:56 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2