Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. writing to a text file

writing to a text file

Scheduled Pinned Locked Moved C#
question
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kyle Maldonado
    wrote on last edited by
    #1

    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

    P 1 Reply Last reply
    0
    • K 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

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Because your name is Kyle. Could you post a small section of the code that is performing the write?

      K 1 Reply Last reply
      0
      • P PIEBALDconsult

        Because your name is Kyle. Could you post a small section of the code that is performing the write?

        K Offline
        K Offline
        Kyle Maldonado
        wrote on last edited by
        #3

        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

        K L 2 Replies Last reply
        0
        • K 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

          K Offline
          K Offline
          Kyle Maldonado
          wrote on last edited by
          #4

          Anyone see anything wrong?

          Kyle Maldonado

          G 1 Reply Last reply
          0
          • K Kyle Maldonado

            Anyone see anything wrong?

            Kyle Maldonado

            G Offline
            G Offline
            gericooper
            wrote on last edited by
            #5

            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

            K 1 Reply Last reply
            0
            • G gericooper

              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

              K Offline
              K Offline
              Kyle Maldonado
              wrote on last edited by
              #6

              Its for a school project, we have to use a text file, using a fixed witdth. And we have to use binary.

              Kyle Maldonado

              G P 2 Replies Last reply
              0
              • K 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

                G Offline
                G Offline
                gericooper
                wrote on last edited by
                #7

                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();

                K 1 Reply Last reply
                0
                • G gericooper

                  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();

                  K Offline
                  K Offline
                  Kyle Maldonado
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • K 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

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    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


                    1 Reply Last reply
                    0
                    • K 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

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      Ah, that explains it.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups