C# Help, Reading Files

02/26/2010 23:15 xScott#1
Hey, i cant seem to get my head around this,

What im trying to do is, Read a line from a file but, i want to single out each word and save it into a String array, using the "Trim()" command? but i just cant do it, if you can point me in the right direction it would be nice ;D
02/26/2010 23:19 tanelipe#2
PHP Code:
string[] ReadFile(string Path) {
    if(
File.Exists(Path)) {
        return 
File.ReadAllText(Path).Split('\n');
    }
    return 
null;

Something like that? This one reads the whole file though.

PHP Code:
string[] ReadFile(string Pathint Line) {
    if(
File.Exists(Path)) {
        return 
File.ReadAllLines(Path)[Line].Split('\n');
    }
    return 
null;

This one reads the specified line.
02/26/2010 23:25 xScott#3
Quote:
Originally Posted by tanelipe View Post
PHP Code:

string
[] ReadFile(string Path) {
    if(
File.Exists(Path)) {
        return 
File.ReadAllText(Path).Split('\n');
    }
    return 
null;

Something like that?
ermm, i think so, im not sure xD ill give it a try ;o
02/27/2010 00:13 PeTe Ninja#4
well tanel he said each word your splitting each line

you could do like
C# aint open so just writing
Code:
string[] lines = File.ReadAllLines("mylines.txt");

foreach (string line in lines)
{
    string[] words = line.split(' ');

}
02/27/2010 01:00 xScott#5
Quote:
Originally Posted by PeTe Ninja View Post
well tanel he said each word your splitting each line

you could do like
C# aint open so just writing
Code:
string[] lines = File.ReadAllLines("mylines.txt");

foreach (string line in lines)
{
    string[] words = line.split(' ');

}
thanks both of you :]

Code:
 string[] lines = File.ReadAllLines(@"C:\Users\ADVENT\Documents\Questions.txt");
            foreach (string line in lines)
            {
                string[] words = line.Split(' ');
                for (int i = 0; i < words.Length; i++)
                {
                    Console.WriteLine(words[i]);
                }

            }
got all the words to show in console, cheers its what i wanted ;]
02/27/2010 01:38 PeTe Ninja#6
Quote:
Originally Posted by xScott View Post
thanks both of you :]

Code:
 string[] lines = File.ReadAllLines(@"C:\Users\ADVENT\Documents\Questions.txt");
            foreach (string line in lines)
            {
                string[] words = line.Split(' ');
                for (int i = 0; i < words.Length; i++)
                {
                    Console.WriteLine(words[i]);
                }

            }
got all the words to show in console, cheers its what i wanted ;]
Here is another pretty cool file viewer :)

i made it in C++ :D

Check attachment..
02/27/2010 01:40 xScott#7
im getting confused, 1element in the array holds 3 words but in differant lines, so its possible to save the 3 words into 3 differant string variables?

and thats pretty decent, you make it yourself?
02/27/2010 01:48 PeTe Ninja#8
Quote:
Originally Posted by xScott View Post
im getting confused, 1element in the array holds 3 words but in differant lines, so its possible to save the 3 words into 3 differant string variables?

and thats pretty decent, you make it yourself?
yeah i was learning C++ a while ago from youtube videos and it had a guide somewhat similar and then i made this one a bit more advanced so it works good but yeah i made it :D

anyway on your thing is this what you kind of need? 3 diff words 3 diff strings

Code:
string a = "";
            string b = "";
            string c = "";

            string[] lines = File.ReadAllLines(@"C:\Users\ADVENT\Documents\Questions.txt");
            foreach (string line in lines)
            {
                string[] words = line.Split(' ');
                for (int i = 0; i < words.Length; i++)
                {
                    a = words[0];
                    b = words[1];
                    c = words[2];
                    Console.WriteLine(words[i]);
                }

            }
02/27/2010 01:59 xScott#9
Quote:
Originally Posted by PeTe Ninja View Post
yeah i was learning C++ a while ago from youtube videos and it had a guide somewhat similar and then i made this one a bit more advanced so it works good but yeah i made it :D

anyway on your thing is this what you kind of need? 3 diff words 3 diff strings

Code:
string a = "";
            string b = "";
            string c = "";

            string[] lines = File.ReadAllLines(@"C:\Users\ADVENT\Documents\Questions.txt");
            foreach (string line in lines)
            {
                string[] words = line.Split(' ');
                for (int i = 0; i < words.Length; i++)
                {
                    a = words[0];
                    b = words[1];
                    c = words[2];
                    Console.WriteLine(words[i]);
                }

            }
thanks! ill play around with it for abit, so i get abit more familiar with it, cheers for your help :)
02/27/2010 02:07 PeTe Ninja#10
np =]
02/27/2010 03:51 InfamousNoone#11
Quote:
Originally Posted by xScott View Post
Hey, i cant seem to get my head around this,

What im trying to do is, Read a line from a file but, i want to single out each word and save it into a String array, using the "Trim()" command? but i just cant do it, if you can point me in the right direction it would be nice ;D
This is not necessarily the most efficient way to do it but...
Code:
// using System.IO
// using System.Collections.Generic
public static string[] ReadAllWords(string File)
{
	// Note: An exception will be thrown here if the file does not exist
	string[] Lines = File.ReadAllLines(File);
	List<string> Words = new List<string>();
	foreach (string line in Lines)
	{
		foreach (string s in line.Split(' '))
			Words.Add(s);
	}
	return Lines.ToArray();
}
02/27/2010 04:00 PeTe Ninja#12
Quote:
Originally Posted by InfamousNoone View Post
This is not necessarily the most efficient way to do it but...
Code:
// using System.IO
// using System.Collections.Generic
public static string[] ReadAllWords(string File)
{
	// Note: An exception will be thrown here if the file does not exist
	string[] Lines = File.ReadAllLines(File);
	List<string> Words = new List<string>();
	foreach (string line in Lines)
	{
		foreach (string s in line.Split(' '))
			Words.Add(s);
	}
	return Lines.ToArray();
}
well if it aint infamous..where u been hiding
02/27/2010 04:03 xScott#13
Quote:
Originally Posted by InfamousNoone View Post
This is not necessarily the most efficient way to do it but...
Code:
// using System.IO
// using System.Collections.Generic
public static string[] ReadAllWords(string File)
{
	// Note: An exception will be thrown here if the file does not exist
	string[] Lines = File.ReadAllLines(File);
	List<string> Words = new List<string>();
	foreach (string line in Lines)
	{
		foreach (string s in line.Split(' '))
			Words.Add(s);
	}
	return Lines.ToArray();
}
cheers ill give it a go xD
02/27/2010 04:05 PeTe Ninja#14
Quote:
Originally Posted by xScott View Post
cheers ill give it a go xD
:)


infamous is it better to use a list or an array?
02/27/2010 04:34 xScott#15
I was playing around with it for abit and managed to make it do what i wanted :
Code:
public static string[] ReadAllWords()
        {
            
            // Note: An exception will be thrown here if the file does not exist
            string[] Lines = File.ReadAllLines(@"C:\Users\ADVENT\Documents\Questions.txt");
            List<string> Words = new List<string>();
            foreach (string line in Lines)
            {
                foreach (string s in line.Split(' '))
                    Words.Add(s);
            }
            string[] ArrayWords = new string[Words.Count];
            Words.CopyTo(ArrayWords);
            for(byte i = 0; i < ArrayWords.Length ; i++)
            {
                Console.WriteLine(ArrayWords[i]);
            }
            return Lines.ToArray();
        }
its probably terrible, but im working with stuff i aint used before such as lists. ill have a play around with it xD