Basicaly i want to read a .txt file line by line.
As i read each line i wish to recognise any space characters between the doubles, for example
"12.1 34.478 67.9" as like something like X Y Z co-ordinates.
Once spaces are recognised, i can treat each double value independently, and can do specific calcuations to each.
currently im using this to read lines
string line = reader.ReadLine();
MessageBox.Show("Line Read: " + line + "");
I tried a array based approach like below, but the output was never successful.
char[] block = new char[3];
reader.ReadBlock(block, 0, 3);
Console.WriteLine(block);
MessageBox.Show("" + block + "");
Basic Finished working Code thanks to #Bartosz looks like
string line = reader.ReadLine();
var split = line.Split(' ');
var x = double.Parse(split[0]);
var y = double.Parse(split[1]);
var z = double.Parse(split[2]);
MessageBox.Show("X:" + x + " Y:" + y + " Z:" + z + "");
When you obtain your line, you could:
var split = line.Split(' ');
Then you can obtain each double:
var x = double.Parse(split[0]);
var y = double.Parse(split[0]);
var z = double.Parse(split[0]);
(or TryParse depending on your error handling needs, you should also check if your split is indeed of length 3).
Note: bewre of the locale settings on your OS, if your file use .as float pointing seperator, it mai fail on machine where , is set.
What about something like this?
var filename = "c:\\test.txt";
var allLines = File.ReadAllLines(filename);
var myList = new List<IEnumerable<double>>();
foreach (var line in allLines)
{
var currentLine = line.Split(' ').Select(i => double.Parse(i));
myList.Add(currentLine);
}
A little bit refactored :
var myList = File.ReadAllLines("c:\\test.txt")
.Select(line => line.Split(' ')
.Select(double.Parse))
.ToList();
http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx
using System;
using System.IO;
class Test
{
public static void Main()
{
try
{
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
then split using var stringlist = string.Split('')
and double.Parse every string in stringlist
Related
I am trying to making a simple tool Windows form app, that I can paste a list of 200 X,Y coordinates and then I can click a button and the tool will output a text file with a command on each line that will have the X,Y coordinates in it.
the input looks like this:
65737,163129
-21687,-27399
164089,50153
164649,63465
-28663,140057
-28951,110329
149833,-30231
and the output should look something like this:
waypoint:WLM 1:1:78168:~:1560:1:true:0:gui.xaero_default:false:0:false
waypoint:WLM 2:2:919432:~:154200:11:true:0:gui.xaero_default:false:0:false
waypoint:WLM 3:3:791080:~:15624:0:true:0:gui.xaero_default:false:0:false
waypoint:WLM 4:4:79288:~:16968:6:true:0:gui.xaero_default:false:0:false
waypoint:WLM 5:5:79064:~:155702:9:true:0:gui.xaero_default:false:0:false
This is what I have so far, I was able to parse the input into a array but now i have no idea how to combine it and save it as a text file.
private void button2_Click(object sender, EventArgs e)
{
var lines = richTextBox1.Text.Split((new char[] { ',' }), StringSplitOptions.RemoveEmptyEntries);
foreach (var s in lines)
{
string[] result = lines.ToArray();
outputwindow.Text = String.Join("\n", result); // parse string so they are all on new lines ( they are still in the array as one tho)
var cords = outputwindow.Text.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); // we split the data again and now the result is cords by them selves
foreach (var c in cords)
{
string[] cordstoarray = cords.ToArray(); // cordstoarry should now be the proper data set
//waypoint:name:initials:x:y:z:color:disabled:type:set:rotate_on_tp:tp_yaw:global
decimal startnumb = startingnumberb.Value;
// wc[1] + wc[0] + name + wc[0] + inl + wc[0] + cordstoarray[0] + wc[0] + wc[3] + wc[0] + cordstoarray[1] + wc[0] + wc[4]
for (int i = 0; i < cordstoarray.Length; i++)
{
string[] buildwaypoints = new string[13];
string[] wc = { ":", "waypoint", "~", "1:false:0:gui.xaero_default:false:0:false" };
string name = nametextbox.Text; // get the name
string inl = initialstextbox.Text; // get the initials
buildwaypoints[i] = { wc[1] , wc[0] , name , wc[0] , inl , wc[0] , cordstoarray[i] , wc[0] , wc[3] , wc[0] , cordstoarray[i + 1] , wc[0] , wc[4]}; // Build wapypoint array
File.WriteAllLines("Triwaypt.txt", buildwaypoints);
using (StreamReader sr = new StreamReader("Triwaypt.txt"))
{
string res = sr.ReadToEnd();
Console.WriteLine(res);
}
}
}
}
}
you can use File.WriteAllLines() - but in a slightly different way
first add List<string> outputLines = new List<string>(); to the start of your program (just after var lines = ...), so that you can start saving your generated lines (or you can use File.AppendAllText if you want to save memory)
Then when you generate your buildWayPoints array - just generate a string to add to the list instead. Something like
outputLines.Add($"{wc[1]}{wc[0]}{name}{wc[0]}{inl}{wc[0]}{cordstoarray[i]}{wc[0]}{wc[3]}{wc[0]}{cordstoarray[i + 1]}{wc[0]}{wc[4]}");//assuming your code up to this point was correct, also you can replace the {wc[n]} with the literal text if you feel like it
Should work, but I'd definitely reccomend cleaning up the code around that section. Finally after the loop is finished you need to write your new list of lines to file with something like File.WriteAllLines(#"Filepath", outputLines)
Assuming your question revolved around creating the output file, the above should work perfectly, if your question was actually about properly formatting the input you'd need to explain your logic a bit more clearly - but as a stab in the dark
counter++;//define counter as int counter = 0; earlier
var twoCords = c.Split(',');
var outputLine = $"waypoint:WLM {counter}:{counter}:{c[0]}:~:{c[1]}:{idk about this one sorry}:true:0:gui.xaero_default:false:0:false";
p.s. one immediate bug I need to mention is you did a foreach(var c in cords) but then you reference cords instead of c in your code
I'm having problem searching text with certain format. my file looks as below.
britney ak4564gc1 18
scott ak3333hc2 28
jenny ad4564gc3 32
amy ak4564gc4 29
I would like to search for worknumber with certain format which is dynamic. here is my code. the format must have specific length with * as change variable.
for (int line = 0; line <= Countline(OriFile)-1; line++)
{
var desiredText = File.ReadAllLines(OriFile).ElementAt(line);
string s = desiredText.ToString();
string b = s.Substring(WONUmStart, WONumLength);
//format changeable(I changed it to make it easier to understand)
if(b.Contains(a.TextBox.ToString())) //textbox value "ak****gc*"
{
if (WoNum != b)
{
WoNum = b;
StreamWriter sw = new StreamWriter(reportfile, true);
sw.WriteLine(Path.GetFileName(OriFile) +
" " +
WoNum +
" " +
Path.GetFileName(MergeFile));
sw.Flush();
sw.Close();
}
}
}
can anybody guide me?
I suggest using regular expressions with Linq, e.g.
string pattern = #"\bak.{3}gc.{1}\b";
var result = File
.ReadLines(OriFile)
.Select(line => Regex.Match(line, pattern)) // if we expect at most one match per line
.Where(match => match.Success)
.Select(match => match.Value);
...
File.WriteAllLines(reportfile, result.
Select(line => string.Format("{0} {1} {2}",
Path.GetFileName(OriFile), line, Path.GetFileName(MergeFile))));
Something I was curious about, I'm coding a utility for a old game that I play and this allows for custom NPC's. Long story short, I'm coding a reader for these custom NPC files. I've gotten most of the reading down with a line.contains() method (all code will be shown later) but there's a problem. The file can contain either just "height" or "gfxheight" which both do different things. Using line.contains("width") will make it output both width and gfxwidth twice. I don't really know any good way to explain it so here's the file:
width=32
height=32
gfxwidth=64
gfxheight=32
nofireball=1
noiceball=1
noyoshi=1
grabside=0
The Console output when I read it in and do what I need to split the lines and such:
And here's the code I use for height and gfxheight (of course there are others but these are the only problems I have when reading):
if (line.Contains("height"))
{
var split = line.Split(new char[] { '=' }, 2);
decimal dc;
//var val = int.Parse(split.ToString());
Console.WriteLine(split[0].ToString() + " is equal to " + split[1].ToString());
npcHeight.Value = Decimal.Parse(split[1].ToString());
npcHeight.Enabled = true;
npcHCb.Checked = true;
}
if (line.Contains("gfxheight"))
{
var split = line.Split(new char[] { '=' }, 2);
//var val = int.Parse(split.ToString());
Console.WriteLine(split[0].ToString() + " is equal to " + split[1].ToString());
}
Of course there's also the code for width and gfxwidth and the other various codes but I'm not going to bother posting those because I can apply what I get for the height to those.
So what would I have to do to differentiate between them? Suggestions?
Thanks in advanced,
Mike
Read the file into a string array, then parse it into a dictionary.
var file = File.ReadAllLines(yourFile);
var config = (from line in file
let s = line.Split('=')
select new { Key = s[0], Value = s[1] })
.ToDictionary(x => x.Key, x => x.Value);
Now you can access anything you want by referencing the key:
var gfxHeight = config["gfxheight"]; // gfxHeight is a string containing "32"
If you know the value after the = is always a number, you could parse it:
var config = (from line in file
let s = line.Split('=')
select new { Key = s[0], Value = int.Parse(s[1]) })
.ToDictionary(x => x.Key, x => x.Value);
var gfxHeight = config["gfxheight"]; // gfxHeight is an int containing 32
Instead of trying to figure out what each line is before splitting it, try splitting it first. This parsing approach leverages the format of the file and has a much-reduced dependency on its data:
foreach (var line in lines) {
var data = line.Split('=', 2);
if (data.Length != 2) {
continue;
}
var attrib = data[0];
var value = data[1];
Console.WriteLine(attrib + " is equal to " + value);
switch (attrib) {
case "height":
// ...
break;
case "gfxheight":
// ...
break;
}
}
I figured out a solution actually! Be warned: I haven't refreshed the page yet to see any proposed answers.
if (line.Contains("width"))
{
if (line.Contains("gfx"))
{
var split = line.Split(new char[] { '=' }, 2);
//var val = int.Parse(split.ToString());
Console.WriteLine(split[0].ToString() + " is equal to " + split[1].ToString());
npcWidth.Value = Decimal.Parse(split[1].ToString());
npcWidth.Enabled = true;
npcWCb.Checked = true;
}
else
{
var split = line.Split(new char[] { '=' }, 2);
Console.WriteLine(split[0].ToString() + " is equal to " + split[1].ToString());
pNpcWidth.Value = Decimal.Parse(split[1].ToString());
pNpcWidth.Enabled = true;
pNpcWidthCb.Checked = true;
}
}
Basically that ^
What it does is checks if the line is width with that line.Contains method. And if it does, it then checks to see if it contains gfx in it (as in gfxheight, gfxwidth, etc) and if it does, then that's the gfxheight or gfxwidth value. If not, it's the regular height/width.
I have a file. Each line looks like the following:
[00000] 0xD176234F81150469: foo
What I am attempting to do is, if a line contains a certain substring, I want to extract everything on the right of the substring found. For instance, if I were searching for 0xD176234F81150469: in the above line, it would return foo. Each string is of variable length. I am using C#.
As a note, every line in the file looks like the above, having a base-16 number enclosed in square brackets on the left, followed by a hexadecimal hash and a semicolon, and an english string afterwards.
How could I go about this?
Edit
Here is my code:
private void button1_Click(object sender, EventArgs e)
{
Form1 box = new Form1();
if(MessageBox.Show("This process may take a little while as we loop through all the books.", "Confirm?", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
XDocument doc = XDocument.Load(#"C:\Users\****\Desktop\books.xml");
var Titles = doc.Descendants("Title");
List<string> list = new List<string>();
foreach(var Title in Titles)
{
string searchstr = Title.Parent.Name.ToString();
string val = Title.Value;
string has = #"Gameplay/Excel/Books/" + searchstr + #":" + val;
ulong hash = FNV64.GetHash(has);
var hash2 = string.Format("0x{0:X}", hash);
list.Add(val + " (" + hash2 + ")");
// Sample output: "foo (0xD176234F81150469)"
}
string[] books = list.ToArray();
File.WriteAllLines(#"C:\Users\****\Desktop\books.txt", books);
}
else
{
MessageBox.Show("Aborted.", "Aborted");
}
}
I also iterated through every line of the file, adding it to a list<>. I must've accidentally deleted this when trying the suggestions. Also, I am very new to C#. The main thing I am getting stumped on is the matching.
You could use File.ReadLines and this Linq query:
string search = "0xD176234F81150469:";
IEnumerable<String> lines = File.ReadLines(path)
.Select(l => new { Line = l, Index = l.IndexOf(search) })
.Where(x => x.Index > -1)
.Select(x => x.Line.Substring(x.Index + search.Length));
foreach (var line in lines)
Console.WriteLine("Line: " + line);
This works if you don't want to use Linq query.
//"I also iterated through every line of the file, adding it to a list<>." Do this again.
List<string> li = new List<string>()
//However you create this string make sure you include the ":" at the end.
string searchStr = "0xD176234F81150469:";
private void button1_Click(object sender, EventArgs e)
{
foreach (string line in li)
{
string[] words;
words = line.Split(' '); //{"[00000]", "0xD176234F81150469:", "foo"}
if (temp[1] == searchStr)
{
list.Add(temp[2] + " (" + temp[1] + ")");
// Sample output: "foo (0xD176234F81150469)"
}
}
}
string file = ...
string search= ...
var result = File.ReadLines(file)
.Where(line => line.Contains(search))
.Select(line => line.Substring(
line.IndexOf(search) + search.Length + 1);
Unfortunately, none of the other solutions worked for me. I was iterating through the hashes using foreach, so I would be iterating through all the items millions of times needlessly. In the end, I did this:
using (StreamReader r = new StreamReader(#"C:\Users\****\Desktop\strings.txt"))
{
string line;
while ((line = r.ReadLine()) != null)
{
lines++;
if (lines >= 6)
{
string[] bits = line.Split(':');
if(string.IsNullOrWhiteSpace(line))
{
continue;
}
try
{
strlist.Add(bits[0].Substring(10), bits[1]);
}
catch (Exception)
{
continue;
}
}
}
}
foreach(var Title in Titles)
{
string searchstr = Title.Parent.Name.ToString();
string val = Title.Value;
string has = #"Gameplay/Excel/Books/" + searchstr + ":" + val;
ulong hash = FNV64.GetHash(has);
var hash2 = " " + string.Format("0x{0:X}", hash);
try
{
if (strlist.ContainsKey(hash2))
{
list.Add(strlist[hash2]);
}
}
catch (ArgumentOutOfRangeException)
{
continue;
}
}
This gave me the output I expected in a short period of time.
How to split this string
1014,'0,1031,1032,1034,1035,1036',0,0,1,1,0,1,0,-1,1
and get this string array as result
1014
'0,1031,1032,1034,1035,1036'
0
0
1
1
0
1
0
-1
1
in C#
I believe that this regex should give you what you are looking for:
('(?:[^']|'')*'|[^',\r\n]*)(,|\r\n?|\n)?
http://regexr.com?2vib4
EDIT:
Quick code snippet on how it might work:
var rx = new Regex("('(?:[^']|'')*'|[^',\r\n]*)(,|\r\n?|\n)?");
var text= "1014,'0,1031,1032,1034,1035,1036',0,0,1,1,0,1,0,-1,1";
var matches = rx.Matches(text);
foreach (Match match in matches)
{
System.Console.WriteLine(match.Groups[1].ToString());
}
try this,
string line ="1014,'0,1031,1032,1034,1035,1036',0,0,1,1,0,1,0,-1,1" ;
var values = Regex.Matches(line, "(?:'(?<m>[^']*)')|(?<m>[^,]+)");
foreach (Match value in values) {
Console.WriteLine(value.Groups["m"].Value);
}
This code is not pretty at all, but it works. :) (Does not work with multiple "strings" within the string.)
void Main()
{
string stuff = "1014,'0,1031,1032,1034,1035,1036',0,0,1,1,0,1,0,-1,1";
List<string> newStuff = new List<string>();
var extract = stuff.Substring(stuff.IndexOf('\''), stuff.IndexOf('\'', stuff.IndexOf('\'') + 1) - stuff.IndexOf('\'') + 1);
var oldExtract = extract;
extract = extract.Replace(',',';');
stuff = stuff.Replace(oldExtract, extract);
newStuff.AddRange(stuff.Split(new[] {','}));
var newList = newStuff;
for(var i = 0; i < newList.Count; i++)
newList[i] = newList[i].Replace(';',',');
// And newList will be in the format you specified, but in a list..
}
Firstly split a string on ' (single) quote and then after go for comma (,).
You don't need a parser, you don't need Regex. Here's a pretty simple version that works perfectly:
var splits = input
.Split('\'')
.SelectMany(
(s,i) => (i%2==0)
? s.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries)
: new[]{ "'" + s + "'"}
);
This is exactly what #AVD + #Rawling described ... Split on ', and split only "even" results, then combine.
using System;
using System.IO;
using Microsoft.VisualBasic.FileIO; //Microsoft.VisualBasic.dll
public class Sample {
static void Main(){
string data = "1014,'0,1031,1032,1034,1035,1036',0,0,1,1,0,1,0,-1,1";
string[] fields = null;
data = data.Replace('\'', '"');
using(var csvReader = new TextFieldParser(new StringReader(data))){
csvReader.SetDelimiters(new string[] {","});
csvReader.HasFieldsEnclosedInQuotes = true;
fields = csvReader.ReadFields();
}
foreach(var item in fields){
Console.WriteLine("{0}",item);
}
}
}