Can you point me to some examples?
So it seems that you a text file rather than a binary file so you should use a StreamReader or perhaps a TextFieldParser rather than a FileStream. In actual fact, a StreamReader creates a FileStream under the hood and adds some functionality to convert the binary data to text and a TextFieldParser creates a StreamReader under the hood and adds some functionality to separate the text into fields. If you want random access then you will still need to access the FileStream in order to call Seek or set the Position property. As an example, here's how you might read a record using a StreamReader, assuming that line breaks are comprised of carriage return / line feed pairs:I have data in a file. The first field of data has a USER ID 36 characters long, next is a DATE 12 characters long, next a USER NAME 25 characters long and then a NOTE FIELD 4000 characters long. The DATE and USER NAME are not encrypted, the other fields are. I need to read each field. Each field is saved as a string.
private readonly int[] fieldWidths = {36, 12, 25, 4000}; private StreamReader reader; private void OpenFile(string path) { reader = File.OpenText(path); } private void CloseFile() { reader.Close(); reader = null; } private string[] ReadRecordAtIndex(int index) { var recordLength = fieldWidths.Sum(); // Move the file pointer to the start of the requested record. // The extra two characters is for the line break. reader.BaseStream.Position = index == 0 ? 0 : index*(recordLength + 2); var data = new char[recordLength]; reader.Read(data, 0, recordLength); var startIndex = 0; var fields = new List<string>(); foreach (var fieldWidth in fieldWidths) { fields.Add(new string(data, startIndex, fieldWidth)); startIndex += fieldWidth; } return fields.ToArray(); }
Click the 'Go Advanced' button under the editor and then click the Manage Attachments button to attach a file.I would upload a copy of my file but I don't see how to do that.
Zip (delete the bin & obj folders) and post your project, I could take a look at it over the weekend.The last Visual Basic I am using is 2015. I have the random access for that program so I guess the syntax change to C# has me confused.
boxtext = boxtext + c;is going to implicitly call ToString on that Char array and the result of that is not to create a String containing the same characters but rather creates a String containing the type name, which is what most ToString methods do. If you want to create a String from a Char array then you need to use the appropriate String constructor.