I have a file contains two lines . and in which line there is a double parameter .
I want to read both lines from the file and save them in an array of doubles .
I used the C# code below , but It doesn't work . It doesn't read anything and the array is empty after running the code .
Anybody has any idea where did I do wrong ?
Thanks for help .
private FileStream input;
double[] arr;
int i = 1;
input = new FileStream(Application.StartupPath+"\\City.txt", FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(input);
while (!reader.EndOfStream)
{
arr[i] = Convert.ToDouble(reader.ReadLine());
i++;
}
reader.Close();
This is a complete example of what you are doing.
string line;
List<double> values = new List<double>();
string path = Path.Combine(Application.StartupPath, "City.txt");
System.IO.StreamReader file = new System.IO.StreamReader(path);
while((line = file.ReadLine()) != null)
{
values.Add(double.Parse(line));
}
file.Close();
Based on "How to: Read a Text File One Line At a Time (MSDN)"
try this approach
using (StreamReader sr = File.OpenText(Application.StartupPath+"\\City.txt"))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
arr[i] = Convert.ToDouble(line);
i++;
}
}
and you should at least initialize arr: arr = new double[_size] and i should be zero because arrays in c# are zero based. And better use generic collection like List<T>(List<double> in this case).
The issue is while (!reader.EndOfStream) because when you initially read it in the position is at the end of the file. This is solidified by the fact that the line arr[i] should fail because you've not initialized the array (in fact, it shouldn't even compile...). So, how about this:
double[] arr = new double[2];
...
reader.BaseStream.Position = 0;
while (!reader.EndOfStream)
{
arr[i] = Convert.ToDouble(reader.ReadLine());
i++;
}
However, a more straight forward approach would be something like this:
var arr = new List<double>();
var lines = File.ReadAllLines(Application.StartupPath+"\\City.txt");
foreach (var l in lines)
{
arr.Add(Convert.ToDouble(l));
}
return arr.ToArray();
Another option is use File.ReadAllLines, considering that file size is small.
string[] stringDoubles = File.ReadAllLines(path, Encoding.UTF8);
for(int i=0;i<stringDoubles.Length;i++)
arr[i] = Convert.ToDouble(stringDoubles[i]);
The code as you posted will not compile, because you have not initialized your array, as well as having a visibility modifier on your FileStream. I'd guess this code is from two different locations in your project.
However, there's a much simpler way to do this: File.ReadAllLines
string path = #"c:\dev\text.txt"; //your path here
string[] lines = File.ReadAllLines(path);
double[] doubles = new double[2];
for (int i = 0; i < doubles.Length; i++)
{
double d;
if (double.TryParse(lines[i], out d))
doubles[i] = d;
}
Related
I am reading from a file and I am trying to skip first two lines and start reading from the third one. I've checked other questions which were answered but none of them worked on unity for some reason. I get several errors however it should work.
StreamReader reader = new StreamReader(path);
string line = "";
while ((line = reader.ReadLine()) != null)
{
string[] words = line.Split(' ');
string type = words[0];
float x = float.Parse(words[1]);
....
}
If I understand correctly, we can try to use File.ReadAllLines which will return all line of text content from your file text and then start reading on the third line (array start as 0, so that the third line might be contents[2]).
var contents = File.ReadAllLines(path);
for (int i = 2; i < contents.Length; i++)
{
string[] words = contents[i].Split(' ');
string type = words[0];
float x = float.Parse(words[1]);
}
If we know the Encoding of the file we can try to set Encoding to the second parameter in File.ReadAllLines
Similar to D-Shih's solution, is one using File.ReadLines, which returns an IEnumerable<string>:
var lines = File.ReadLines(path);
foreach (string line in lines.Skip(2))
{
string[] words = line.Split(' ');
string type = words[0];
float x = float.Parse(words[1]);
// etc.
}
The benefit of this approach over D-Shih's is that you don't have to read the entire file into memory at once to process it, so this solution is analogous to your existing solution's use of StreamReader.
As a solution for directly fixing your problem, you just need to call ReadLine twice before getting into the loop (to skip the two lines), though I'd argue the solution above is more legible:
using (StreamReader reader = new StreamReader(path))
{
string line = "";
// skip 2 lines
for (int i = 0; i < 2; ++i)
{
reader.ReadLine();
}
// read file normally
while ((line = reader.ReadLine()) != null)
{
string[] words = line.Split(' ');
string type = words[0];
float x = float.Parse(words[1]);
....
}
}
Notice that I've also wrapped the reader in a using, so that the file handle will be closed & disposed of once the loop completes, or in case of an exception being thrown.
I am trying to make code, that will basically open desired file, store all lines into array, and then, if line does fulfill condition, line will be save into new text file. When running program, I get error message to line reader, that index is outside of array. Please help, I am new to C#
int NumLines = new StreamReader(OpenFile.FileName).ReadToEnd().Split(new char[] { '\n' }).Length;
StreamReader Data = new StreamReader(OpenFile.FileName);
string[] arr = new string[NumLines];
for (int i = 0; i <= NumLines; i++)
{
StreamWriter File = new StreamWriter("C://Users//Tomas//Desktop//BC//tmp//Data.txt");
arr[i] = Data.ReadLine();
Index should be less than array size:
for (int i = 0; i < NumLines; i++)
^
But you don't need to put lines into array in order to write them to other file. E.g. if you want to write only some lines by condition:
File.WriteAllLines(newFileName, File.ReadLines(OpenFile.FileName).Where(condition));
Where condition is a delegate which checks whether line should be written. E.g. if you want to write only lines which start with "Foo":
.Where(line => line.StartsWith("Foo"))
NOTE: If you want to write all lines to new file without condition, then you simply can copy original file:
File.Copy(OpenFile.FileName, newFileName);
UPDATE: From your comments, it's clear that you need to find line which matches some condition and also write to output 6th and 8th lines after that line. You still can do that without dumping all source file into memory:
private static IEnumerable<string> GetVertexCoordinates(string fileName)
{
var enumerator = File.ReadLines(fileName).GetEnumerator();
while (enumerator.MoveNext())
{
if (enumerator.Current == "VERTEX")
{
yield return enumerator.Current;
for (var i = 0; i < 6; i++) enumerator.MoveNext(); // skip 6 lines
yield return enumerator.Current;
for (var i = 0; i < 2; i++) enumerator.MoveNext(); // skip 2 more lines
yield return enumerator.Current;
}
}
}
And usage is still one-liner
File.WriteAllLines(newFileName, GetVertexCoordinates(OpenFile.FileName));
Try this way:
string[] lines = File.ReadAllLines(filepath);
Then, if you need to compare all lines of this file with some YOUR line, you can do this way:
string targetFilePath = "C://Users//Tomas//Desktop//BC//tmp//Data.txt";
string specificLine = "line, which you try to find";
foreach (var line in lines)
if (line.Equals(specificLine))
File.WriteAllText(targetFilePath, line);
Your code could be shortcutted this way...
/// Disposal of your reader when unused is essential to close your file
using(var reader = new StreamReader(OpenFile.FileName))
{
var lines = reader.ReadToEnd().Split(new char[] { '\n' });
int NumLines = lines.Length;
for (int i = 0; i < NumLines; i++)
{
... do something here
}
}
So i have to write a program that reads values (numbers) from Values.txt file and store them into array of integers (at the beginning i dont know the number of values in file so i dont know the length of array).
First i loop through the file and use counter variable to get the number of values to store into array. Then comes the problem, i dont know how to start from the beginning of file to catch values and store them into array.
When i run it i get 0 0 0 0 0 as a result.
If instead of
myReader.DiscardBufferedData();
myReader.BaseStream.Seek(0, SeekOrigin.Begin);
myReader.BaseStream.Position = 0;
I use the myReader.Close() and then myReader = new StreamReader("Values.txt), the result is correct, can somebody please explain why this is happening and howcan i fix this code :)
string lineOfText = "";
int counter = 0;
int[] intArray;
StreamReader myReader = new StreamReader("Values.txt");
while(lineOfText != null)
{
lineOfText = myReader.ReadLine();
if(lineOfText != null)
{
counter++;
}
}
intArray = new int[counter];
myReader.DiscardBufferedData();
myReader.BaseStream.Seek(0, SeekOrigin.Begin);
myReader.BaseStream.Position = 0;
counter = 0;
while(lineOfText != null)
{
lineOfText = myReader.ReadLine();
if (lineOfText != null)
{
intArray[counter] = int.Parse(lineOfText);
}
counter++;
}
myReader.Close();
for (int j = 0; j < intArray.Length; j++)
{
Console.WriteLine(intArray[j]);
}
This explains how to reset the stream if you want to do it in one pass Return StreamReader to Beginning.
Are you required to use an array? A list would be perfect for what your doing, you can then later push that list into an array or just work directly from the list. Code example below:
List<string> ints = new List<string>();
using (StreamReader sr = new StreamReader("example.txt"))
{
ints.Add(sr.ReadLine());
}
public void get10FirstLines()
{
StreamReader sr = new StreamReader(path);
String lines = "";
lines = sr.readLine();
}
How can I get the first 10 lines of the file in the string?
Rather than using StreamReader directly, use File.ReadLines which returns an IEnumerable<string>. You can then use LINQ:
var first10Lines = File.ReadLines(path).Take(10).ToList();
The benefit of using File.ReadLines instead of File.ReadAllLines is that it only reads the lines you're interested in, instead of reading the whole file. On the other hand, it's only available in .NET 4+. It's easy to implement with an iterator block if you want it for .NET 3.5 though.
The call to ToList() is there to force the query to be evaluated (i.e. actually read the data) so that it's read exactly once. Without the ToList call, if you tried to iterate over first10Lines more than once, it would read the file more than once (assuming it works at all; I seem to recall that File.ReadLines isn't implemented terribly cleanly in that respect).
If you want the first 10 lines as a single string (e.g. with "\r\n" separating them) then you can use string.Join:
var first10Lines = string.Join("\r\n", File.ReadLines(path).Take(10));
Obviously you can change the separator by changing the first argument in the call.
var lines = File.ReadAllLines(path).Take(10);
You may try to use File.ReadLines. Try this:-
var lines = File.ReadLines(path).Take(10);
In your case try this as you want the first 10 lines as a single string so you may try to use string.Join() like this:
var myStr= string.Join("", File.ReadLines(path).Take(10));
StringBuilder myString = new StringBuilder();
TextReader sr = new StreamReader(path);
for (int i=0; i < 10; i++)
{
myString.Append(sr.ReadLine())
}
String[] lines = new String[10];
for (int i = 0; i < 10; i++)
lines[i] = sr.readLine();
That loops ten times and places the results in a new array.
public void skip10Lines()
{
StringBuilder lines=new StringBuilder();
using(StreamReader sr = new StreamReader(path))
{
String line = "";
int count=0;
while((line= sr.ReadLine())!=null)
{
if(count==10)
break;
lines.Append(line+Environment.NewLine);
count++;
}
}
string myFileData=lines.ToString();
}
OR
public void skip10Lines()
{
int count=0;
List<String> lines=new List<String>();
foreach(var line in File.ReadLines(path))
{
if(count==10)
break;
lines.Add(line);
count++;
}
}
Reason beeing is that the nested operators File.ReadLines(path).Take(10).ToList(); will do the following truly:
string[] lines = File.ReadLines(path); // reads all lines of the file
string[] selectedlines = lines.Take(10); // takes first 10 line s into a new array
List<String> LinesList = selectedlines.ToList();
Especially the first part is cumbersome as it reads the full file into an enumerable of lines. This eats Performance and Memory. The question poster asked specifically for large files.
for this reason, I would rather recommend:
/// <summary>
/// This function loads and returns the first x lines from a file
/// </summary>
/// <param name="path">The path to the file to read</param>
/// <param name="amountOfLines">the number of lines which should be read</param>
/// <param name="encoding">optional value, which specifies the format in which the text file is saved</param>
/// <returns></returns>
public List<string> GetFirstXLines(string path, int amountOfLines, Encoding encoding = null)
{
// if no encoding is set, try the default encoding (File Format)
if (encoding == null) encoding = Encoding.Default;
// create list which will be filled with the lines and returned later
List<string> lines = new List<string>();
// wrap streamreader around so it gets closed+disposed properly later
using (StreamReader reader = new StreamReader(path, encoding))
{
// loop x times to get the first x lines
for (int i = 0; i < amountOfLines; i++)
{
// read the next line
string line = reader.ReadLine();
// if the line is null, we are at the end of file, break out of the loop
if (line == null) break;
// if the line was not null, add it to the lines list
lines.Add(line);
}
}
// return the lines
return lines;
}
In Groovy, a JVM based language, one approach is:
def buf = new StringBuilder()
Iterator iter = new File(path).withReader{
for( int cnt = 0;cnt < 9;cnt++){
buf << it.readLine()
}
}
println buf
Since, there is no 'break' from a closure, the loop is nested within the closure, and thereby the resource handling is taken care of by the Groovy runtime.
I need to initialize 2D array and first column is every row in a file. How do I get the number of rows in a file?
You could do:
System.IO.File.ReadAllLines("path").Length
Edit
As Joe points out, I left out all the standard error handling and didn't show you would then use this same array to process in the rest of your code.
From MSDN:
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
Console.WriteLine (line);
counter++;
}
file.Close();
You would have to open the file reading in each line to get the count:
var lines = File.ReadAllLines(filename);
var count = lines.Length;
int counter = 0;
string line;
System.IO.StreamReader file = new System.IO.StreamReader("c:\\t1.txt");
while((line = file.ReadLine()) != null)
{
counter++;
}
file.Close();
counter will give you number of lines. you can insert line into your array as well withthe loop.
There may be a more efficient way for larger files, but you could start with something like:
int l_rowCount = 0;
string l_path = #"C:\Path\To\Your\File.txt";
using (StreamReader l_Sr = new StreamReader(l_path))
{
while (l_Sr.ReadLine())
l_rowCount++;
}
It would probably be more useful for you to actually open the file, read the lines into a List, then create your 2D array.
List<string> lines = new List<string>()
using(System.IO.StreamReader file = new System.IO.StreamReader(fileName))
{
while(!file.EndOfStream) lines.Add(file.ReadLine());
}
You can then use your lines list to create your array.
could you go with something more exotic like a linq statement
Count * from textfile
something like that?