|
Hello,
I'm developing an application for file processing. Basically I have a .txt file witch contains data from an AFM meassurement. The file itself contains the headers with information about the performed experiment and two segments built up by numbers, whitch are the actual experimental data. In order to be able to process the AFM data easier I want to divide the file in two parts, one part per header, and also remove the headers themselves.
I solved the problem by just copying the data connected to the first header in a new file and the data connected with the second header to another new file.
The problem is that the last line of each new copied file is not complete.
The last line of the original file is for example:
3.4327463E-6 1.8961255E-9 3.4336897E-6 2.8574045E-6 0.004730297 -0.020049555
The last line of my newly created file (witch is supposed to be an exact copy of the original line) is for example:
3.4327463E-6 1.89
A very large portion of the origninal information is lost.
I use an object of the StreamWriter Class to write the new file and an object of the StreamReader Class to read the original file. So I guess it's a problem with the StreamWriter.
Thank you for your help in advance.
|
|
|
|
|
Why don't you provide some meaningful code.
Hogan
|
|
|
|
|
I think you need to call the Flush() method on the StreamWriter.
"It was the day before today.... I remember it like it was yesterday."
-Moleman
|
|
|
|
|
Thank you very much. This solved the problem.
|
|
|
|
|
The problem is actually that you are closing the file before you are closing the stream. Just close the stream before the file, and the stream will be flushed automatically.
---
single minded; short sighted; long gone;
|
|
|
|
|
I agree completely on this one. Flush is irrelevant, closing things in the
logical order is what is needed.
|
|
|
|
|
Hi everyone,
So here is my concern, I really hope someone here can provide me with some much needed guidance.
Logically, I am not sure what route to take with a project I am working on. So far it is only partially built, but it contains an extensive GUI (without most of the backend still not coded) and a UDP listener that binds to a port, retrieves a byte stream and parses it into a number of variables.
The UDP stream is continuous with packets being sent at around every half second(forever). I would like to have the variables parsed from the byte stream to populate some text fields and and labels in the GUI and I am not quite sure what route to take to do this? Will I need to use multi-threading and separate the processes?
I have gotten the UDP listener to work and I could get the GUI to update the fields accordingly, but only if I stopped listening after a number of broadcasts. The GUI form wouldn't even load until the UDP listener process was finished....
realistically, the GUI would not have to update every half second. it could be every 3 or 4 packets. but in the end I would still need to save the ALL of the parsed data to an XML file or DB, so it would have to be recognized at some point(still deciding where to go with that part...). In any case I am more in need of help regarding the two processes mentioned above.
any kind of help or advice or thoughts would be greatly appreciated.
thank you in advance,
Joe
I am who I am because of who everyone around me is.
|
|
|
|
|
Hey Joe,
By strange coincidence I'm working on something similar (and indeed asked a related question earlier!).
The technique I'm looking at is the BeginRecieve method of the UdpClient class, which grabs data from the port asynchronously, enabling your application to do other things whilst this goes on. (I didn't find the MSDN example particularly straight forward to follow, but eventually got it working.)
"It was the day before today.... I remember it like it was yesterday."
-Moleman
|
|
|
|
|
thank you for the tip.
I will read into the BeginReceive method at MSDN. If you come across any links, while you are working on this that you think I may also find useful, feel free to pass them on to me.
regards,
joe
I am who I am because of who everyone around me is.
|
|
|
|
|
I'm not sure this will be any use to you, but here is a simple listening application I knocked up. When running it'll listen for TFTP requests on Port 69, and you should be able to see received data written to the console and that the console remains able to accept user input:
class Program
{
public static bool listen = true;
public static IPEndPoint endPoint;
public static UdpClient udpClient;
static void Main(string[] args)
{
Console.WriteLine("Server Running...");
endPoint = new IPEndPoint(IPAddress.Any, 69);
udpClient = new UdpClient(endPoint);
ReceiveMessages();
while (listen)
{
string str = Console.ReadLine();
if (str == "q")
{
listen = false;
}
else
{
Console.WriteLine(str);
}
}
}
public static void ReceiveCallback(IAsyncResult ar)
{
UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).udpClient;
IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).endPoint;
Byte[] receiveBytes = u.EndReceive(ar, ref e);
Console.WriteLine(Encoding.ASCII.GetString(receiveBytes) + "\r\n");
ReceiveMessages();
}
public static void ReceiveMessages()
{
UdpState state = new UdpState();
state.endPoint = endPoint;
state.udpClient = udpClient;
udpClient.BeginReceive(new AsyncCallback(ReceiveCallback), state);
}
}
class UdpState
{
public UdpClient udpClient;
public IPEndPoint endPoint;
}
Once it's running, from a command prompt enter a TFTP command like TFTP -i localhost GET test.txt and you should see it working - "q" quits.
"It was the day before today.... I remember it like it was yesterday."
-Moleman
|
|
|
|
|
Im pretty sure I get how the entire program works ( a little confused with the Async UDP stuff, but I get the overall idea).
just out of curiosity, what do each of the following two lines from the ReceiveCallback function specifically do?
<br />
UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).udpClient;<br />
IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).endPoint;<br />
I am who I am because of who everyone around me is.
|
|
|
|
|
Very good question - it caused me to wonder too!
Basically, when the asynchronous BeginReceive is called, you pass in a state object (which is the "state" instance of the UdpState class I knocked up). This is then received in the callback as the IAsyncResult's AsyncState property. Those two lines basically cast out the udpClient and endPoint contained in the AsyncState so that they can be used.
I'm still trying to figure it out, but I guess you could simply refer to the class level udpClient adn IPEndPoint variables here (the MSDN example didn't have these declared...), but given that what's going on here is asynchronous in nature, it's possible that those class variables could change between the async callback being called and any work actually being processed leading to very subtle bugs.
"It was the day before today.... I remember it like it was yesterday."
-Moleman
|
|
|
|
|
Thank you so much for your help.
I finally understand and have gotten the Async sockets to work. However I am having a rather small problem, and since I am new to C#, I cannot figure out the solution.
basically, I have a windows form with a few labels on it that need to be updated with each packet. my problem is in the permissions for accessing and setting these variables. Since the labels are properties in a windows form and the UDP listener is int the program.cs, I have no way of accessing these values from ReceiveCallback to update the form.
Ideally, with every loop through the callback function I would want to update label1 & label2 in my form (with already parsed bytes from the stream). Yet I cannot access label1 or label2 from the ReceiveCallback function where I am doing the byte stream parsing.... would you (or anyone!) by any chance have some insight on this?
thanks,
Joe
I am who I am because of who everyone around me is.
|
|
|
|
|
Well there are a number of possible options, but I think using events might be the best solution for you here.
Basically you'd setup up an Event in your Packet Listener class which fires every time a packet is received. You could then have your form listen to those events, and update accordingly, taking an an Event Argument from the fired event. You'll definintely have to look at cross threading issues (this is because having two threads update a UI is a bad idea), but this isn't too difficult, and there are some articles on CP which explain how to do this.
"It was the day before today.... I remember it like it was yesterday."
-Moleman
|
|
|
|
|
I have a phare of source:
InitializeComponent();
String []Nhat=Directory.GetDirectories(Directory.GetCurrentDirectory());
String Nht = Nhat[0];
when I debug,no happen,but when I run the progam,a dialogue display and require me to close dialogue,why I don't undersand
|
|
|
|
|
I don't understand.
What should happen?
What actually happens?
Upcoming events:
* Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ...
"I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless."
My website
|
|
|
|
|
You are making an assumption in the line String Nht = Nhat[0] that you actually have an entry. Before you do this, you need to make sure that Nhat actually has entries in it.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
but why when I debug,Nhat[] have contained data( data is a String)
|
|
|
|
|
Because you are using GetCurrentDirectory. Here's a hint - pop up a message box to say what GetCurrentDirectory contains in the release version.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
|
Whatever happens, don't give us any details.
If a dialog box tells you something, don't tell us.
If an Exception fires, don't reveal its message.
If you did, we might be able to help you; that can not be what you want...
|
|
|
|
|
I know its a silly ques.
I've got treeview control which holds different class names.
In normal ways what we do is.
Ayclass cls= new AnyClass();
now the AnyClass's name is stored in a variable. how to instantiate a class.
here's the failed code.
string varClass= treeview1.node.ToString();
varclass d= new varClass();
Hope I make myself clarify.
|
|
|
|
|
Normally you don't do this at all. It's possible using reflection, but having to use reflection is usually an indication that you are doing something backwards.
What is it that you are trying to accomplish, really? You should ask for a solution for that, instead of asking about the way that you think that it's solved.
---
single minded; short sighted; long gone;
|
|
|
|
|
If you are sure that's the way to go (see post from Guffa) then this is how it's working:
object d = Activator.CreateInstance(Type.GetType(typeName));
see help on CreateInstance for further information.
-^-^-^-^-^-
no risk no funk
|
|
|
|
|
here's what I want.
here's chart reports in treeview control for end user to display.
Sales_Monthwise
Sales_Citywise
sales_PartyWise
when a user clicks Sales_Monthwise I want open Charting Class with the same name.
one possible way is:
if (treeview.node.text == "Sales_MonthWise) {
Sales_Monthwise sm= new Sales_Monthwise();
sm.Show();
}
bcoz there are so many classes I don't want to insert logic for every class.
|
|
|
|