Skocz do zawartości

czytanie z pliku txt


# CSH External VIP Project

Masz dosyć problemów z czynnikiem zaufania w CS2 lub notorycznymi banami?

Sprawdź CSH External VIP Project.


Więcej informacji  

Rekomendowane odpowiedzi

elo ma ktoś poradnik czy jakoś mi mógł pomóc więc.
Chciałbym zrobić program (aka bruteforce) ale nie umiem zrobić by program czytał linijke
w pliku txt.
tzn. jest aaa pozniej bbb , ccc w tym pliku i czyta linijke 1 i ją wkleja i pozniej 2 > 3 >4 >5 > itd

ma ktos jakiegos tutoriala xD?

Odnośnik do komentarza

getline, proste

 

http://www.cplusplus.com/reference/string/string/getline/

 

przykład z kodu mojej gierki, wczytywanie tekstu z pliku txt, gdzie przechowuje dane o nazwie postaci itp.

void LoadCharacter::Load()
{
	cout << "Wybierz plik zapisu, wpisujac jego nazwe (bez .txt)." << endl;
	cin >> SaveName;
	fstream loading(".\\saveFiles\\" + SaveName + ".txt", ios::in);

	if (loading.good() == true)
	{
		string line; int LineNumber = 0;

		while (getline(loading,line))
		{
			switch (LineNumber)
			{
			case 0: name = line; break;
			case 1: origin = line; break;
			case 2: CharacterClass = line; break;
			case 3: race = line; break;
			case 4: gender = line; break;
			case 5: strength = atoi(line.c_str()); break;
			case 6: dexterity = atoi(line.c_str()); break;
			case 7: intelligence = atoi(line.c_str()); break;
			}
			LineNumber++;
		}
		loading.close();
	}

	else
	{
		cout << "Wybrany plik jest nieprawidlowy lub uszkodzony." << endl;
	}
}
Odnośnik do komentarza

elo ma ktoś poradnik czy jakoś mi mógł pomóc więc.

Chciałbym zrobić program (aka bruteforce) ale nie umiem zrobić by program czytał linijke

w pliku txt.

tzn. jest aaa pozniej bbb , ccc w tym pliku i czyta linijke 1 i ją wkleja i pozniej 2 > 3 >4 >5 > itd

ma ktos jakiegos tutoriala xD?

Może zainteresuje Cię ta wklejka:

class Program
{
    #region Private variables
 
    // the secret password which we will try to find via brute force
    private static string password = "p123";
    private static string result;
 
    private static bool isMatched = false;
 
    /* The length of the charactersToTest Array is stored in a
     * additional variable to increase performance  */
    private static int charactersToTestLength = 0;
    private static long computedKeys = 0;
 
    /* An array containing the characters which will be used to create the brute force keys,
     * if less characters are used (e.g. only lower case chars) the faster the password is matched  */
    private static char[] charactersToTest =
    {
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
        'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
        'u', 'v', 'w', 'x', 'y', 'z','A','B','C','D','E',
        'F','G','H','I','J','K','L','M','N','O','P','Q','R',
        'S','T','U','V','W','X','Y','Z','1','2','3','4','5',
        '6','7','8','9','0','!','$','#','@','-'
    };
 
    #endregion
 
    static void Main(string[] args)
    {
        var timeStarted = DateTime.Now;
        Console.WriteLine("Start BruteForce - {0}", timeStarted.ToString());
 
        // The length of the array is stored permanently during runtime
        charactersToTestLength = charactersToTest.Length;
 
        // The length of the password is unknown, so we have to run trough the full search space
        var estimatedPasswordLength = 0;
 
        while (!isMatched)
        {
            /* The estimated length of the password will be increased and every possible key for this
             * key length will be created and compared against the password */
            estimatedPasswordLength++;
            startBruteForce(estimatedPasswordLength);
        }
 
        Console.WriteLine("Password matched. - {0}", DateTime.Now.ToString());
        Console.WriteLine("Time passed: {0}s", DateTime.Now.Subtract(timeStarted).TotalSeconds);
        Console.WriteLine("Resolved password: {0}", result);
        Console.WriteLine("Computed keys: {0}", computedKeys);
 
        Console.ReadLine();
    }
 
    #region Private methods
 
    /// <summary>
    /// Starts the recursive method which will create the keys via brute force
    /// </summary>
    /// <param name="keyLength">The length of the key</param>
    private static void startBruteForce(int keyLength)
    {
        var keyChars = createCharArray(keyLength, charactersToTest[0]);
        // The index of the last character will be stored for slight perfomance improvement
        var indexOfLastChar = keyLength - 1;
        createNewKey(0, keyChars, keyLength, indexOfLastChar);
    }
 
    /// <summary>
    /// Creates a new char array of a specific length filled with the defaultChar
    /// </summary>
    /// <param name="length">The length of the array</param>
    /// <param name="defaultChar">The char with whom the array will be filled</param>
    /// <returns></returns>
    private static char[] createCharArray(int length, char defaultChar)
    {
        return (from c in new char[length] select defaultChar).ToArray();
    }
 
    /// <summary>
    /// This is the main workhorse, it creates new keys and compares them to the password until the password
    /// is matched or all keys of the current key length have been checked
    /// </summary>
    /// <param name="currentCharPosition">The position of the char which is replaced by new characters currently</param>
    /// <param name="keyChars">The current key represented as char array</param>
    /// <param name="keyLength">The length of the key</param>
    /// <param name="indexOfLastChar">The index of the last character of the key</param>
    private static void createNewKey(int currentCharPosition, char[] keyChars, int keyLength, int indexOfLastChar)
    {
        var nextCharPosition = currentCharPosition + 1;
        // We are looping trough the full length of our charactersToTest array
        for (int i = 0; i < charactersToTestLength; i++)
        {
            /* The character at the currentCharPosition will be replaced by a
             * new character from the charactersToTest array => a new key combination will be created */
            keyChars[currentCharPosition] = charactersToTest[i];
 
            // The method calls itself recursively until all positions of the key char array have been replaced
            if (currentCharPosition < indexOfLastChar)
            {
                createNewKey(nextCharPosition, keyChars, keyLength, indexOfLastChar);
            }
            else
            {
                // A new key has been created, remove this counter to improve performance
                computedKeys++;
 
                /* The char array will be converted to a string and compared to the password. If the password
                 * is matched the loop breaks and the password is stored as result. */
                if ((new String(keyChars)) == password)
                {
                    if (!isMatched)
                    {
                        isMatched = true;
                        result = new String(keyChars);
                    }
                    return;
                }
            }
        }
    }
 
    #endregion
}

Źródło: https://gist.github.com/jwoschitz/1129249

Odnośnik do komentarza
Gość
Ten temat został zamknięty. Brak możliwości dodania odpowiedzi.

  Tagi

×
×
  • Dodaj nową pozycję...

Powiadomienie o plikach cookie

Umieściliśmy na Twoim urządzeniu pliki cookie, aby pomóc Ci usprawnić przeglądanie strony. Możesz dostosować ustawienia plików cookie, w przeciwnym wypadku zakładamy, że wyrażasz na to zgodę. Regulamin. Polityka prywatności