473,395 Members | 1,556 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

filing issues in C#

Hi,
I am trying to access a file in two different classes simultaneously. Is there any solution ?

thank you in advance,

Regards,

Syed Ahmed Hussain
Jul 24 '09 #1
9 2116
r035198x
13,262 8TB
For what purposes? Read in both, write in both? What have you tried so far?
Jul 24 '09 #2
tlhintoq
3,525 Expert 2GB
@Ahmedhussain
Solution to what? You haven't stated a problem.
Jul 24 '09 #3
Sorry guys I was actually in a bit of hurry...

I am using sensors to get the humidity and temperature values in a hexadecimal string. I am writing those values into a file. And at the same moment this file is accessed from another class to get the values and convert them into integer valuesand then writing a delimiter in the file in order to remember that upto where I have read the file. But the problem is actually when I run the code it gives me error on file close.

Is there any solution??

This is of my first class where I am getting the values from the sensor... and writing it in a file....
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. private void separateTemperaturenHumidity(string reading)
  4.         {
  5.             Int32 RawTemp = 0;
  6.             Int32 RawHum = 0;
  7.             Int32 RawLight = 0;
  8.             double TempinDegC = 0;
  9.             double RelHuminPer = 0;
  10.             double LightinLx = 0;
  11.             string NodeID, Temperature, Humidity, Light;
  12.  
  13.  
  14.             // Node ID
  15.             NodeID = reading.Substring(28, 4);
  16.             //      NodeID = NodeID + reading.Substring(45, 2);
  17.  
  18.             // Light Reading
  19.             Light = reading.Substring(36, 4);
  20.             //  Light = Light + reading.Substring(57, 2);
  21.  
  22.             // Temperature Reading
  23.             Temperature = reading.Substring(40, 4);
  24.             //  Temperature = Temperature + reading.Substring(63, 2);
  25.  
  26.             // Humidity Reading
  27.             Humidity = reading.Substring(44, 4);
  28.             //Humidity = Humidity + reading.Substring(69, 2);
  29.  
  30.  
  31.  
  32.             //Converting string into Decimal value
  33.             RawTemp = Convert.ToInt32(Temperature, 16);
  34.             RawHum = Convert.ToInt32(Humidity, 16);
  35.             RawLight = Convert.ToInt32(Light, 16);
  36.  
  37.             // Converting Raw Temperature into Degree Centigrade
  38.             TempinDegC = 0.01 * RawTemp - 39.6;
  39.  
  40.             // Converting Raw Humidity into Relative Humidity in Percentage
  41.             RelHuminPer = (TempinDegC - 25) * (0.01 + 0.00008 * RawHum) + (-4 + 0.0405 * RawHum - 0.0000028 * RawHum * RawHum);
  42.  
  43.             // Converting Raw Light into Lux unit
  44.             double V = (float)RawLight / 4096 * 1.5;
  45.             double I = V / 100000;
  46.  
  47.             LightinLx = 1e6 * I * 1000;
  48.  
  49.  
  50.             // Converting values into strings
  51.             string Str_T = RawTemp + "";
  52.             string Str_TC = TempinDegC + "";
  53.             string Str_H = RawHum + "";
  54.             string Str_RH = RelHuminPer + "";
  55.             string Str_L = RawLight + "";
  56.             string Str_Llx = LightinLx + "";
  57.  
  58.             // Writing to File namely SensorData.txt
  59.             writeFile(reading, NodeID, Str_T, Str_TC, Str_H, Str_RH, Str_L, Str_Llx);
  60.         }
  61.  
  62. private void writeFile(string str_packet, string NID, string RT, string TC, string H, string RH, string L, string LLx)
  63.         {
  64.             try
  65.             {
  66.                 //Pass the filepath and filename to the StreamWriter Constructor
  67.  
  68.                 StreamWriter sw = new StreamWriter(@"C:\Documents and Settings\syed\My Documents\Visual Studio 2005\Projects\Data Repositiry and Terminal\Data Repositiry and Terminal\obj\Debug\data\sensordatapackets.text", true);
  69.                 //Write a line of text
  70.                 sw.WriteLine(str_packet + " " + DateTime.Now);
  71.                 //Close the file
  72.                 sw.Close();
  73.  
  74.                 sw = new StreamWriter(@"C:\Documents and Settings\syed\My Documents\Visual Studio 2005\Projects\Data Repositiry and Terminal\Data Repositiry and Terminal\obj\Debug\data\" + NID + "datapackets.text", true);
  75.                 //Write a line of text
  76.                 sw.WriteLine(str_packet + " " + DateTime.Now);
  77.                 //Close the file
  78.                 sw.Close();
  79.  
  80.             }
  81.             catch (Exception e)
  82.             {
  83.                 rtfTerminal.AppendText(e.StackTrace);
  84.                 System.Console.Write(e.StackTrace);
  85.                 MessageBox.Show("Exception: " + e.StackTrace);
  86.  
  87.             }
  88.  
  89.         }
  90.  
  91.  
And the below one is from another class...

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  public void getUpdate()
  4.         {
  5.             char marker = '=';
  6.             string delimitData = null;
  7.             int k = 0;
  8.             int j = 0;
  9.  
  10.  
  11.                 FileStream fs = new FileStream(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\Data Repositiry and Terminal\Data Repositiry and Terminal\obj\Debug\data\Sensordatapackets.text", FileMode.Open);
  12.  
  13.                 StreamReader sr = new StreamReader(fs);
  14.                 StreamWriter sw = new StreamWriter(fs);
  15.                 data = sr.ReadToEnd();
  16.                 j = data.Length -1;
  17.                 while ((j >= 0)&&(!(data[j].Equals('='))))
  18.                 { 
  19.                     delimitData= delimitData+ data[j];
  20.                         j--;
  21.                 }
  22.  
  23.                 char[] c = delimitData.ToCharArray();
  24.                 Array.Reverse(c);
  25.                 delimitData = new String(c);
  26.                 this.packetSplitter(delimitData);
  27.                 sw.WriteLine(marker);
  28.                 k++;
  29.  
  30.  
  31.         }
  32.  
  33.  
  34.         public void packetSplitter(string args)
  35.         {
  36.             Convertor convert = new Convertor();
  37.  
  38.             string sensorID;
  39.             string splitter = "\r\n";
  40.             char replacementSplit = '-'; 
  41.             string[] seperatePacketsTimeStamp = new string[3];
  42.             int i = 0;
  43.             char seperator = ' ';
  44.             string timepatch;
  45.  
  46.             args = args.Replace(splitter, replacementSplit.ToString());
  47.             string[] packets = args.Split(replacementSplit);
  48.             int length = packets.Length;
  49.  
  50.             while (i < length)
  51.             {//seperator will be used here in order to seperate the packets from eachother...
  52.                 seperatePacketsTimeStamp = packets[i].Split(seperator);
  53.  
  54.                 if (seperatePacketsTimeStamp.Length==1)
  55.                 { i++; }
  56.                 else
  57.                 {
  58.                     sensorID = seperatePacketsTimeStamp[0].Substring(28, 4);
  59.  
  60.                     timepatch = seperatePacketsTimeStamp[2] + ' ' + seperatePacketsTimeStamp[3];
  61.  
  62.                     this.DBCheckAndFill(timepatch, seperatePacketsTimeStamp[1], seperatePacketsTimeStamp[0], sensorID);
  63.                     i++;
  64.                 }
  65.  
  66.             }
  67.  
  68.         }
  69.  
Jul 24 '09 #4
Just a reminder
both class should run simultaneously. and then their would be an other class of convertor as well which will convert the values but thats the part for the DB...


thanks again

Regards,

Syed Ahmed Hussain
Jul 24 '09 #5
tlhintoq
3,525 Expert 2GB
I am using sensors to get the humidity and temperature values in a hexadecimal string. I am writing those values into a file. And at the same moment this file is accessed from another class to get the values and convert them into integer valuesand then writing a delimiter in the file in order to remember that up to where I have read the file.
Is there a reason other than the passing of the values, for writing this data to a file? This is a rather clunky and slow way to pass data.

I would suggest you have only one class that accesses the file. Give it methods for reading and writing and updating. Your other classes can pass data to it, and ask for data from it.

But the problem is actually when I run the code it gives me error on file close
And that error would be.... ????
Jul 24 '09 #6
That error comes when the first class closes the file. I dont exactly remember the error and I cant provide you the error right now because I am using Virtual PC. And its not enabling the hardware virtualization, so I can check the error when sensor passes the values.

Is there any one who could help me out in VPC problems as well. I have to submit this on monday and I am totally stucked here.

Regards,
Syed Ahmed Hussain
Jul 25 '09 #7
tlhintoq
3,525 Expert 2GB
I use VMware, sorry.

That error comes when the first class closes the file.
Right. You mentioned that. And it makes sense, since two files have the file open, then one closes it and the other is still expecting it to be open. Hence the earlier suggestion to only have one class controlling one file. Did you give that a try?

I have to submit this on monday
Homework assignment?

I will ask again
Is there a reason other than the passing of the values, for writing this data to a file? This is a rather clunky and slow way to pass data.
Jul 25 '09 #8
tlhintoq
3,525 Expert 2GB
And at the same moment this file is accessed from another class to get the values
If you want to write the data to a file so you can have a history, that's one thing... but its silly to read them back from another class in the same application. There are a number of ways to pass them directly from one class to another, completely eliminating the cause of your two-class-reading-one-file problem.

Put the data in a custom arguments class.
Raise an event with the arguments.
Have the second class subscribe to this event.
Every time class1 raises the event with the arguments, class2 will react. Just like reacting to button_click event.
Jul 25 '09 #9
Dear tlhintoq,

You can say its my assignment, because I am doing my internship in CRUC (Center of Research in Ubiquitous Computing) . I am working on Ontology . We are actually creating a software that will use a reasoner in .NET to reason certain information which will be provided.

And you got that right that when the file is closed other classes are using it.

Perhaps there is a reason for writing it into a file. Can we use any file locking instead of changing the classes. I totally agreed with you for making an event handler or do all in a single class, but I talked to my professor and he asked me that he dont want to change the class schema.
Jul 26 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tom Loredo | last post by:
Hi folks- I'm about to move from a Solaris 8/SPARC environment to a Dell running RedHat 9. Are there any issues I need to be aware of in bringing my Python code over (mostly scientific...
2
by: malcolm | last post by:
Hello, We have a robust (.NET 1.1 c# winforms) client-server application that utilizes many typed DataSets, typed DataTables and typed DataRows. Our application is a series of windows and popup...
2
by: j. shepherd | last post by:
I am looking for a filing software for the office. I need to get all of the files in the office entered into some type of software or database, that I can pull up scanned documents, location of...
1
by: editprod | last post by:
I'd like to create an Access database for my the central filing system of my office. My boss can never find where files are. We have approx. 2000 files in a dozen file drawers, each drawer with a...
1
by: RANIA | last post by:
i wana make a program in which user find the index of agiven string through filing and also user can delete the given string from file replace and isert a string into fil.
1
by: GaryDean | last post by:
We have been developing all of our .net applications on 32 bit windows using 32 bit SQL Server. We are being asked to now deploy to servers running 64bit windows and 64bit SQL Server. Are there...
0
by: reddog | last post by:
Can anyone direct me to a site (or help me) that will tell me how to set up Direct Digitial Filing? Iam running Windows XP. Thank You for Your Valued Time Reddog
0
by: Chicago | last post by:
$9.95 Online Tax Filing www.MHBSgloabl.com
3
by: eschneider | last post by:
Just some common issues with WS: Using custom objects: When objects change, seems you are always fixing some issue. Update references, which sometimes does not work. Deployment: Weird errors...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.