|
Could you direct me to a sample code please?
|
|
|
|
|
How to get the count of lines in .txt file.
Help.
C#
|
|
|
|
|
I'm not sure if you can do anything but brute force it. Here's one way:
// Read the file into a byte array
FileStream fs = File.Open("TextFile1.txt", FileMode.Open);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, (int)fs.Length);
// int to store num of lines
int numOfLines = 0;
// if the file is not empty, we have at least 1 line
if (bytes.Length > 0) numOfLines++;
// increment the count for each newline character found
foreach (byte b in bytes) if(b == '\n') numOfLines ++;
...
This is just a starting point and would need to be more robust depending on the requirements. If the textfile's length is greater than int.MaxValue I suppose you'd need to read the file in sections. This solution will count all lines (even blank lines and trailing blank lines).
I would be interested to see another solution. I'm sure there must be a better one.
Cheers,
Ian
|
|
|
|
|
I would personally prefer to use StreamReader, it certainly looks more elegant.
int lineCount = 0;
using (System.IO.StreamReader sr = new System.IO.StreamReader("myFile.txt"))
{
while ( sr.ReadLine() != null) lineCount++;
}
Console.WriteLine( "Line Count: " + lineCount.ToString() );
Obviously you should add some error checking like making sure the file exists ETC but that should otherwise do the job.
Hope it helps
Regards
Wayne Phipps
____________
Time is the greatest teacher... unfortunately, it kills all of its students
View my Blog
|
|
|
|
|
Thanks. I knew there had to be a better way.
|
|
|
|
|
That depends on what you define as a line. There is no single method you can call to just return this value. You'd have to write a function that counts the number of lines in the file, according to the rules that you define for a "line", by reading the entire file and counting the lines up, one-by-one.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
What counts as a "line"? A certain non-printing character? (Then just count the number of that character.) A certain line length? (Then take (total chars)/(line length)).
Etc.
|
|
|
|
|
how can i run a process from my program and get it's output? as in running "ipconfig" and getting what was written in the dos window?
|
|
|
|
|
You can use the "> filename" option for this which will save the output to a file. Example:
C:\>ipconfig > ip.txt
|
|
|
|
|
If you need something more interactive, the System.Diagnostics.Process class allows you to replace the default input and output streams that goto the application. In theory (I've never tried) this would allow you to fully automate any command line app by analyzing screen scrapings to feed it new input.
--
CleaKO The sad part about this instance is that none of the users ever said anything [about the problem].
Pete O`Hanlon Doesn't that just tell you everything you need to know about users?
|
|
|
|
|
Process.StandardOutput Property[^]
Take a look at the first example. It shows how to read from a redirected stream and wait for the child process to exit.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Hi,
I need to build a complex region dividing system.
For example, i want to divide the display in two and then divide one of those sub displays in two again, etc.
For that i'm using the split container.
Basicly i need to dynamicly create split containers.
In the end i need to output a xml file with the regions sizes.
Well, i'm in the beggining of this problem.
For now i have two buttons. One that creates a horizontal split and another a vertical split.
For that i invocate the following method, changing only the orientation:
<br />
private void addNewRegion(System.Windows.Forms.Orientation o)<br />
{<br />
<br />
SplitContainer region = new SplitContainer();<br />
region.SuspendLayout();<br />
this.SuspendLayout();<br />
<br />
region.Dock = System.Windows.Forms.DockStyle.Fill;<br />
region.Location = new System.Drawing.Point(0, 0);<br />
region.Name = "region";<br />
region.Size = new System.Drawing.Size(292, 266);<br />
region.SplitterDistance = Int32.Parse(p.width)/2;<br />
region.TabIndex = 0;<br />
region.Orientation = o;<br />
<br />
this.Controls.Add(region);<br />
region.ResumeLayout(false);<br />
this.ResumeLayout(false);<br />
}<br />
Following the process described above it doesnt work. However if i past the body of the addNewRegion into the Form1_load, the split container is create.
Can anyone explain me why this doesnt work?
Maybe i need to redesign the form. Maybe not.
Any ideas?
Thx,
Nuno
|
|
|
|
|
Try calling this.PerformLayout at the end of the method.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Hi,
Didnt work!
Nuno
|
|
|
|
|
Something has happened!
If i click on the button nothings happens, but, if i minimize and restore the window again, the container gets draw.
This helps??
Nuno
|
|
|
|
|
The container gets added correctly to the form, but you have to explicitely tell the form to redraw. Add a call to the Refresh method.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Hi,
Didnt work as well!
Nuno
|
|
|
|
|
It's not clear to me from your post what behavior you're seeing with this. However, if I understand you correctly, you want a vertical splitter that contains a horizontal splitter in one of its two panels. In order to accomplish this, you need to add the horizontal splitter to the controls collection of the panel you want the horizontal splitter to be contained by. In the above code, whenever you call addNewRegion a new splitter is created and it is added to the form's controls collection. This won't work.
Also, it seems you are over complicated the problem. If you are setting the Dock property of the splitter to DockStyle.Fill, then the control will automatically fill the bounds of the form. So you don't need to set the location or the size.
I would try the following and massage it as needed:
// create the vertical splitter
SplitContainer verticalSplitter = new SplitContainer();
verticalSplitter.Orientation = Orientation.Vertical;
verticalSplitter.Dock = DockStyle.Fill;
// create the horizontal splitter
SplitContainer horizontalSplitter = new SplitContainer();
horizontalSplitter.Orientation = Orientation.Horizontal;
horizontalSplitter.Dock = DockStyle.Fill;
// add the vertical splitter to the form
this.Controls.Add(verticalSplitter);
// add the horizontal splitter to the first panel of the vertical splitter
verticalSplitter.Panel1.Controls.Add(horizontalSplitter);
...
Hope this helps.
|
|
|
|
|
Hi,
I have conscience of what you have just told me but i need to have the freedom to add as many splitContainers inside another splitContainer, recursively.
I don't know how am i gonna do that yet. I'm thinking in keep a structure, like a binary tree to keep track of the info of each splitContainer.
The first will be the root of the tree. If i then want another splitContainer inside the panel2 of the first splitContainer, i'll need to add a new node to the tree at the root's right. And if then i want another split container, inside the seconde split container, etc...
But, for now, i'm just testing the first level, with the most basic example.
My problem right now is with the form refresh.
As i said, the body, of the method, if executed inside the Form1_Load(..) will create a splitContainer soon as the program is executed.
However, if i call the code inside the AddNewRegion(..) method, it is not drawn.
Accidentally i found that, once executed the application, and pressed the button that executes code (which seems not to draw a thing), if i minimize and then restore the window, i can see the splitContainer drawn.
Did you understand me now?
Now i have sure that this is a refresh problem, but i still don't know how to solve it.
Any tips?
Thx,
Nuno
|
|
|
|
|
The SplitContainer control is meant to contain something. By itself, it doesn't always paint correctly. I'm not sure why this is but it is not too much of a problem because a SplitContainer that contains nothing should never exist in an application. Try putting something in the panels of the split containers and see if your problem goes away.
|
|
|
|
|
Nope... at least with what i done, which was the following:
<br />
SplitContainer region = new SplitContainer();<br />
Panel panel = new Panel(); <---<br />
<br />
region.SuspendLayout();<br />
this.SuspendLayout();<br />
<br />
region.Dock = System.Windows.Forms.DockStyle.Fill;<br />
region.Location = new System.Drawing.Point(0, 0);<br />
region.Name = "region";<br />
region.Size = new System.Drawing.Size(292, 266);<br />
region.SplitterDistance = Int32.Parse(p.width)/2;<br />
region.TabIndex = 0;<br />
region.Orientation = o;<br />
<br />
<br />
region.Panel1.Controls.Add(panel); <-----<br />
this.Controls.Add(region);<br />
region.ResumeLayout(false);<br />
<br />
regions.Add(region);<br />
<br />
this.PerformLayout();<br />
this.Refresh();<br />
|
|
|
|
|
I discovered why SplitContainers seem to paint strangely. They have not border by default. YOu have to set their BorderStyle property in order for it to paint borders.
Try:
region.BorderStyle = BorderStyle.Fixed3;
|
|
|
|
|
Yes, that's it!
Thank you very much!
Nuno
|
|
|
|
|
Hi,
I would your help again.
As i was inserting the splitContainer into the main form directly, it was being draw at the position (0,0), staying under a menu bar i had already defined.
To bypass this problem, i putted a panel to define the area i want for this control. Ok, the split container is now being drawn at the point (0,0) of the panel.
Now that i can see the splitContainer that is created i have another problem.
Altough the property dock is defined to fill, it doesnt appear filled, it appears with a very small size inside the panel.
When i move the spliter, the splitContainer becomes filled to the panel.
I tried to refresh() but it doesnt work.
Any tips?
Thx,
Nuno
|
|
|
|
|
I can't seem to duplicate your problem. Perhaps I don't understand you correctly.
I made a Panel smaller than the form's ClientRectangle and added it to the form. I then added a SplitContainer with a DockStyle of Fill to the panel. My SplitContainer fills the panel's client rectangle.
Am I misunderstanding your problem? You don't want your SplitContainer to fill the client rectangle of the form, so you created a panel with the dimensions and location you want the SplitContainer to have, then you added the SplitContainer (with DockStyle.fill) to the panel, but it doesn't fill the panel until you drag the splitter? Is that correct?
|
|
|
|