Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I am looking to create a small windows app that will take a text file of hundreds, maybe thousands of lines of text and then randomize the text and print 5 lines seperated by a line break. I should be able to copy from the app and each time I hit the "generate" button it should delete the previous 5 text outputs.
Here's an example:
https://www.random.org/lists/
The difference is that this app randomizes and prints all lines. Could someone point me to some resources on how to do this exact thing?
Microsoft own c# developer portal has examples of how to read from a text file
How to: Read From a Text File (C# Programming Guide) which can get you started with loading the text.
Microsoft's own Developer Network also has information on random number generation and examples at Random Class
finally Microsoft's own ASP.Net ASP.NET Samples has load of examples and information about building web (or desktop) applications.
You should be able to find working examples and API information on all three of these locations, that will help you with your quest of development of C# applications.
//Initialize variables
static Random rnd;
static StreamReader reader;
static List<string> list;
//here we load the text file into a stream to read each line
using (reader = new StreamReader("TextFile1.txt"))
{
string line;
list = new List<string>();
rnd = new Random();
int index;
//read each line of the text file
while (!reader.EndOfStream)
{
line = reader.ReadLine();
//add the line to the list<string>
list.Add(line);
}
//pull 5 lines at random and print to the console window
for (int i = 0; i < 5; i++)
{
index = rnd.Next(0, list.Count);
Console.WriteLine(list[index]);
}
}
Console.ReadKey();
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
how can i get the numbers that are coded in a ascii art with sticks?
the numberss are in a txt file und it contains this:
I must convert this txt file in
3 2 1 4 5
1 4 5
I read the text file so:
using (StreamReader sr = new StreamReader("SourceFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line);
}
}
string allines = sb.ToString();
Now, like the answer of #Zotta i have to save in two different strings (the first 4 lines and the seconds, than
then will be easier
Your numbers are 4 lines tall each => Split input into blocks of 4 Lines each
Your numbers are separated by columns of whitespace => Search for colums containing only whitespaces and split.
After you separated all the numbers, use a lookup table.
I don't know why this question is down-voted so much but I think it's interesting question. I'll answer giving a general approach other than hardcoding the possible results by finding the characters and that would work with different "ASCII font".
If you're looking for a library, maybe you can look at captcha decoding on google. There is a comprehensive article here if you want to do it yourself for ASCII specifically:
http://www.boyter.org/decoding-captchas/
Also, since most libraries probably only support images, maybe you'll need to convert your ascii art text file into a bitmap by rendering it yourself.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
in c# i need to compare 2 numbers one from a local file, and one another from a downloaded file like a Patcher.
if I use Streamreader c# sad to me that he can't convert string into INT.
are there a solution for this?
file a contains the value "1" , the file b contains the value "2"
so if b>a then download the new files catch from another updater file.
thanks
If that is the only number in the file, you can use File.ReadAllText (or File.ReadAllLines in a multiline file) and convert to int like this:
string[] lines = File.ReadAllLines(#"c:\t.txt");
int number = Convert.ToInt32(lines[0]);
try to use the Convert.ToInt32 method.
If your file contains olny one number, you could use the File.ReadAllLine method, insted of streamreader.
void CompareVersions()
{
WebClient client = new WebClient();
var serverVersion = client.DownloadString("http://yourwebsite.com/version.txt");
using (StreamReader sr = new StreamReader("file.txt"))
{
if (Convert.ToInt32(serverVersion) > Convert.ToInt32(sr.ReadLine()))
{
// server version bigger
}
else
{
// up to date
}
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
So, I'm programming a little word game for a project (with different kinds of didactic games) which basically shuffles the word and the user has to guess it.
Easy so far, but then the same word can't come up more than 1 time so I was using a list to save the used words until I find out the memory problem.
Is there any other way to do it?
My code is this one:
String mixed = "";
while (mixed == "" || mixed == word)
{
mixed = Shuffle(word);
}
for (int i = 0; i < MainWords.used.Count; i++)
{
if (word != MainWords.used[i])
{
MainWords.used.Add(word);
palavraigual = false;
}
}
After 12~15 words the program will run into a out of memory exception. Thanks in advance.
You can be more efficient than a for-loop. This also prevents you from accidentally making the mistake you did.
String mixed = "";
while (mixed == "" || mixed == word)
{
mixed = Shuffle(word);
}
if (!MainWord.used.Contains(word))
{
MainWord.used.Add(word);
palavraigual = false;
}
public static class MainWord
{
public static HashSet<string> used;
}
Instead of the loop, the above utilizes the Contains method. In this case, the HashSet provides a constant-time lookup; however, using Contains rather than a loop to check existence is still good practice even if you using a List or other standard data structure.
Since the above does no looping, it is difficult to accidentally add the word more than once; however, Sets have another beneficial behavior here. They cannot not contain duplicates. If you kept your looping code but changed the data structure to a HashSet you would still avoid your OutOfMemory problem.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I need a library to read 2D barcode (datamatrix) from images on C# project (windows Forms).
I tried using some SDKs, but the SDKs I tried are not free.
Is there any free SDK for reading 2d Barcode from images?
There's an example available:
using DataMatrix.net; // Add ref to DataMatrix.net.dll
using System.Drawing; // Add ref to System.Drawing.
[...]
// ---------------------------------------------------------------
// Date 180310
// Purpose Get text from a DataMatrix image.
// Entry sFileName - Name of the barcode file (PNG, + path).
// Return The text.
// Comments See source, project DataMatrixTest, Program.cs.
// ---------------------------------------------------------------
private string DecodeText(string sFileName)
{
DmtxImageDecoder decoder = new DmtxImageDecoder();
System.Drawing.Bitmap oBitmap = new System.Drawing.Bitmap(sFileName);
List<string> oList = decoder.DecodeImage(oBitmap);
StringBuilder sb = new StringBuilder();
sb.Length = 0;
foreach (string s in oList)
{
sb.Append(s);
}
return sb.ToString();
}
You'll need DataMatrix.net!
Best free Datamatrix coder\decoder that i've used is libdmtx: http://www.libdmtx.org/ . It has c# wrapper, so feel free to use it. I can't write sample code right now, but if you won't be able to handle it yourself, i'll help you a bit later with that.
EDIT:
libdmtx comes with console utils - if you will be able to read your barcodes with console app, you surely will read it using code.
EDIT2:
Here's code samples: http://libdmtx.wikidot.com/libdmtx-net-wrapper
I wonder if you have pictures containing some other info, except the barcode. The thing is - i don't know any free\open source lib to handle finding barcode on a picture, containing any other data properly.
And here's the link to other datamatrix implementations: http://www.libdmtx.org/resources.php
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I need to scramble chars in word. And this scrambling must not be random. In other words, each time of scrambling (on the same word) the new result must be equal to last scramble result (on the same word). Very simple example is XOR. But XOR is very easy to decode and I need something more strength. Could you recommend library for such purpose that identically works on C# and Javascript?
Thank you for any advice!:)
You can use random with fixed seed if you really want to scramble characters:
string input = "hello";
char[] chars = input.ToArray();
Random r = new Random(2011); // Random has a fixed seed, so it will always return same numbers (within same input)
for (int i = 0 ; i < chars.Length ; i++)
{
int randomIndex = r.Next(0, chars.Length);
char temp = chars[randomIndex];
chars[randomIndex] = chars[i];
chars[i] = temp;
}
return new string(chars);
Although I don't really agree about what you were trying to do, here's a link to MD5 javascript library (assuming what you're trying to do is some kind of encryption). As for the C# part, it's a built in feature.
You can use any of the built in .NET classes to generate random numbers and use these to scramble your string. For all the subsequent scramble attempts you can use the result from the first scramble operation. This assumes that the result from the first call is stored somewhere.