Reading last line of txt file and inputting it to variable

Hi, I've recently put together a program and have had some difficulties.

What I want the program to do is spit out every 8 letter word possible (upper and lower case mixed with most special characters) into a .txt file. I found a way to do it, but it'll take way too long to get absolutely every word. Eventually I had to close the executable, and I need a way for the program to pick up where it left off. Otherwise, it'd be better for the program to send every 8 letter string in 1 go instead of having to leave it on for some time.

Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <fstream>
#include <windows.h>
#include <string>

using namespace std;

const char Key[92] =
{
	'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'
};

// Recursive function, keeps clocking characters
// until length is reached

void Generate(unsigned int length, string s)
{
		
		if (length == 0) // when length has been reached
		{
			//	cout << s << "\n"; // print it out

			ofstream myfile("Test.txt", ios_base::app | ios_base::out);
			if (myfile.is_open())
			{
				myfile << ("%s", s);
				myfile << "\n";
				myfile.close();
			}

			return;
		}

		for (unsigned int i = 0; i < 92; i++) // iterate through Keys
		{
			// Create new string with next character
			// Call generate again until string has reached it's length
		
			string appended = s + Key[i];
			Generate(length - 1, appended);
		}
}


void Start()
{
	while (1)
	{
		// Keep growing till I get it right
		static unsigned int stringlength = 8;
		Generate(stringlength, "");
		stringlength++;
	}
}

int main()
{
	cerr << "Starting program in 3...";
	Sleep(3000);
	Start();
	return 0;
}


Any help is wanted, thanks.
Last edited on
What I want the program to do is spit out every 8 letter word possible (upper and lower case mixed with most special characters) into a .txt file.

read the input file word by word (delimited by whitespace), check size of word and send to output file those words that meet the size criterion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <fstream>

int  main()
{
   std::ifstream inFile{"D:\\inputFile.txt"};
   std::ofstream outFile{"D:\\outputFile.txt"};
   std::string word{};
   while (inFile >> word)
   {
       if (word.size() == 8)
       {
           if(inFile)
           {
               outFile << word << "\n";
           }
       }
   }
}
Last edited on
So Test.txt has to have some order.

You need to read the last line of Test.txt and use that as your starting point.


code I wrote for another like project below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

string line;
string lastline;
//...
    if (inputfile.is_open())
		{
		while ( inputfile.good())
			{
			// code here
					getline (inputfile,line);

					if (!line.empty())
					{
					count++;
					lastline=line;
					}
			}
		}
	else
	{
	        cout << "File not open" << endl;
	}

Topic archived. No new replies allowed.