나는 지금 C를 공부하고 있는데 아직 좋지 않다. 나는 사람들의 이름을 입력하고 동시에 .txt 파일을 만들려는 프로그램을 작성하려고합니다. 예를 들어 "Richard"라고 입력하면 Richard.txt 파일이 생성됩니다. 그리고 .txt 파일 안에 다시 이름을 적고 싶습니다.C에서 다른 파일에 데이터 쓰기
첫 번째 이름을 입력하고 첫 번째 .txt 파일을 만든 후에는 새 이름을 입력해도 새 .txt 파일이 만들어지지 않습니다. 대신 첫 번째 이름 뒤에 첫 번째 .txt 파일에 두 번째 이름을 넣습니다.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
struct personnel
{
char name[40];
};
int addPatient(struct personnel patient[], int noAgt);
void writeFile(struct personnel patient[], int noAgt, char filename[]);
void emptyBuffer(void);
int main()
{
struct personnel patient[50];
int ch = 'X';
int noAgt = 0;
char filename[100];
while (ch != 'q')
{
printf("\na)\tEnter new patient"
"\nb)\tWrite file"
"\nc)\tExit program"
"\n\nSelect: ");
ch = getche();
printf("\n\n");
switch (ch)
{
case 'a' :
noAgt = addPatient(patient, noAgt);
break;
case 'b' :
writeFile(patient, noAgt, filename);
break;
case 'c' :
exit(0);
}
}
}
int addPatient(struct personnel patient[], int noAgt)
{
printf("\nPatient %d.\nEnter name: ", noAgt + 1);
scanf("%39[^\n]", patient[noAgt].name);
while(getchar() != '\n')
{
;
}
return ++noAgt;
}
void writeFile(struct personnel patient[], int noAgt, char filename[])
{
int i;
FILE *fptr;
struct personnel rec;
strcpy(filename, patient[i].name);
emptyBuffer();
strcat(filename, ".aow.txt");
fptr = fopen(filename, "w");
for(i = 0; i < noAgt; i++)
{
rec = patient[i];
fprintf(fptr, "Name: %s ", rec.name);
}
fclose(fptr);
printf("\nFile of %d patients written.\n", noAgt);
}
void emptyBuffer(void) /* Empty keyboard buffer */
{
while(getchar() != '\n')
{
;
}
}
"INT addPatient (구조체 인사 환자 [], INT noAgt)"나는()의 WriteFile에가는 사람의 이름을 입력 비트입니다.
"void writeFile (struct personnel patient patient [], int noAgt, char filename [])"파일을 쓰는 곳의 비트입니다.