Anda di halaman 1dari 7

PROGRAM 1

#include <stdio.h>
#include <stdlib.h>

int main() {
FILE *pf;
char kar;

/* Ciptakan file */
if ((pf = fopen("COBA.TXT", "w")) == NULL) {
printf("File tidak dapat diciptakan!\n");
exit(1);
}

printf("Ketikkan apa saja, akhiri dengan ENTER.\n");


printf("Program akan membaca per karakter\n");
printf("dan menyimpannya dalam file COBA.TXT\n\n");

while ((kar = getchar()) != '\n') {


/* Baca karakter dari keyboard */
fputc(kar, pf); /* Tulis ke file per karakter */
}

fclose(pf);

return 0;
}

PROGRAM 2
#include <stdio.h>
#include <stdlib.h>

int main() {
FILE *pf;
char kar;

if ((pf = fopen("COBA.TXT", "r")) == NULL) {


printf("File tidak dapat dibuka!\n");
exit(1);
}

while ((kar = fgetc(pf)) != EOF) {


putchar(kar);
}

printf("\n");
fclose(pf);

return 0;
}

PROGRAM 3
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *pf;
int nilai, sudah_benar;
char jawab;

if ((pf = fopen("BILANGAN.DAT", "wb")) == NULL) {


printf("File gagal diciptakan!\n");
exit(1);
}

printf("MENYIMPAN DATA INTEGER KE FILE\n");


do {
printf("\nBilangan yang akan disimpan: ");
scanf("%d", &nilai); // baca nilai dari keyboard
putw(nilai, pf); // simpan bilangan ke file

printf("Memasukkan data lagi (Y/T)? ");


do {
jawab = getchar(); // baca jawaban dari keyboard
} while (jawab != 'Y' && jawab != 'y' && jawab != 'T' && jawab != 't');

sudah_benar = (jawab == 'T' || jawab == 't');


} while (!sudah_benar);

printf("\nOke. Data sudah disimpan dalam file.\n");


fclose(pf); // tutup file

return 0;
}

PROGRAM 4
#include <stdio.h>
#include <stdlib.h>

int main() { // You should specify the return type of 'main' as 'int'

FILE *pf; // Declare a pointer to a file


int nilai, nomor = 0;

// Open the binary file for reading


if ((pf = fopen("BILANGAN.DAT", "rb")) == NULL) {
printf("File gagal dibuka.\n");
exit(1);
}

printf("Isi file BILANGAN.DAT:\n");


while (1) {
nilai = fgetc(pf); // Use 'fgetc' to read a single character from the file

if (feof(pf) != 0) {
break; // If the end of the file is reached, exit the loop
}

printf("%2d. %d\n", ++nomor, nilai);


}
fclose(pf); // Close the file
return 0; // Return 0 to indicate successful execution
}

PROGRAM 5
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
FILE *f_struktur;
struct {
char judul[261];
char pengarang[20];
int jumlah;
} buku;

if ((f_struktur = fopen("DAFBUKU.DAT", "wb")) == NULL) {


printf("File tidak dapat diciptakan!\n");
exit(1);
}

fflush(stdin); // Clear the input buffer

printf("Judul buku: ");


fgets(buku.judul, sizeof(buku.judul), stdin); // Read book title

// Remove the newline character from the end of the input


buku.judul[strcspn(buku.judul, "\n")] = 0;

printf("Nama pengarang: ");


fgets(buku.pengarang, sizeof(buku.pengarang), stdin); // Read author name

// Remove the newline character from the end of the input


buku.pengarang[strcspn(buku.pengarang, "\n")] = 0;

printf("Jumlah: ");
scanf("%d", &buku.jumlah); // Read the quantity

fwrite(&buku, sizeof(buku), 1, f_struktur); // Write the structure to the file

fclose(f_struktur);
return 0;
}

PROGRAM 6
#include <stdio.h>
#include <stdlib.h>

int main() {
FILE *f_struktur;
struct {
char judul[30];
char pengarang[30];
int jumlah;
} buku;
int i = 1;

if ((f_struktur = fopen("DAFBUKU.DAT", "rb")) == NULL) {


printf("File tidak dapat dibuka!\n");
exit(1);
}

printf("%-2s %-30s %-30s %s\n", "No", "Judul Buku", "Nama Pengarang", "Jumlah");

while (fread(&buku, sizeof(buku), 1, f_struktur) == 1) {


printf("%-2d %-30s %-30s %d\n", i++, buku.judul, buku.pengarang, buku.jumlah);
}

printf("\n");
fclose(f_struktur);

return 0;
}

PROGRAM 7
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PANJANG 256

int main() {
FILE *f_teks;
char string[PANJANG];
char namafile[65];

printf("PROGRAM UNTUK MELIHAT ISI FILE TEKS\n\n");


printf("Masukkan nama file: ");
fgets(namafile, sizeof(namafile), stdin); // Use fgets to read input

namafile[strcspn(namafile, "\n")] = 0; // Remove newline character

printf("\nIsi file %s adalah sbb:\n", namafile);

if ((f_teks = fopen(namafile, "rt")) == NULL) {


printf("File gagal dibuka\n");
exit(1);
}

while (fgets(string, sizeof(string), f_teks) != NULL) {


printf("%s", string);
}

fclose(f_teks);

return 0;
}

PROGRAM 8
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h> // For toupper

#define PANJANG 256

int main() {
FILE *pf_input, *pf_output;
char string[PANJANG];
char namafile_inp[65], namafile_out[65];

printf("PROGRAM UNTUK MENYALIN ISI FILE TEKS\n\n");


printf("Masukkan nama file input: ");
fgets(namafile_inp, sizeof(namafile_inp), stdin);
strtok(namafile_inp, "\n"); // Remove the newline character from the input

printf("Masukkan nama file output: ");


fgets(namafile_out, sizeof(namafile_out), stdin);
strtok(namafile_out, "\n"); // Remove the newline character from the input

if ((pf_input = fopen(namafile_inp, "r")) == NULL) {


printf("File input gagal dibuka\n");
exit(1);
}

if ((pf_output = fopen(namafile_out, "w")) == NULL) {


printf("File output gagal dibuka\n");
exit(1);
}

printf("Isi file %s adalah:\n", namafile_inp);


while (fgets(string, sizeof(string), pf_input) != NULL) {
printf("%s", string);
for (int i = 0; string[i] != '\0'; i++) {
string[i] = toupper(string[i]);
}
fputs(string, pf_output);
}

fclose(pf_input);
fclose(pf_output);

if ((pf_output = fopen(namafile_out, "r")) == NULL) {


printf("File output gagal dibuka\n");
exit(1);
}

printf("\nIsi dari file %s adalah:\n", namafile_out);


while (fgets(string, sizeof(string), pf_output) != NULL) {
printf("%s", string);
}

fclose(pf_output);

return 0;
}
PROGRAM 9
#include <stdio.h>
#include <stdlib.h>

int main() {
struct {
char judul[30];
char pengarang[30];
int jumlah;
} buku;

FILE *pf;
char jawab;
int i, no_record, sudah_benar;
long int offset_byte;

// Open file
if ((pf = fopen("DAFBUKU.DAT", "rb")) == NULL) {
printf("File tidak dapat dibuka!\n");
exit(1);
}

do {
i = 1;
printf("Nomor record dr data yg mau ditampilkan: ");
scanf("%d", &no_record);

offset_byte = (no_record - 1) * sizeof(buku);


fseek(pf, offset_byte, SEEK_SET);

if (fread(&buku, sizeof(buku), 1, pf) == 0) {


printf("Nomor record tdk dikenali!\n");
} else {
printf("%-2s %-30s %-30s %s\n", "No", "Judul Buku", "Nama Pengarang", "Jumlah");
printf("%2d %-30s %-30s %d\n", i++, buku.judul, buku.pengarang, buku.jumlah);
}

printf("Mau mencoba lagi (Y/T)? ");


do {
jawab = getchar();
} while (jawab != 'Y' && jawab != 'y' && jawab != 'T' && jawab != 't');
} while (jawab == 'y' || jawab == 'Y');

printf("\n");
fclose(pf);

return 0;
}

PROGRAM 10
#include <stdio.h>
#include <stdlib.h>

#define SATU_RECORD 1
int main() {
struct {
char judul[30];
char pengarang[30];
int jumlah;
} buku;

FILE *pf;
char jawab;
int no_record, sudah_benar, hasil_baca;
long int offset_byte;

// Open the file that contains book data


if ((pf = fopen("DAFBUKU.DAT", "rb+")) == NULL) {
printf("File tidak dapat dibuka!\n");
exit(1);
}

do {
printf("Nomor record dari data yg mau diubah : ");
scanf("%d", &no_record);

// Set the file pointer to the specified record


offset_byte = (no_record - 1) * sizeof(buku);
fseek(pf, offset_byte, SEEK_SET);

// Read the record pointed by the file pointer


hasil_baca = fread(&buku, sizeof(buku), SATU_RECORD, pf);

if (hasil_baca == 0) {
printf("Nomor record tdk dikenali!\n");
} else {
printf("\n%-30s %-30s %s\n\n", "Judul Buku", "Nama Pengarang", "Jumlah");
printf("%-30s %-30s %d\n", buku.judul, buku.pengarang, buku.jumlah);
printf("Jumlah buku tsb kini= ");
scanf("%d", &buku.jumlah);

// Set the file pointer back to the previous position


fseek(pf, offset_byte, SEEK_SET);

// Rewrite the record


fwrite(&buku, sizeof(buku), SATU_RECORD, pf);
}

printf("\nMau mengubah lagi (Y/T)? ");


do {
jawab = getchar(); // Read the answer from the keyboard
} while (jawab != 'Y' && jawab != 'y' && jawab != 'T' && jawab != 't');
} while (jawab == 'Y' || jawab == 'y');

fclose(pf); // Close the file


return 0;
}

PROGRAM 11

Anda mungkin juga menyukai