|
Hi,all:
I want to read Text File like:
123
456
789
...
Now i use readline() to read it. But really "789" is not "789", it can be "abc","def" or blank line,i don't know it. I hope when i finished read "456",i can try to read nextline,if it is not "789",i hope when i use nextline(), i can still can get this line.
|
|
|
|
|
The simplest way is to read all the lines at once:
string[] lines = File.ReadAllLines(pathToFile); You can just use a basic loop to access the lines in any order you need.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
OK,thank you. But I want to use stream style to read,because my file content is big, i heard that stream is better when fil content is big.
|
|
|
|
|
Depends on the size. "Big" is a relative term!
Streaming isn't difficult either:
using (StreamReader read = new StreamReader(@"D:\Temp\MyBigFile.txt"))
{
while (read.Peek() >= 0)
{
string line = read.ReadLine();
}
}
But...unless your file is really, really big - like 250,000,000 lines plus - it's not necessarily going to be much more efficient than reading all the lines at once.
If you have to "go back" to lines, you can use
long pos = read.BaseStream.Position;
before each ReadLine to find out where in the file you are, and use Seek to go back there. But...remember that if you are storing the position of all lines, you can't store more than 250,000,000 on a 64 bit system, and 500,000,000 on a 32 bit system as there is an upper limit to the size of an array in .NET
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
OK,i Know,Thanke you very much
|
|
|
|
|
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
This may or may not work depending on the version of the .NET framework.
It appears that at least up through .NET 4.5.1, the Position property of a FileStream returned only the information about how far the buffering had read.
namespace ConsoleApplication15
{
class Program
{
static void Main(string[] args)
{
using (System.IO.StreamReader reader = new System.IO.StreamReader(@"..\..\Program.cs"))
{
if (reader.EndOfStream)
return;
System.Console.WriteLine("Stream.Position = {0}", reader.BaseStream.Position);
}
System.Console.ReadLine();
}
}
}
This outputs:
Stream.Position = 455
(But looking at the 4.6.1 source code for FileStream , it appears that it will give the correct Position based on the actual read position.)
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed."
- G.K. Chesterton
|
|
|
|
|
What you want to do, Read text file Line by line or at once, if you want to read it at once then you can refer a above solution given by OriginalGriff
if you want to read it line by line use below code
using System;
using System.IO;
class Program
{
static void Main()
{
foreach (string line in File.ReadLines("c:\\file.txt"))
{
Console.WriteLine("-- {0}", line);
}
}
}
Find More .Net development tips at : .NET Tips
The only reason people get lost in thought is because it's unfamiliar territory.
|
|
|
|
|
Um...that doesn't read it line by line - it reads them all and processes them line by line in a loop afterwards.
If you want to read line by line you need to loop on StreamReader.ReadLine instead.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Yes, basically it reads it at once but later split it by lines
Find More .Net development tips at : .NET Tips
The only reason people get lost in thought is because it's unfamiliar territory.
|
|
|
|
|
Then ,how can i do it use stream ? line by line? I found Peek() ,but only can read 1 char, i need try read 1 line.
|
|
|
|
|
use below code
const Int32 BufferSize = 128;
using (var fileStream = File.OpenRead(fileName))
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize)) {
String line;
while ((line = streamReader.ReadLine()) != null)
}
Find More .Net development tips at : .NET Tips
The only reason people get lost in thought is because it's unfamiliar territory.
|
|
|
|
|
A stream doesn't know about lines; a line-feed is a character. So, you reed 1 char, until you find a new-line character, then process.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
But you explicitly say "if you want to read it line by line use below code" - which it doesn't do!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I think you're confusing File.ReadLines[^] and File.ReadAllLines[^].
ReadAllLines reads all lines into memory and returns them as an array.
ReadLines returns an iterator that yields each line as it's read.
File.ReadLines is essentially the same as:
using (StreamReader reader = new StreamReader(path))
{
string line;
while ((line = reader.ReadLine()) != null)
{
yield return line;
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
It's not a programming problem, but one of mathematics. Find the mathematical solution; turning that into C# code should be easy.
|
|
|
|
|
The math forums are useless. They ask me to show how I'm solving it haha.
The point of my post is that I don't know how to solve it... otherwise I wouldn't be asking for help lmao
|
|
|
|
|
Sorry, but this is a programming, not a maths, forum. The entire problem is one of applying certain mathematical formulas, which you should be able to find in any decent reference. Converting those calculations into code is the easy part.
|
|
|
|
|
Correct...once I got help with the proper formula converting it was simple. Some coders went through more advanced math and are definitely built for this stuff...I am not one of them...and judging by how difficult it was to find an answer, I'm guessing there aren't many out there either. Still, was fun to figure out.
|
|
|
|
|
This is not a programming problem.
It is Geometry.
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
You repeat the first part of Richard's answer almost word by word without adding anything. What's the purpose of this?
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Basically, I have just read the question, and not the first answer
Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
|
|
|
|
|
Answer was to convert the 1% line to degree and add or subtract to the angles. The math equation associated with it was y=ax+b, a=tan(theta).
|
|
|
|
|
Hello Brothers
I am Looking for the best GPS tracking Application until now.
I want to make application for students tracking in Our University
please any ideas about how I start ?
if any brothers have Application please show me it
Thank you very much for all the members in code project
Best wishes
Dr. Kasa.
|
|
|
|