Passing unique items to a file.

closed account (LzqpfSEw)
I'm writing a program that passes random letters to a file, but because it's random I usually get letters that are the same.

How can I perform a check before passing the letters to a file to see if that particular letter has already been sent?

__

Tried several things without any luck, I'm not in a programming class so don't worry about doing my homework.
closed account (D80DSL3A)
I tried this because it seemed like a good exercise for me.
I came up with 2 methods:

The easy way.
a) Fill a character array with all 26 letters then use random_shuffle() to "randomize" the order of the elements.
b) Write all 26 letters to the file.

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
#include <iostream>
#include <fstream>
#include <cstdlib>// for rand()
#include<ctime>
#include<algorithm>// for random_shuffle()
using namespace std;

int main()
{
    char ltrs[26];// an array to hold all 26 letters

    srand((unsigned)time(0));// seed random # generator

    for(int i=0; i<26; ++i)
        ltrs[i] = 'a' + rand()%26;// assign letters in alphabetic order
    random_shuffle( ltrs, ltrs+26);// shuffle the letters in the array randomly

   fstream fs("test.txt", fstream::out); 
    if( fs.is_open() )
    {
        for(int i=0; i<26; ++i)// write all the letters to the file.
            fs << ltrs[i];
        fs.close();
    }
    else
        cout << "could not open file.";

    cout << endl;
    return 0;
}


The hard way.
a)Generate a character randomly.
b) Search the file for this character.
c) If the character was not found then write the character to the file, otherwise generate another random character and try again.

The program below includes extra stuff for counting the # of tries to insert the characters.
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
#include <iostream>
#include <fstream>
#include <cstdlib>// for rand()
#include<ctime>
using namespace std;

int main ()
{
    fstream fs;

    fs.open("test.txt", fstream::out);// create or overwrite the file
    fs.close();

    fs.open("test.txt", fstream::in | fstream::out);// open for reading and writing
    if( fs.is_open() )
    {
        // write 1st char
        srand((unsigned)time(0));
        char ch = 'a' + rand()%26;
        fs << ch;
        cout << "First char = " << ch << endl;

        // subsequent chars
        int i=1;// counts # of characters written to file
        int tries=0, totTries=0;// counts # of tries to insert a char into the file
        while( i < 26 )// for all remaining characters
        {
            bool found = false;// ch already in file?
            ch = 'a' + rand()%26;
            ++tries;

            fs.clear();// clear eof flag
            fs.seekg (0, ios::beg);// start at beginning of file
            while( !fs.eof() )// search for ch in the file
            {
                char chTest;
                fs >> chTest;
                if( chTest == ch )
                {
                    found = true;// ch was found in the file
                    break;
                }
            }
            if( !found )// ch not found in the file
            {
                fs.clear();
                fs.seekg (0, ios::end);// go to end of file
                fs << ch;// insert ch
                ++i;// character count incremented
//                cout << "inserted char = " << ch << " after " << tries << " tries." << endl;
                cout << "inserted char = " << ch << " after " << tries << (tries==1 ? "try." : " tries.") << endl;
                totTries += tries;
                tries = 0;// reset for next character
            }
        }
        cout << "total tries = " << totTries << endl;

        fs.close();// done!
    }
    else
        cout << "could not open file.";

    cout << endl;
return 0;
}


I hope that's what you were looking for!
Last edited on
closed account (LzqpfSEw)
After a few segmentation faults and compile errors I did it.

Works like a dream, thank you. :)
Topic archived. No new replies allowed.