Reputation: 125
I am attempting to take numbers (characters 0-9) in from a file and store them in memory.
Lets say we have a string called "register" (and can only (must) hold 5 chars max) and the register string will take in numbers that are read from the file so for example:
File1.txt:
The house number is 10 and the price is 4000 and 3.
So the register would be filled with the following: "10400"
Some logic would then be performed against the string and then the first char would be removed from string and everything would shift 1 to the left and another char (number) from the file would be added e.g.:
04000
and then...
40003
Hopefully somebody could shed some light on this and provide some ways of achieving this :)
Upvotes: 0
Views: 139
Reputation: 131
I would likely put a method for fetching the correct string into a value. See below for an example:
static string FetchRegister(string Source, int Max, int StartIndex)
{
string Register = string.Empty;
int RegisterIndex = 0;
for (int i = 0; i < Source.Length; i++)
{
if (char.IsNumber(Source[i]))
{
if (RegisterIndex >= StartIndex)
{
Register += Source[i].ToString();
if (Register.Length == Max)
{
return Register;
}
}
RegisterIndex += 1;
}
}
return Register;
}
Upvotes: 0
Reputation: 3915
You can create an extension method to List like so:
static class Helper
{
public static void Push<T>(this List<T> list, T item)
{
if (list.Count == 5)
list.RemoveAt(0);
list.Add(item);
}
}
And then you can use it like:
List<char> queue = new List<char>(5);
queue.Push('1');
queue.Push('0');
queue.Push('4');
queue.Push('0');
queue.Push('0');
Subsequent Call to Push will remove the first char and add the last
queue.Push('1');
Upvotes: 0
Reputation: 71573
OK...
First, a FileStream and associated StreamReaders will allow you to read from the file in pretty much any format you desire. This will be important because your specific algorithm will determine the retrieval method.
Boiling it down, you want to read characters from the file, and when that character is a number, store it in the register, continuing in this manner until you have five number characters in the register. Then, you'll do some logic that results in the first number no longer being useful, so you truncate it and get the next value.
How about something along these lines?
var register = new StringBuilder();
using(var stream = File.Open("File1.txt"))
{
bool ended, fileEnded;
int buffer;
while(!ended)
{
while(register.Length < 5 && !fileEnded)
{
buffer = stream.ReadByte();
if(buffer == -1)
{
fileEnded = true;
break;
}
var myChar = (char)buffer;
if(Char.IsNumber(myChar))
StringBuilder.Append(myChar);
}
//at this point you have 5 characters in register (or have run out of file).
//perform your logic, then remove the front character
register.Remove(0,1);
//repeat the loop. You won't get any more new characters once you reach the end of file,
//but the main loop will keep running until you set ended to true
if(WereDone())
ended=true;
}
stream.Close();
}
You could also read the entire file into a string variable, then apply a Regex that will find number characters, concatenate those into a large buffer, then fill your Register from that. That is a better approach for a small file, but this one will work for any file size.
Upvotes: 0
Reputation: 241661
Well, if you want to lop the first character off a string and add on one at the end, you can just say:
string s = "10400";
string t = s.Substring(1) + "0";
This gives t = "04000"
. Repeating:
string u = t.Substring(1) + "3";
This gives u = "40003"
.
So, what more do you want? Figuring out the logic of what to add to the end is your job.
Upvotes: 1