Jump to content

CPU: Intel Core i5 4690K @ 4.6Ghz CPU Cooler: Noctua NH-D15 GPU: GTX 1070 TI RAM: Crucial Ballistix Tactical 16GB (4x4) Mobo: ASUS Z97-PRO(Wi-Fi ac) PSU: Corsair RM Series RM750 Case: Fractal Design Define R4 no window

Link to post
Share on other sites

the most simple way. I asked because those examples looked a bit to unnecessary

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to post
Share on other sites

the most simple way. I asked because those examples looked a bit to unnecessary

no, this is as simple as it gets. reading a text file in c# really is goes like this. it's a bit weird but then again, can I ask why exactly you want to read a text file? why not use a database?

CPU: Intel Core i5 4690K @ 4.6Ghz CPU Cooler: Noctua NH-D15 GPU: GTX 1070 TI RAM: Crucial Ballistix Tactical 16GB (4x4) Mobo: ASUS Z97-PRO(Wi-Fi ac) PSU: Corsair RM Series RM750 Case: Fractal Design Define R4 no window

Link to post
Share on other sites

no, this is as simple as it gets. reading a text file in c# really is goes like this. it's a bit weird but then again, can I ask why exactly you want to read a text file? why not use a database?

I just started learning c#, so there are some exercises that need reading from a file. And i dont know what a database is :D

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to post
Share on other sites

Depends on the format but if you have discrete lines then something as simple as this:

    class Program    {        static void Main(string[] args)        {            Array.ForEach(File.ReadAllLines("Resources/Text.txt"), Console.WriteLine);            Console.ReadKey();        }    }

Given the input text file:

In the sea without leesStandeth the bird of HermesEating his wings variableAnd maketh himself yet full stableWhen all his feathers be from him goneHe standeth still here as a stoneHere is now both white and redAnd all so the stone to quicken the deadAll and some without fableBoth hard and soft and malleableUnderstand now well and rightAnd thank you God of this sightThe bird of Hermes is my name eating my wings to make me tame.

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

I wish there were better tutorials/books out there, when i tried to learn C# i could never understand how apps write config files for example, read and understand every line thats not commented and understands the values given ex: "width= 1920" next line "height =1080", and the program understands those as parameters from the config file.

Just reading and showing/formatting text seems not so hard.

Link to post
Share on other sites

 

Depends on the format but if you have discrete lines then something as simple as this:

    class Program    {        static void Main(string[] args)        {            Array.ForEach(File.ReadAllLines("Resources/Text.txt"), Console.WriteLine);            Console.ReadKey();        }    }

Given the input text file:

In the sea without leesStandeth the bird of HermesEating his wings variableAnd maketh himself yet full stableWhen all his feathers be from him goneHe standeth still here as a stoneHere is now both white and redAnd all so the stone to quicken the deadAll and some without fableBoth hard and soft and malleableUnderstand now well and rightAnd thank you God of this sightThe bird of Hermes is my name eating my wings to make me tame.

But what if i have a text file that contains not just a string in line or ints, but a mixed line like:

 

25 minutes contain 1500 seconds

30 minutes contain 1800 seconds

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to post
Share on other sites

But what if i have a text file that contains not just a string in line or ints, but a mixed line like:

 

25 minutes contain 1500 seconds

30 minutes contain 1800 seconds

 

As I said, it depends on the format. What are you trying to accomplish?

 

If you are interested in configuration then XML and especially using LINQ to XML is by far the better way to go since you can have much better control over your own serialization.

 

If it's just a text file that you want to process then if you know it's structure to begin with (or even if it's dynamic) then just use string.Split and then something like <whatever>.parse/tryparse or Convert if the type has support.

 

You also the power of LINQ for doing things like (very simple example):

<input>.Any(char.IsDigit);string(<input>.Where(char.IsDigit).ToArray());

There's also Regex support.

 

Lots of tools to play with right out of the box.

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

MisterWhite,

 

It sounds like you are just trying to do some simple file manipulation.  (Sounds like you are a "relative" newbie to programming)

 

C# interacts with input/output via what is called Streams.  Without getting too technical, this is an abstraction that allows data to be sent back and forth using a common theme.

 

The simplest (and most efficient) way to read a text file (with lines like your example) would be:

 

    class Program    {        static void Main(string[] args)        {            // Using is a construct you will want to research.  Basically, it opens and closes the file            // for you without you needing to            using(StreamReader rdr = new StreamReader("{your filepath here}"))            {                // EndOfStream will allow you to identify when you are at the "End of a stream", i.e.                 // you have finished reading the file (in this case)                while(!rdr.EndOfStream)                {                    // ReadLine does what it's name implies.  It will read a line of text in, one at a time.                    // hence why we need to use the while loop above to read the entire file                    string line = rdr.ReadLine();                    // Do something here to extract your values from the string                }            }        }    }

You will need to add a using statement at the top, of:

using System.IO;

https://msdn.microsoft.com/en-us/library/System.IO.StreamReader(v=VS.110).aspx  (Info on streamreader)

 

From what it sounds like, you'll also need to parse that string to get values out of it.  If so, look up "String.split", and "Int.Parse"

 

Most base and simple types, (int, decimal, datetime, etc..) have a parse method you can use to parse the data you need from a string and get it in the proper format.

Link to post
Share on other sites

-snip-

 

It should probably be said (since you are coming from a C++ background) that the using block/call to Dispose() on something implementing IDisposable is about as close as it gets to deterministic destruction in C# i.e. it doesn't and any attempts to make it so is an abuse of the language and undoubtedly points to a questionable design.

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×