I have been looking for an answer for the last few hours on google with no success. I am trying to create a program that will determine what file to open from user input.

For example:

names of the text files are xaa through xzz.

The text files contain integers that are incremented 1 to 250,000,000.

Each file contains 370,000 integers except the last file.
xaa - 1 through 370,000
xab - 370,000 through 740,000
etc.

So the program asks which number they would like to find and the file goes to the file with that number and opens that line then prints it to the screen.

I am having a hard time figuring out how to tell the program which file to open. I know I can take the number entered and divided by 370000 to find out what file it is but how to I translate that into the file name? (example the number requested is 200, 200/370000 is less then 1 so it would be on the first page.

Any help would be appreciated.

Thanks.

Suppose n is the number you're looking for. Then:

const int chunksize = 370000, numletters = 26;
assert(n < numletters * numletters * chunksize);
int chunkno = n / chunksize;
int digit1 = chunkno / numletters;
int digit2 = chunkno % numletters;
const char* alphabet = "abcdefghijklmnopqrstuvwxyz";
string filename = string("x") + alphabet[digit1] + alphabet[digit2];

Edited 5 Years Ago by arkoenig: changed const char to const char*

Think of the letters as having a place value, 'a' = 0, 'b' = 1, so "aa" is 0*25+0, "ab" is 0*25+1, "zz" is equal to 25*25 + 25 (check my math, lol).

To get the value of a character, subtract 'a'.

EDIT: arkoenig's got it (and in much nicer form)

Edited 5 Years Ago by jonsca: n/a

To get the value of a character, subtract 'a'.

This technique works on most computers in use today, but it is not actually guaranteed to work. That's why I coded my example the way I did.

This technique works on most computers in use today, but it is not actually guaranteed to work. That's why I coded my example the way I did.

Very good to know, thank you.

Suppose n is the number you're looking for. Then:

const char alphabet = "abcdefghijklmnopqrstuvwxyz";

I'm assuming you meant const char * here?

Edited 5 Years Ago by jonsca: n/a

Very good to know, thank you.

One reason it's not portable is that IBM mainframes use a character set in which 'a' through 'i' are hex 81 through 89, but 'j' is hex 91. Much more information than you ever wanted to know about this character set can be found here.

Edited 5 Years Ago by arkoenig: n/a

Thank you all so much for your help. That is what I needed. I really appreciate the quick response.

This article has been dead for over six months. Start a new discussion instead.