writing to a text file
-
I am writing a program that stores date, as fixed width data. The record is supposed to be 532 characters wrong. When I do the getRecordLength method it says that the record is 548 characters wrong. I looked at the file and its adding a strange character ( [], (, #, $, etc...) to each thing im writing. Why is it adding this and how do i get rid of this. Thanks. Kyle
Kyle Maldonado
-
I am writing a program that stores date, as fixed width data. The record is supposed to be 532 characters wrong. When I do the getRecordLength method it says that the record is 548 characters wrong. I looked at the file and its adding a strange character ( [], (, #, $, etc...) to each thing im writing. Why is it adding this and how do i get rid of this. Thanks. Kyle
Kyle Maldonado
Because your name is Kyle. Could you post a small section of the code that is performing the write?
-
Because your name is Kyle. Could you post a small section of the code that is performing the write?
this is the code clsContact myData = new clsContact(); string Filename = "Employee.dat"; int flag; string State = cboState.Text; string Country = cboCountry.Text; string Birthday = txtBdayMonth.Text + '/' + txtBdayDay.Text + '/' + txtBdayYear.Text; string Anniversary = txtAnnMonth.Text + '/' + txtAnnDay.Text + '/' + txtAnnYear.Text; string HomePhone = txtHAreaCode.Text + '-' + txtHPrefix.Text + '-' + txtHLast.Text; string CellPhone = txtCAreaCode.Text + '-' + txtCPrefix.Text + '-' + txtCLast.Text; string EmployeePictureFile = txtLName.Text + txtFName.Text + ".jpg"; myData.FirstName = txtFName.Text; myData.LastName = txtLName.Text; myData.MI = txtMI.Text; myData.SSN = txtSSN.Text; myData.AddressLine1 = txtAddressLine1.Text; myData.AddressLine2 = txtAddressLine2.Text; myData.Zip = txtZip.Text; myData.OfficeExtension = txtOfficeExt.Text; myData.Email = txtEmail.Text; myData.Spouse = txtSpouse.Text; myData.Department = cboDepartment.Text; myData.State = cboState.Text; myData.Country = cboCountry.Text; myData.City = txtCity.Text; myData.Birthday = Birthday; myData.Anniversary = Anniversary; myData.HomePhoneNumber = HomePhone; myData.CellPhoneNumber = CellPhone; myData.EmployeePictureFile = EmployeePictureFile; BinaryWriter bw = null; if (!validation()) { return; } myData.FileName = Filename; flag = myData.Create(Filename); if (flag == 0) //An error { return; } { { try { int recs = (int)myData.getRecordCount(); //getInputValues(); myData.WriteOneRecord(recs); } catch (Exception ex) { MessageBox.Show("Error saving contact" + contact.FileName + ex.Message); } finally { if (bw != null) { bw.Close(); } } lstContacts.Items.Add(myData.LastName + "\t" + myData.FirstName); contact = null; } } } and its calling these methods from a class
-
this is the code clsContact myData = new clsContact(); string Filename = "Employee.dat"; int flag; string State = cboState.Text; string Country = cboCountry.Text; string Birthday = txtBdayMonth.Text + '/' + txtBdayDay.Text + '/' + txtBdayYear.Text; string Anniversary = txtAnnMonth.Text + '/' + txtAnnDay.Text + '/' + txtAnnYear.Text; string HomePhone = txtHAreaCode.Text + '-' + txtHPrefix.Text + '-' + txtHLast.Text; string CellPhone = txtCAreaCode.Text + '-' + txtCPrefix.Text + '-' + txtCLast.Text; string EmployeePictureFile = txtLName.Text + txtFName.Text + ".jpg"; myData.FirstName = txtFName.Text; myData.LastName = txtLName.Text; myData.MI = txtMI.Text; myData.SSN = txtSSN.Text; myData.AddressLine1 = txtAddressLine1.Text; myData.AddressLine2 = txtAddressLine2.Text; myData.Zip = txtZip.Text; myData.OfficeExtension = txtOfficeExt.Text; myData.Email = txtEmail.Text; myData.Spouse = txtSpouse.Text; myData.Department = cboDepartment.Text; myData.State = cboState.Text; myData.Country = cboCountry.Text; myData.City = txtCity.Text; myData.Birthday = Birthday; myData.Anniversary = Anniversary; myData.HomePhoneNumber = HomePhone; myData.CellPhoneNumber = CellPhone; myData.EmployeePictureFile = EmployeePictureFile; BinaryWriter bw = null; if (!validation()) { return; } myData.FileName = Filename; flag = myData.Create(Filename); if (flag == 0) //An error { return; } { { try { int recs = (int)myData.getRecordCount(); //getInputValues(); myData.WriteOneRecord(recs); } catch (Exception ex) { MessageBox.Show("Error saving contact" + contact.FileName + ex.Message); } finally { if (bw != null) { bw.Close(); } } lstContacts.Items.Add(myData.LastName + "\t" + myData.FirstName); contact = null; } } } and its calling these methods from a class
Anyone see anything wrong?
Kyle Maldonado
-
Anyone see anything wrong?
Kyle Maldonado
I guess from the code, your thing would be much easier to handle as a SQL (or simply a Database) task. Still, I have question: Why is it important the size you stated in your 1st post? to avoid strange chars: you can use streamwriter instead of binarywriter with proper encoding (eg.: Encoding.UTF-16 - as a parameter-) if you expect other than eng. characters. cheers, geri
-
I guess from the code, your thing would be much easier to handle as a SQL (or simply a Database) task. Still, I have question: Why is it important the size you stated in your 1st post? to avoid strange chars: you can use streamwriter instead of binarywriter with proper encoding (eg.: Encoding.UTF-16 - as a parameter-) if you expect other than eng. characters. cheers, geri
Its for a school project, we have to use a text file, using a fixed witdth. And we have to use binary.
Kyle Maldonado
-
Its for a school project, we have to use a text file, using a fixed witdth. And we have to use binary.
Kyle Maldonado
still don't understand why to use a fixed size (do you mean fixed size, when you write fixed width?): an idea to your code: it 's always useful to close your writer in a finally block like: binarywriter.close();
-
still don't understand why to use a fixed size (do you mean fixed size, when you write fixed width?): an idea to your code: it 's always useful to close your writer in a finally block like: binarywriter.close();
When I say fixed size i mean that NAMESIZE = 30 if (txtName.length < NAMESIZE) { txtName.PadRight(NAMESIZE).Substring(0,NAMESIZE); } so then all the records are the same size so its easier to pull data out. (The above code was just written fast, may not be correct)
Kyle Maldonado
-
this is the code clsContact myData = new clsContact(); string Filename = "Employee.dat"; int flag; string State = cboState.Text; string Country = cboCountry.Text; string Birthday = txtBdayMonth.Text + '/' + txtBdayDay.Text + '/' + txtBdayYear.Text; string Anniversary = txtAnnMonth.Text + '/' + txtAnnDay.Text + '/' + txtAnnYear.Text; string HomePhone = txtHAreaCode.Text + '-' + txtHPrefix.Text + '-' + txtHLast.Text; string CellPhone = txtCAreaCode.Text + '-' + txtCPrefix.Text + '-' + txtCLast.Text; string EmployeePictureFile = txtLName.Text + txtFName.Text + ".jpg"; myData.FirstName = txtFName.Text; myData.LastName = txtLName.Text; myData.MI = txtMI.Text; myData.SSN = txtSSN.Text; myData.AddressLine1 = txtAddressLine1.Text; myData.AddressLine2 = txtAddressLine2.Text; myData.Zip = txtZip.Text; myData.OfficeExtension = txtOfficeExt.Text; myData.Email = txtEmail.Text; myData.Spouse = txtSpouse.Text; myData.Department = cboDepartment.Text; myData.State = cboState.Text; myData.Country = cboCountry.Text; myData.City = txtCity.Text; myData.Birthday = Birthday; myData.Anniversary = Anniversary; myData.HomePhoneNumber = HomePhone; myData.CellPhoneNumber = CellPhone; myData.EmployeePictureFile = EmployeePictureFile; BinaryWriter bw = null; if (!validation()) { return; } myData.FileName = Filename; flag = myData.Create(Filename); if (flag == 0) //An error { return; } { { try { int recs = (int)myData.getRecordCount(); //getInputValues(); myData.WriteOneRecord(recs); } catch (Exception ex) { MessageBox.Show("Error saving contact" + contact.FileName + ex.Message); } finally { if (bw != null) { bw.Close(); } } lstContacts.Items.Add(myData.LastName + "\t" + myData.FirstName); contact = null; } } } and its calling these methods from a class
Hi, from MSDN on BinaryWriter.Write Method (String): "Writes a length-prefixed string to this stream in the current encoding of the BinaryWriter, and advances the current position of the stream in accordance with the encoding used and the specific characters being written to the stream" :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets
-
Its for a school project, we have to use a text file, using a fixed witdth. And we have to use binary.
Kyle Maldonado
Ah, that explains it.