//boy3vil
//open file
#include <stdio.h>
#include <conio.h>
#define MAX 50
#define MAX_DATA 10
#define NEWLINE printf("\n");
int GetSize(const char*);
void NewLineEraser(char*);
int main(int argc, char *argv[]) {
char a[30];
printf("Masukkan nama file=");
scanf ("%s",a);
FILE *stream = fopen(a,"w+"); int i, max;
char nama[MAX], tujuan[MAX];
printf("- Jumlah data : ");
scanf("%d",&max); fflush(stdin);
if((max > 0) && (max <= MAX_DATA)) {
for(i = 0; i < max; ++i) {
printf("1. Nama : ");
fgets(nama,(sizeof(nama)/sizeof(char))-1,stdin);
fflush(stdin); NewLineEraser(nama);
printf("2. Tujuan : ");
fgets(tujuan,(sizeof(tujuan)/sizeof(char))-1,stdin);
fflush(stdin); NewLineEraser(tujuan); NEWLINE;
fprintf(stream,"1. Nama : %s\n",nama);
fprintf(stream,"2. Tujuan : %s\n\n",tujuan);
}
}
fclose(stream);
getch();
fflush(stdin);
return(0);
}
int GetSize(const char* buffer) {
int i = 0;
while(buffer[i] != '\0') {
++i;
} return(i);
}
void NewLineEraser(char* buffer) {
if(buffer[GetSize(buffer)-1] == '\n') {
buffer[GetSize(buffer)-1] = '\0';
}
}
Categories:
blog,
c program,
pemrograman1