Question Random file access

pumper

Member
Joined
May 12, 2016
Messages
14
Programming Experience
1-3
I am new to C# programing. I have been using visual basic.
and when I access my file I use: FileOpen, FileGet FilePut and FileClose features of visual basic.

What would be similar in C#?
 
You shouldn't be using those in VB anyway. They are holdovers from VB6. If you're using a .NET language then there are numerous ways to work with files in the System.IO namespace. You can create a FileStream to get random binary access to a file.
 
Can you point me to some examples?

Not specifically but it works like any Stream. If you were to explain what you're trying to achieve, what you've tried to achieve it and how it doesn't meet expectations, we may be able to point you in the right direction.
 
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.

I would upload a copy of my file but I don't see how to do that.
 
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.
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:
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();
}

I would upload a copy of my file but I don't see how to do that.
Click the 'Go Advanced' button under the editor and then click the Manage Attachments button to attach a file.
 
Thanks, today will be spent studying and applying what you sent me.

I may have left out an important piece of information. I am using Visual C# with forms, the data in the file comes from text boxes on that form.
When the data is read it will be placed into the text boxes.
 
Dang this is a topic from the past, I also remember doing rand files back in vb6... they were a pain in the ass back then too.
I've found an article that discusses how to do this with vb.net (should be easy to convert to C# since today the two languages only differ by syntax and you know vb from vb6): How to correctly read a random access file in VB.NET - Stack Overflow
 
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.
 
My Project files

I have attached MyData Project. I did not delete the bin and object folders for fear for deleting the wrong thing.
I am attempting to upgrade this project from Visual Basic to C#. So far I have the form and buttons and text boxes added.
When the form loads I need to read the first 36 text data, this is the password, then I can De-encrypt it and check it.
This is where I am stuck now.

This is what I have now:
System.IO.StreamReader StreamReader = new StreamReader(@"C:\MyData.txt");

//Reading Line from file
String text;
while ((text = StreamReader.ReadLine()) != null)

//Writing the read line to textBox
textBox3.Text = text;

//Close the file.
StreamReader.Close();

This is for a file written in line but that is not what I have so it does not work. It gets data into textmox3 but it is not what I need.

I have not attempted to the rest of the program, I must get the password but done first then I can load the rest to populate the other fields.
 
This appeared as tho it was going to work:

string boxtext = "";
using (TextReader reader = File.OpenText(@"C:\MyData.txt"))
{
char[] c = new char[5];
reader.ReadBlock(c, 0, 5);
boxtext = boxtext + c;
}
this.textBox3.Text = boxtext;

But it returned this "System.Char[]" textBox3
 
You need to think about what data types you're using. This:
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.
 
I searched for String constructor and made this change to the code:

string boxtext = "";
using (TextReader reader = File.OpenText(@"C:\MyData.txt"))
{
char[] c = new char[36];
reader.ReadBlock(c, 0, 36);
boxtext = new string(c);
}

this.textBox3.Text = boxtext;

This gave me the results I expected. Now to create a pointer so I can read the next field to read.
 
When you call a Read method on your StreamReader, it advances the file pointer to the end of what you read. You don't have to do anything to read the next field other that call ReadBlock again. Only when you want to read a new record do you need to move that pointer to the beginning of the desired record, by setting the Position or calling Seek on the BaseStream.
 

Latest posts

Back
Top Bottom