C# Check EOF in text file to fix array - c#

I have a code to read text file and save the data into a double array to plot graph:
string filename = openFileDialog1.FileName;
var lineCount = 0;
using (var reader = File.OpenText(#filename))
{
string line;
while ((line = reader.ReadLine()) != null)
{
var data = line.Split(',');
GlobalDataClass.dDataArray[lineCount, 0] = double.Parse(data[0]);
GlobalDataClass.dDataArray[lineCount, 1] = double.Parse(data[1]);
lineCount++;
}
ShowGraphData(lineCount);
}
And I have created a public class that initiate the array to [2000,2]:
static class GlobalDataClass
{
public static double[,] dDataArray = new double[2000, 2];
public static long iTotalReadingPoint;
}
My text file will be like:
0,29
1,31
2,32
3,32
4,30
However I want my program to detect the EOF so that the text file can contain random rows and still able to plot the graph not restricted to 2000 rows. Is it possible?please advice.TQ

If you want to use same class then try given code. This is not most optimum memory usage wise but still this should work.
string filename = openFileDialog1.FileName;
var lineCount = 0;
while ((line = reader.ReadLine()) != null)
{
if (GlobalDataClass.dDataArray.GetLength(0) == lineCount)
{
double[,] newTemp = GlobalDataClass.dDataArray;
int increaseLenght = newTemp.Length + 1000;
GlobalDataClass.dDataArray = new double[increaseLenght, 2];
Array.Copy(newTemp, GlobalDataClass.dDataArray, newTemp.LongLength);
}
var data = line.Split(',');
GlobalDataClass.dDataArray[lineCount, 0] = double.Parse(data[0]);
GlobalDataClass.dDataArray[lineCount, 1] = double.Parse(data[1]);
lineCount++;
}

The StreamReader class has a boolean property named EndOfStream that is essentially EoF for FileStream objects, and there's the Peek() method which is usually the standard way of reading until EoF.
var reader = File.OpenText( ... );
while( reader.Peek() != -1 ) //Peek() returns -1 on EoF or if the stream is not seekable.
{
var line = reader.ReadLine();
...
}
However reading from the file isn't really the problem if you don't want to be restricted to 2,000 lines. You might consider using a generic container of some flavour. For example:
using System.Collections.Generic;
string line = "12,34";
var dDataList = new List<KeyValuePair<double, double>>();
string[] parts = line.Split( ',' );
dDataList.Add( new KeyValuePair<double, double>( Double.Parse( parts[0] ), Double.Parse( parts[1] ) ) );
...
foreach( var pair in dDataList )
Console.WriteLine( "{0} => {1}", pair.Key, pair.Value ); // 12 => 34
Will let you add as many pairs of doubles as you want, within reason, without having to fiddle with array resizing/copying or any of that nasty business. It's generally considered good practice to use a container like List<T> when dealing with an unknown amount of data, and to use arrays when you know exactly how much data you're going to have or the absolute maximum amount of data you can have.

I think you're asking your question a bit wrong; The problem you have is that you are declaring an array of 2000 unit fixed length, but you actually want it to be dynamic length. As Abhinav said, a list may work for you:
firstly, you might consider creating a class/struct for your coordinates:
public class Coordinate
{
public double x;
public double y;
}
Then, create a list of coordinates (of 0 length initially)...
static class GlobalDataClass
{
public static List<Coordinate> dDataArray = new List<Coordinate>();
public static long iTotalReadingPoint;
}
And then add Coordinate objects to your list as you find new lines..
string filename = openFileDialog1.FileName;
var lineCount = 0;
using (var reader = File.OpenText(#filename))
{
string line;
while ((line = reader.ReadLine()) != null)
{
var data = line.Split(',');
Coordinate coord = new Coordinate();
coord.x = double.Parse(data[0]);
coord.y = double.Parse(data[1]);
GlobalDataClass.dDataArray.Add(coord);
lineCount++;
}
ShowGraphData(lineCount);
}

Related

Separate string to array

I try to get a file's content into an array in C#.
My program is similar to a shopping list.
I'm not able to store the data as a JSON file cause of some requirements.
I decided to store the data like:
3x water
2x fish
I grabbed the data using a simple StreamReader and got all the data into a string.
I tried to separate the string by \n using split and store the data into a string array.
Now I wanted to split each string again to get the quantity of the product.
I tried splitting each index using a foreach and storing them in a second array. I also tried to store the data in the same array.
I want to get something like
string[] cars = {"3", "water", "2", "fish"};
or store the data in a list.
Is my attempt a bad one?
Is there a way to do it much more easily?
Easy is a very subjective requirement.
You could write it in one statement with a variant of Split that restricts it to splitting at most once (so you can use an x in a description), and uses SelectMany to fold down all the lines into a single array of strings - as you asked.
string[] cars = File.ReadAllLines("input.txt")
.SelectMany(line => line.Split(new char[] { 'x' }, 2))
.Select(s => s.Trim())
.ToArray();
...but I wouldn't.
I think that maintainability is important. This code is understandable, useful, and obvious.
using System;
using System.Collections.Generic;
using System.IO;
class Program
{
public class Car
{
private int quantity;
private string description;
public Car(int quantity, string description)
{
this.quantity = quantity;
this.description = description;
}
public override string ToString() => $"{quantity}x {description}";
}
static void Main(string[] args)
{
// Use a list to store all the entries.
List<Car> cars = new List<Car>();
string[] lines = File.ReadAllLines("input.txt");
// Parse each line of the file.
foreach (var line in lines)
{
// Ignore completely blank lines.
if (string.IsNullOrWhiteSpace(line))
continue;
// Find the delimiter 'x'.
int pos = line.IndexOf('x');
// Handle case where no delimiter is present.
if (pos < 0)
throw new FormatException("The line is in an invalid format because it does not contain an 'x'.");
// Split the string into two parts at the delimiter position.
string firstPart = line.Substring(0, pos); // everything before the delimiter
string lastPart = line.Substring(pos + 1); // everything after the delimiter
// Interpret the first part as an integer.
if (!int.TryParse(firstPart, out int quantity))
throw new FormatException("The quantity is not a number.");
// Disallow zero or less for quantities.
if (quantity < 1)
throw new InvalidDataException("The quantity is not a positive number.");
// Trim whitespace from the description.
string description = lastPart.Trim();
// Require a non-empty description.
if (string.IsNullOrWhiteSpace(description))
throw new InvalidDataException("The description is missing.");
cars.Add(new Car(quantity, description));
}
}
}
If we suppose that each item has it's own line inside the file, this solution goes through each line and separates the quantity and the name of the item, after that these are stored in an array.
private static string[] GetData()
{
using (StreamReader sr = new StreamReader("source.in"))
{
int numberOfItems = File.ReadAllLines("source.in").Length;
string[] Data = new string[numberOfItems * 2];
int DataIndex = -1;
string line;
while ((line = sr.ReadLine()) != null)
{
//Separate the things
int index = line.IndexOf('x');
string num = line.Substring(0, index++);
Data[++DataIndex] = num;
string itm = line.Substring(++index);
Data[++DataIndex] = itm;
}
return Data;
}
}

difference between Metatrader CSV and C# StreamWriter CSV. How to solve it?

I have a Code in Metatrader that writes some Quotes to CSV, but while doing same in C# the Expert advisor reads the values a different way....
This Code in MetaEditor writes the CSV file:
li_40 = FileOpen(ls_32, FILE_CSV|FILE_WRITE|FILE_SHARE_READ, ";");
if (li_40 > 0) {
FileWrite(li_40, ls_16);
FileClose(li_40);
This Writes in C#:
List<string> listB = new List<string>();
using (StreamReader reader = new StreamReader(File.OpenRead(oFile)))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
listB.Add(reader.ReadLine());
}
reader.Close();
}
using (StreamWriter swOut = new StreamWriter(oFile))
{
foreach (var item in listB)
{
swOut.Write(item);
swOut.Write(Environment.NewLine);
}
for (int i = 0; i <= gridIn.Columns.Count - 1; i++)
{
if (i > 0)
{
swOut.Write(", ");
}
value = dr.Cells[i].Value.ToString();
string vals = dr.Cells[2].Value.ToString();
int priceDecimalPlaces = vals.Split('.').Count() > 1
? vals.Split('.').ToList().ElementAt(1).Length
: 0;
string nell = "0";
if (priceDecimalPlaces == 3)
{
nell = "0.001";
}
if (priceDecimalPlaces == 5)
{
nell = "0.00001";
}
if (priceDecimalPlaces == 4)
{
nell = "0.0001";
}
//replace comma's with spaces
value = value.Replace(',', ' ');
//replace embedded newlines with spaces
value = value.Replace(Environment.NewLine, "");
If the difference between the C# double and the Metatrader's current double value is 0.12098-0.12096=2, the Metatrader won't see the value as 2 but something very much higher like 18,17 and so on, but writing this same value from the MetaTrader's code gives correct value...
I read the CSV using _lread:
uchar chBuff[1024];
int res = _lread(hFile, chBuff, 1);
//
//CreateFileA(
res = _lread(hFile, chBuff, 350);
ls_308 = CharArrayToString(chBuff, 0, res, CP_UTF8);
//Alert(Ls_84);
ls_308=StringSubstr(ls_308,0,StringFind(ls_308,"\r\n",0));
if (_lclose(hFile)<0) Print("Error closing");
I think there are some difference between C# doubles on Metatrader and normal Metatrader doubles
The error was caused by the System's encoding language which was different to UTF-8 that was specified in the Metatrader code
ls_308 = CharArrayToString(chBuff, 0, res, CP_UTF8);
So, I changed the UTF to All Code Page assigned by System by doing this
ls_308 = CharArrayToString(chBuff, 0, res, CP_ACP);
Hope someone find this useful..

Replace the start of line in a file quickly

I have an initial file containing lines such as:
34 964:0.049759 1123:0.0031 2507:0.015979
32,48 524:0.061167 833:0.030133 1123:0.002549
34,52 534:0.07349 698:0.141667 1123:0.004403
106 389:0.013396 417:0.016276 534:0.023859
The first part of a line is the class number. A line can have several classes.
For each class, I create a new file.
For instance for class 34 the resulting file will be :
+1 964:0.049759 1123:0.0031 2507:0.015979
-1 524:0.061167 833:0.030133 1123:0.002549
+1 534:0.07349 698:0.141667 1123:0.004403
-1 389:0.013396 417:0.016276 534:0.023859
For class 106 the resulting file will be :
-1 964:0.049759 1123:0.0031 2507:0.015979
-1 524:0.061167 833:0.030133 1123:0.002549
-1 534:0.07349 698:0.141667 1123:0.004403
+1 389:0.013396 417:0.016276 534:0.023859
The problem is I have 13 files to write for 200 class.
I already ran a less optimized version of my code and it took several hours.
With my code below it takes 1 hour to generate the 2600 files.
Is there a way to perform such a replacement in a faster way? Are regex a viable option?
Below is my implementation (works on LINQPAD with this data file)
static void Main()
{
const string filePath = #"C:\data.txt";
const string generatedFilesFolderPath = #"C:\";
const string fileName = "data";
using (new TimeIt("Whole process"))
{
var fileLines = File.ReadLines(filePath).Select(l => l.Split(new[] { ' ' }, 2)).ToList();
var classValues = GetClassValues();
foreach (var classValue in classValues)
{
var directoryPath = Path.Combine(generatedFilesFolderPath, classValue);
if (!Directory.Exists(directoryPath))
Directory.CreateDirectory(directoryPath);
var classFilePath = Path.Combine(directoryPath, fileName);
using (var file = new StreamWriter(classFilePath))
{
foreach (var line in fileLines)
{
var lineFirstPart = line.First();
string newFirstPart = "-1";
var hashset = new HashSet<string>(lineFirstPart.Split(','));
if (hashset.Contains(classValue))
{
newFirstPart = "+1";
}
file.WriteLine("{0} {1}", newFirstPart, line.Last());
}
}
}
}
Console.Read();
}
public static List<string> GetClassValues()
{
// In real life there is 200 class values.
return Enumerable.Range(0, 2).Select(c => c.ToString()).ToList();
}
public class TimeIt : IDisposable
{
private readonly string _name;
private readonly Stopwatch _watch;
public TimeIt(string name)
{
_name = name;
_watch = Stopwatch.StartNew();
}
public void Dispose()
{
_watch.Stop();
Console.WriteLine("{0} took {1}", _name, _watch.Elapsed);
}
}
The output:
Whole process took 00:00:00.1175102
EDIT: I also ran a profiler and it looks like the split method is the hottest spot.
EDIT 2: Simple example:
2,1 1:0.8 2:0.2
3 1:0.4 3:0.6
12 1:0.02 4:0.88 5:0.1
Expected output for class 2:
+1 1:0.8 2:0.2
-1 1:0.4 3:0.6
-1 1:0.02 4:0.88 5:0.1
Expected output for class 3:
-1 1:0.8 2:0.2
+1 1:0.4 3:0.6
-1 1:0.02 4:0.88 5:0.1
Expected output for class 4:
-1 1:0.8 2:0.2
-1 1:0.4 3:0.6
-1 1:0.02 4:0.88 5:0.1
I have eliminated the hottest paths from your code by removing the split and using a bigger buffer on the FileStream.
Instead of Split I now call ToCharArray and then parse the first Chars to the first space and while I'm at it a match with classValue on a char by char basis is performed. The boolean found indicates an exact match for anything before the , of the first space. The rest of the handling is the same.
var fsw = new FileStream(classFilePath,
FileMode.Create,
FileAccess.Write,
FileShare.None,
64*1024*1024); // use a large buffer
using (var file = new StreamWriter(fsw)) // use the filestream
{
foreach(var line in fileLines) // for( int i = 0;i < fileLines.Length;i++)
{
char[] chars = line.ToCharArray();
int matched = 0;
int parsePos = -1;
bool takeClass = true;
bool found = false;
bool space = false;
// parse until space
while (parsePos<chars.Length && !space )
{
parsePos++;
space = chars[parsePos] == ' '; // end
// tokens
if (chars[parsePos] == ' ' ||
chars[parsePos] == ',')
{
if (takeClass
&& matched == classValue.Length)
{
found = true;
takeClass = false;
}
else
{
// reset matching
takeClass = true;
matched = 0;
}
}
else
{
if (takeClass
&& matched < classValue.Length
&& chars[parsePos] == classValue[matched])
{
matched++; // on the next iteration, match next
}
else
{
takeClass = false; // no match!
}
}
}
chars[parsePos - 1] = '1'; // replace 1 in front of space
var correction = 1;
if (parsePos > 1)
{
// is classValue before the comma (or before space)
if (found)
{
chars[parsePos - 2] = '+';
}
else
{
chars[parsePos - 2] = '-';
}
correction++;
}
else
{
// is classValue before the comma (or before space)
if (found)
{
// not enough space in the array, write a single char
file.Write('+');
}
else
{
file.Write('-');
}
}
file.WriteLine(chars, parsePos - correction, chars.Length - (parsePos - correction));
}
}
Instead of iterating over the un-parsed lines 200 times, how about parsing the lines upfront into a data structure then iterating over that 200 times? This should minimize the numer of string manipulation operations.
Also using StreamReader instead of File.ReadLines, so the entire file is not in memory twice -- once as string[] and another time as Detail[].
static void Main(string[] args)
{
var details = ReadDetail("data.txt").ToArray();
var classValues = Enumerable.Range(0, 10).ToArray();
foreach (var classValue in classValues)
{
// Create file/directory etc
using (var file = new StreamWriter("out.txt"))
{
foreach (var detail in details)
{
file.WriteLine("{0} {1}", detail.Classes.Contains(classValue) ? "+1" : "-1", detail.Line);
}
}
}
}
static IEnumerable<Detail> ReadDetail(string filePath)
{
using (StreamReader reader = new StreamReader(filePath))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
int separator = line.IndexOf(' ');
Detail detail = new Detail
{
Classes = line.Substring(0, separator).Split(',').Select(c => Int32.Parse(c)).ToArray(),
Line = line.Substring(separator + 1)
};
yield return detail;
}
}
}
public class Detail
{
public int[] Classes { get; set; }
public string Line { get; set; }
}

Reading lines from a text file, converting them, then writing back to new file

I have some basic knowledge of C#, but I am having trouble coding something that seems simple in concept. I want to read a file (.asm) containing values such as
#1
#12
#96
#2
#46
etc.
on multiple consecutive lines. I then want to get rid of the # symbols (if they are present), convert the remaining number values to binary, then write these binary values back to a new file (.hack) on their own lines. There isn't a set limit on the number of lines, which is my biggest issue as I don't know how to check for lines dynamically. So far I can only read and convert lines if I code to look for them, then I can't figure out how to write these values on their own lines in the new file. Sorry if this sounds a bit convoluted, but any help would be appreciated. Thanks!
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
var line = File.ReadAllText(openFileDialog1.FileName);
using (StreamWriter sw = File.CreateText("testCode.hack"))
{
var str = line;
var charsToRemove = new string[] {"#"};
foreach (var c in charsToRemove)
{
str = str.Replace(c, string.Empty);
}
int value = Convert.ToInt32(str);
string value2 = Convert.ToString(value, 2);
if (value2.Length < 16)
{
int zeroes = 16 - value2.Length;
if(zeroes == 12)
{
sw.WriteLine("000000000000" + value2);
}
}
else
{
sw.WriteLine(value2);
}
}
This code should help you get going real fast:
static void Main(string[] args)
{
string line = string.Empty;
System.IO.StreamReader reader = new System.IO.StreamReader(#"C:\test.txt");
System.IO.StreamWriter writer = new System.IO.StreamWriter(#"C:\test.hack");
while ((line = reader.ReadLine()) != null) // Read until there is nothing more to read
{
if (line.StartsWith("#"))
{
line = line.Remove(0, 1); // Remove '#'
}
int value = -1;
if (Int32.TryParse(line, out value)) // Check if the rest string is an integer
{
// Convert the rest string to its binary representation and write it to the file
writer.WriteLine(intToBinary(value));
}
else
{
// Couldn't convert the string to an integer..
}
}
reader.Close();
writer.Close();
Console.WriteLine("Done!");
Console.Read();
}
//http://www.dotnetperls.com/binary-representation
static string intToBinary(int n)
{
char[] b = new char[32];
int pos = 31;
int i = 0;
while (i < 32)
{
if ((n & (1 << i)) != 0)
{
b[pos] = '1';
}
else
{
b[pos] = '0';
}
pos--;
i++;
}
return new string(b);
}
My suggestion create a List<string>. Here are steps
Read input (.asm) file into List
Open StreamWriter for output (.hack) file.
Loop through List<string> modify the string and write into file.
Code Example:
List<string> lstInput = new List<string>();
using (StreamReader reader = new StreamReader(#"input.asm"))
{
string sLine = string.Empty;
//read one line at a time
while ((sLine = reader.ReadLine()) != null)
{
lstInput.Add(sLine);
}
}
using (StreamWriter writer = new StreamWriter(#"output.hack"))
{
foreach(string sFullLine in lstInput)
{
string sNumber = sFullLine;
//remove leading # sign
if(sFullLine.StartsWith("#"))
sNumber = sFullLine.Substring(1);
int iNumber;
if(int.TryParse(sNumber, out iNumber))
{
writer.WriteLine(IntToBinaryString(iNumber));
}
}
}
public string IntToBinaryString(int number)
{
const int mask = 1;
var binary = string.Empty;
while(number > 0)
{
// Logical AND the number and prepend it to the result string
binary = (number & 1) + binary;
number = number >> 1;
}
return binary;
}
Reference: IntToBinaryString method.
NOTE: Int to Binary String method mentioned in the answer of #TheDutchMan is better choice.

Reading x,y values from a .txt file in c#

I'm trying to read x and y values from a text file into a string array where the line is being split on the ','
However, when I run this code I get an error saying the index was out of the bounds of the array on the first element. I've tried using temporary strings to store the data and then convert them but I still get the same error on the second element. here is my code I have implemented without the temporary strings.
string line;
while ((line = coordStream.ReadLine()) != null)
{
string[] temp = new string[2];
temp[0] = "";
temp[1] = "";
temp = line.Split(',');
trees[count].X = Convert.ToInt16(temp[0]);
trees[count].Y = Convert.ToInt16(temp[1]);
count++;
}
Here is the code with the temporary storage too:
string line;
while ((line = coordStream.ReadLine()) != null)
{
string[] temp = new string[2];
temp[0] = "";
temp[1] = "";
temp = line.Split(',');
string xCoord = temp[0];
string yCoord = temp[1];
trees[count].X = Convert.ToInt16(xCoord);
trees[count].Y = Convert.ToInt16(yCoord);
count++;
}
I know this seems a trivial error but I cannot seem to get this working. If I debug and step through the array manually it works but when I don't step through it(i.e let the program run) these errors are thrown
EDIT: The first 10 lines of data are as follows:
654,603
640,583
587,672
627,677
613,711
612,717
584,715
573,662
568,662
564,687
There are no empty lines in the text file.
As pointed out by Jon Skeet, removing the temp assignments seems to have fixed this error. However even with the assignments it should still have worked. The following code sample inside the while loop works:
string[] temp;
temp = line.Split(',');
trees[count].X = Convert.ToInt16(temp[0]);
trees[count].Y = Convert.ToInt16(temp[1]);
count++;
The number of trees is known but I'd like to thank everyone for their input. Expect more questions in the near future :D
Try using a List<Point> for your trees collection instead of an array. This will help if you don't know the correct count upfront.
var trees = new List<Point>();
while (...)
{
...
trees.Add(new Point(x, y));
}
The second possible issue is when the input line does not contain valid data (for example, is empty). Often the last line with data ends with a newline thus the last line is empty.
while ((line = coordStream.ReadLine()) != null)
{
var temp = line.Split(',');
if (temp.Length != 2)
continue;
....
}
var lineContents = File.ReadAllLines("").Select(line => line.Split(',')).Where(x => x.Count() == 2);
var allTrees = lineContents.Select(x => new Trees() { X = Convert.ToInt16(x[0]), Y = Convert.ToInt16(x[1]) });

Categories

Resources