附加随机文本,不重复文件(C) [英] Append Random Text without Repetition for File (C)
问题描述
我有5个姓名列表
char *name[] = {"a","b","c","d","e"};
我有3个文件
char path1[PATH_MAX+1]
snprintf(path1, PATH_MAX+1, "%sfile1.txt",dirname);
FILES *filename1 = fopen(path1, "w")
.
.
.
char path3[PATH_MAX+1]
snprintf(path3, PATH_MAX+1, "%sfile3.txt",dirname);
FILES *filename3 = fopen(path3, "w")
我想要的是将 a,b,c,d,e (每个文件一个)随机添加到其中三个文件中,而无需重复.
What I want is to randomly append a,b,c,d,e (one of them per file) into three of those files without repetition.
我现在拥有的是(其中一个例子)
srand(time(NULL));
int one = rand()%5;
char path1[PATH_MAX+1];
snprintf(path1, PATH_MAX+1, "%sfile1.txt",dirname);
FILES *filename1 = fopen(path1, "w");
fputs(name[one],filename1);
fclose(filename1);
但是,有时我的 file1.txt 和 file3.txt 都包含 b (与名称)
However, sometimes it is still possible where my file1.txt and file3.txt both contain b (same alphabet from name)
问题
我是否错过了某些东西来确保所有随机结果总是唯一的?
Did I miss something to make sure that all the random result always unique?
用6行代码创建一个文件并在其中添加一个随机名称是否也很有效?我只是想知道是否必须创建20个文件,所以我会写120行,这些行基本相同,只是数量不同(文件名 1 到文件名 3 )
Is it also efficient tho to have 6 lines of code to create one file and append a random name inside it? I'm just wondering if I have to create like 20 files, I will write 120 lines that basically almost the same, just different in number (filename1 to filename3)
谢谢.
推荐答案
要获得唯一的字符序列,可以从逐渐减小的池中绘制它们.例如,选择a
后,将从池中将其删除.当然,该池必须至少与要打印的文件数量一样大.
To get a unique sequence of characters, you can draw them from a diminishing pool. For example, after you have picked the a
, it is removed from the pool. Of course, the pool must be at least as big as the number of files you want to print.
实现这种选择的一种简单方法是从池中选择一个字符,将池中的最后一个字符移动到所选择字符的位置,并将池大小减小一个.
A simple way to implement this sort of picking is to pick a char from the pool, move the last character from the pool to the place of the picked character and decrease the pool size by one.
如果您看到大量的代码重复,尤其是如果唯一的区别是沿着filename1
,filename2
,filename3
等行的变量名称,则应该按钟声,您应该使用数组:FILE *file[NFILE]
.但是请注意,一次只能打开一定数量的文件.
If you see a lot of repetition of code, especially if the only difference is a variable name along the lines of filename1
, filename2
, filename3
and so on should ring a bell that you should use an array: FILE *file[NFILE]
. Be aware, though, that you can only have a certain number of files open at a time.
在您的情况下,您想将一个字符写入文件.无需同时打开多个文件:打开文件,选择一个字符,将其写入文件,然后关闭文件.然后处理下一个文件.
In your case, you want to write a single character to a file. There's no need to have multiple file s open simultaneously: Open a file, pick a char, write it to the file, close e file. Then process the next file.
我想下面的程序可以满足您的需求.
The program below does what you want, I think.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define NFILES 10
int main()
{
char pool[] = "abcdefghij"; // Available chars as array
int npool = sizeof(pool) - 1; // Pool size minus terminating '\0'
int i;
if (npool < NFILES) {
fprintf(stderr,
"Not enough letters in pool for %d files.\n", NFILES);
exit(1);
}
srand(time(NULL));
for (i = 0; i < NFILES; i++) {
int ipick = rand() % npool; // position of pick
char pick = pool[ipick]; // picked char
char fname[20];
FILE *f;
pool[ipick] = pool[--npool]; // remove pick from pool
snprintf(fname, sizeof(fname), "file-%03d.txt", i);
f = fopen(fname, "w");
if (f == NULL) {
fprintf(stderr, "Could not create %s.\n", fname);
exit(1);
}
fprintf(f, "%c\n", pick);
fclose(f);
}
return 0;
}
这篇关于附加随机文本,不重复文件(C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!