無題
Posted: 2009年7月15日(水) 00:24
初心者です。
すいません。宿題なんですけど、色々やっても動きません。
何処を改善すればいいかお願いします。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h> /*strcat関数で必要 */
int main()
{
srand((unsigned) time(NULL)); /* 乱数系列の変更 */
int i, j;
FILE *fp;
char bun[256], file[33][30];
fp = fopen("test2.txt", "r"); /* ここにファイル名を入れる */
if(fp == NULL) {
perror("ファイルをオープンできません\n");
return -1;
}
i = 0;
while(fscanf(fp, "%s", &file[0]) != EOF) {
i++;
}
fclose(fp);
fp = fopen("test3.txt", "w"); /* ここに書き込むファイルの名前を入れる */
if(fp == NULL) {
perror("ファイルをオープンできません\n");
return -1;
}
j = 0;
while(j < 20) {
i = 0;
i = rand()%11;
strcpy(bun, &file[0]);
strcat(bun, "は");
i += (12 - i);
i += rand()%9;
strcat(bun, &file[0]);
i += (22 - i);
i += rand()%10;
strcat(bun, &file[0]);
strcat(bun, "\n");
fprintf(fp, bun);
j++;
}
fclose(fp);
return 0;
}
すいません。宿題なんですけど、色々やっても動きません。
何処を改善すればいいかお願いします。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h> /*strcat関数で必要 */
int main()
{
srand((unsigned) time(NULL)); /* 乱数系列の変更 */
int i, j;
FILE *fp;
char bun[256], file[33][30];
fp = fopen("test2.txt", "r"); /* ここにファイル名を入れる */
if(fp == NULL) {
perror("ファイルをオープンできません\n");
return -1;
}
i = 0;
while(fscanf(fp, "%s", &file[0]) != EOF) {
i++;
}
fclose(fp);
fp = fopen("test3.txt", "w"); /* ここに書き込むファイルの名前を入れる */
if(fp == NULL) {
perror("ファイルをオープンできません\n");
return -1;
}
j = 0;
while(j < 20) {
i = 0;
i = rand()%11;
strcpy(bun, &file[0]);
strcat(bun, "は");
i += (12 - i);
i += rand()%9;
strcat(bun, &file[0]);
i += (22 - i);
i += rand()%10;
strcat(bun, &file[0]);
strcat(bun, "\n");
fprintf(fp, bun);
j++;
}
fclose(fp);
return 0;
}