My text file is delimited (2 fields (text & numeric)). I'm trying to get the output into columnar format and include a count of the # of records following the columnar format. My text file is separated by commas. So far I can only read the text file. Here is

namespace ConsoleApplication5
{
    public class Class1
    {

        static void Main(string[] args)
        {

            String line;
            {
                //Pass the file path and file name to the StreamReader constructor
                StreamReader sr = new StreamReader("data.txt");

                //Read the first line of text
                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();
                Console.ReadLine();
            }
        }
    }
}

Recommended Answers

All 14 Replies

What means columnar format?
You mean one value under another?
PS: would you like to have an array of items?

I'm making a console app that reads data in and writes to the screen. It should come from a delimited text file, the output should be in columnar format (separated neatly in columns)Text Ex.

year,1997, 1998           output: year  1997    1998
date,   Nov.15, Sep.16            date  Nov.15  Sept.16

Text file is delimited. Array, yes.
Maybe something like this:

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileString;

            Console.SetBufferSize(800, 600);

            while ((fileString = Console.ReadLine()) != null)
            {
                string[] fsArray = fileString.Split(',');
                Console.WriteLine("{0, 15} | {1, -70} | {2, 15}", fsArray[0], fsArray[1], fsArray[2]);
            }
        }
    }
}

I did what I was thinking you want it to do:

class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, int> dir = new Dictionary<string, int>();
            using (StreamReader sr = new StreamReader(@"C:\1\testFile.txt"))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    string[] array = line.Split(',');
                    dir.Add(array[0].ToString(), Convert.ToInt32(array[1]));
                }
            }
            Show(dir);
        }

        private static void Show(Dictionary<string, int> dir)
        {
            foreach (KeyValuePair<string, int> line in dir)
            {
                Console.WriteLine("KEY: {0}, VALUE: {1}", line.Key, line.Value);
            }
            Console.ReadLine();
        }
    }

Hope it helps, if not, let me know.
bye,

Mitja

Thanks!

I did what I was thinking you want it to do:

class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, int> dir = new Dictionary<string, int>();
            using (StreamReader sr = new StreamReader(@"C:\1\testFile.txt"))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    string[] array = line.Split(',');
                    dir.Add(array[0].ToString(), Convert.ToInt32(array[1]));
                }
            }
            Show(dir);
        }

        private static void Show(Dictionary<string, int> dir)
        {
            foreach (KeyValuePair<string, int> line in dir)
            {
                Console.WriteLine("KEY: {0}, VALUE: {1}", line.Key, line.Value);
            }
            Console.ReadLine();
        }
    }

Hope it helps, if not, let me know.
bye,

Mitja

Got this error:Index was outside the bounds of the array. my txt.file is as follows:

2002-03,
STAT, Value NR CR,
Team Games, 33 - -
Games Played, 33 - -
Games Started, 30 - -
Min Per Game, 30.7 542 20
Minute Pct, 76.1 482 21
Possession Pct, 19.9 - 28
Floor Pct, 50.4 719 27
Offensive Rating, 121.5 37 1
Shot Pct, 23.9 542 14
Effective FG Pct, 54.4 341 10
Tr Shooting Pct, 59.8 190 3
FG Made, 149 310 14
FG Att, 361 212 11
FG Pct, 41.3 557 19
3pt FG, 95 21 1
3pt FG Att, 238 26 2
3pt FG Pct, 39.9 135 6
FT Made, 102 231 8
FT Att, 111 449 17
FT Pct, 91.9 - -
Free Throw Rate, 30.7 721 24
Points, 495 145 8
Pts Per Game, 15.0 264 9
2pt FG Point Pct, 21.8 - 29
3pt FG Point Pct, 57.5 90 3
FT Point Pct, 20.6 510 17
Rebounds, 81 - 54
RPG, 2.5 - 55[/QOUTE]

Hmm regarding your text file, you cannot have integer as the value parameter. Change the int to string (in the dictonary).

like that:

static void Main(string[] args)
        {
            Dictionary<string, string> dir = new Dictionary<string, string>();
            using (StreamReader sr = new StreamReader(@"C:\1\testFile.txt"))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    string[] array = line.Split(',');
                    dir.Add(array[0], array[1]);
                }
            }
            Show(dir);
        }

        private static void Show(Dictionary<string, string> dir)
        {
            foreach (KeyValuePair<string, string> line in dir)
            {
                Console.WriteLine("KEY: {0}, VALUE: {1}", line.Key, line.Value);
            }
            Console.ReadLine();
        }

... let me know if is it workin, ok?
Mitja

Hmm regarding your text file, you cannot have integer as the value parameter. Change the int to string (in the dictonary).

like that:

static void Main(string[] args)
        {
            Dictionary<string, string> dir = new Dictionary<string, string>();
            using (StreamReader sr = new StreamReader(@"C:\1\testFile.txt"))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    string[] array = line.Split(',');
                    dir.Add(array[0], array[1]);
                }
            }
            Show(dir);
        }

        private static void Show(Dictionary<string, string> dir)
        {
            foreach (KeyValuePair<string, string> line in dir)
            {
                Console.WriteLine("KEY: {0}, VALUE: {1}", line.Key, line.Value);
            }
            Console.ReadLine();
        }

... let me know if is it workin, ok?
Mitja

]An Error message occurred: Index was outside the bounds of the array.

dir.Add(array[0],array[1]);

Is there something wrong with the text file? It is delimited is it not? I'm making the text file more simple. Can you test this? Thanks!

2002-03,
STAT, Value
Team Games, 33
Games Played, 33
Games Started, 30
Min Per Game, 30.7
Minute Pct, 76.1
Possession Pct, 19.9
Floor Pct, 50.4
Offensive Rating, 121.5
Shot Pct, 23.9
Effective FG Pct,
Tr Shooting Pct,
FG Made, 149
FG Att, 361
FG Pct, 41.3
3pt FG, 95
3pt FG Att, 238
3pt FG Pct, 39.9
FT Made, 102
FT Att, 111
FT Pct, 91.9
Free Throw Rate, 30.7
Points, 495
Pts Per Game, 15.0
2pt FG Point Pct, 21.8
3pt FG Point Pct, 57.5
FT Point Pct, 20.6
Rebounds, 81
RPG, 2.5

I have tried your text and it works good.
You have to set your own path (full path) to the file. Mine is:@"C:\1\testFile.txt". You set your own.

I have tried your text and it works good.
You have to set your own path (full path) to the file. Mine is:@"C:\1\testFile.txt". You set your own.

I have set my own. It's not a path error I'm getting. Look at last error I posted. i do not know why it is not working.

As I said, I have put your text into my file, and it all works fine. No errors at all.
I dont know what can be wrong on our side.

I have tried your text and it works good.
You have to set your own path (full path) to the file. Mine is:@"C:\1\testFile.txt". You set your own.

Could this be the problem? Last question sorry.

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

I keep getting that same error-index was outside the bounds of the array, out of range.

If this is the error, that means that one of 2 arrays (array[0] or array[1]) but mostly would be array[1] cannot get filled, becuase there is no data for it. Thats means that in the "line" there was no comma (,) and the string couldnt get splitted. There is only one string - but the adding code requires 2 strings.

Please check your file, if there is a comma on EVERY single line.
And please put a break point to go through the code line by line. And tell me exactly when this error occures (which line is in that moment in the streamReader).

Break point is thta red dot, which you put it by clicking on the far left side of the text editor. When code comes to break point, go with pressing F11 line by line.

commented: Very helpful!!! +11

If this is the error, that means that one of 2 arrays (array[0] or array[1]) but mostly would be array[1] cannot get filled, becuase there is no data for it. Thats means that in the "line" there was no comma (,) and the string couldnt get splitted. There is only one string - but the adding code requires 2 strings.

Please check your file, if there is a comma on EVERY single line.
And please put a break point to go through the code line by line. And tell me exactly when this error occures (which line is in that moment in the streamReader).

Break point is thta red dot, which you put it by clicking on the far left side of the text editor. When code comes to break point, go with pressing F11 line by line.

I got it! Somehow when I tweaked the old file to make it easier to work with it became my down fall. I recopied the original and it works! My bad, idiot move! Now thats said and done, do you know how to add a count of the number of records following the columnar output. Like count: 25? Thank you!!!

This how:

class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, string> dir = new Dictionary<string, string>();
            using (StreamReader sr = new StreamReader(@"C:\1\testFile.txt"))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    string[] array = line.Split(',');
                    dir.Add(array[0], array[1]);
                }
            }
            Show(dir);
        }

        private static void Show(Dictionary<string, string> dir)
        {
            int counter = 0;
            foreach (KeyValuePair<string, string> line in dir)
            {
                Console.WriteLine("KEY: {0}, VALUE: {1}", line.Key, line.Value);
                counter++;
            }
            Console.WriteLine("Total number of records: {0}", counter.ToString());
            Console.ReadLine();
        }
    }
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.