15.07.2013 Views

“bukuAjar” — 2011/9/22 — 13:39 — page i — #1 - cs.unsyiah.ac.id ...

“bukuAjar” — 2011/9/22 — 13:39 — page i — #1 - cs.unsyiah.ac.id ...

“bukuAjar” — 2011/9/22 — 13:39 — page i — #1 - cs.unsyiah.ac.id ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

✐<br />

✐<br />

✐<br />

✐<br />

<strong>“bukuAjar”</strong> <strong>—</strong> <strong>2011</strong>/9/<strong>22</strong> <strong>—</strong> <strong>13</strong>:<strong>39</strong> <strong>—</strong> <strong>page</strong> 75 <strong>—</strong> #89<br />

11.4. Beberapa Contoh Manipulasi File 75<br />

Satu-Satu Karakter.<br />

#include <br />

#include <br />

#include <br />

int main (vo<strong>id</strong>){<br />

int c;<br />

FILE *fp;<br />

char *filename = "data.txt";<br />

}<br />

if((fp = fopen(filename, "r"))==NULL){<br />

printf("Error dalam mengakses file...\n");<br />

return EXIT_FAILURE;<br />

}<br />

while((c=fgetc(fp)) != EOF){<br />

putchar(toupper(c));<br />

}<br />

fclose(fp);<br />

return EXIT_SUCCESS;<br />

Contoh 2. Buka File, Jumlahkan, dan Tulis Penjumlahan ke<br />

dalam File.<br />

#include <br />

#include <br />

int main (vo<strong>id</strong>){<br />

int a, sum = 0;<br />

FILE *ifp, *ofp;<br />

ifp = fopen("input_file.txt", "r");<br />

ofp = fopen("output_file.txt", "w");<br />

}<br />

while (fscanf(ifp,"%d",&a)==1){<br />

sum += a;<br />

}<br />

fprintf(ofp, "Jumlah = %d\n",sum);<br />

fclose(ifp);<br />

fclose(ofp);<br />

return EXIT_SUCCESS;<br />

✐<br />

✐<br />

✐<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!