{"id":614,"date":"2022-03-28T08:43:26","date_gmt":"2022-03-28T08:43:26","guid":{"rendered":"https:\/\/ekomartantoh.net\/artikel\/?p=614"},"modified":"2022-03-30T01:13:30","modified_gmt":"2022-03-30T01:13:30","slug":"belajar-c-file","status":"publish","type":"post","link":"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/","title":{"rendered":"Belajar C++ File"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ekomartantoh.net\/artikel\/wp-content\/uploads\/2022\/03\/image-edited.png\" alt=\"Belajar C++ File\" class=\"wp-image-646\" width=\"616\" height=\"346\" srcset=\"https:\/\/ekomartantoh.net\/artikel\/wp-content\/uploads\/2022\/03\/image-edited.png 1024w, https:\/\/ekomartantoh.net\/artikel\/wp-content\/uploads\/2022\/03\/image-edited-300x169.png 300w, https:\/\/ekomartantoh.net\/artikel\/wp-content\/uploads\/2022\/03\/image-edited-768x432.png 768w\" sizes=\"auto, (max-width: 616px) 100vw, 616px\" \/><\/figure><\/div>\n\n\n\n<p>Belajar file pada c++<\/p>\n\n\n\n<p>Dengan menggunakan librari fstream memungkinkan kita bekerja dengan file. Jadi kita dapat gabungkan dua librari iostream dan fstream. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Terdapat 3 kelas pada librari fstream yang digunakan untuk membuat, menulis atau membaca file:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>ofstream, untuk membuat dan menulis ke file<\/li><li>ifstream, untuk membaca dari file<\/li><li>fstream, kombinasi ofstream dan ifstream, membuat, membacar dan menulis ke file<\/li><\/ol>\n\n\n\n<p>Untuk membuat file gunakan kelas ofstream atau fstream, dan tentukan nama file. Untuk menulis ke file gunakan operator penyisipan (&lt;&lt;).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Contoh:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;iostream&gt;\n#include &lt;fstream&gt;\n#include &lt;string&gt;\nusing namespace std;\n\nint main () {\n  \/\/ Create a text file\n  ofstream MyWriteFile(&quot;filename.txt&quot;);\n\n  \/\/ Write to the file\n  MyWriteFile &lt;&lt; &quot;Files can be tricky, but it is fun enough!&quot;;\n \n  \/\/ Close the file\n  MyWriteFile.close();\n\n  \/\/ Create a text string, which is used to output the text file\n  string myText;\n\n  \/\/ Read from the text file\n  ifstream MyReadFile(&quot;filename.txt&quot;);\n\n  \/\/ Use a while loop together with the getline() function to read the file line by line\n  while (getline (MyReadFile, myText)) {\n    \/\/ Output the text from the file\n    cout &lt;&lt; myText;\n  }\n\n  \/\/ Close the file\n  MyReadFile.close();\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Latihan Belajar C++ File Aplikasi Sederhana<\/h2>\n\n\n\n<p>Dalam latihan ini akan dibuat sebuah program sederhana dimana memiliki kemampuan:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Membuat file txt sebagai database data barang<\/li><li>Mengimput data barang<\/li><li>Menampilkan data barang<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Class, Attribut dan Method:<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>Class Barang<\/li><li>Atribut:<ul><li>kodeBarang<\/li><li>namaBarang<\/li><li>hargaBarang<\/li><\/ul><\/li><li>Method:<ul><li>simpanBarang<\/li><li>tampil<\/li><\/ul><\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Berikut video tutorialnya:<\/h3>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"OOP C++ Bekerja dengan File\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/3XHSD84ewsM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><figcaption>Video Tutorial OOP C++ Bekerja dengan File<\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Berikut Script Programnya:<\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;iostream&gt;\n#include &lt;string&gt;\n#include &lt;fstream&gt;\nusing namespace std;\n\nclass Barang{\npublic:\n    string kodeBarang;\n    string namaBarang;\n    float hargaBarang;\n\n    void simpanBarang(string x, string y, float z){\n        kodeBarang = x;\n        namaBarang = y;\n        hargaBarang = z;\n\n        ofstream myWriteFile(&quot;filename.txt&quot;, ios::app);\n        myWriteFile &lt;&lt; &quot;\\n&quot; &lt;&lt; kodeBarang &lt;&lt; &quot;\\t&quot; &lt;&lt; namaBarang &lt;&lt; &quot;\\t&quot; &lt;&lt; hargaBarang;\n        myWriteFile.close();\n    }\n\n    void tampil(){\n        string myText;\n        ifstream myReadFile(&quot;filename.txt&quot;);\n        while (getline(myReadFile, myText)){\n            cout &lt;&lt; myText &lt;&lt; endl;\n        }\n        myReadFile.close();\n    }\n};\n\nint main()\n{\n    string kode, nama;\n    float harga;\n\n    cout &lt;&lt; &quot;INPUT DATA BARANG \\n&quot;;\n    cout &lt;&lt; &quot;Kode Barang: &quot;;\n    cin &gt;&gt; kode;\n    cout &lt;&lt; &quot;Nama Barang: &quot;;\n    cin &gt;&gt; nama;\n    cout &lt;&lt; &quot;Harga: &quot;;\n    cin &gt;&gt; harga;\n\n    Barang dataBarang;\n\n    dataBarang.simpanBarang(kode, nama, harga);\n    dataBarang.tampil();\n\n    return 0;\n}\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">Baca Juga:<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/ekomartantoh.net\/artikel\/2021\/10\/27\/sistem-operasi-berbasis-gui-banyak-dikembangkan-demikian-pula-klasifikasinya\/\">Sistem Operasi Berbasis GUI<\/a><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Referensi:<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.w3schools.com\/cpp\/cpp_oop.asp\" target=\"_blank\" rel=\"noreferrer noopener\">C++ OOP (Object-Oriented Programming)<\/a><\/li><li><a href=\"https:\/\/www.youtube.com\/watch?v=ZYBkuY1eiZ4&amp;list=PLZS-MHyEIRo7-RC_-hkL9gu0_ofABw862\" target=\"_blank\" rel=\"noreferrer noopener\">(2) Belajar C++ [OOP] &#8211; 01 &#8211; Pendahuluan, apa itu object oriented programming &#8211; YouTube<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Belajar file pada c++ Dengan menggunakan librari fstream memungkinkan kita bekerja dengan file. Jadi kita dapat gabungkan dua librari iostream dan fstream. Terdapat 3 kelas pada librari fstream yang digunakan untuk membuat, menulis atau membaca file: ofstream, untuk membuat dan menulis ke file ifstream, untuk membaca dari file fstream, kombinasi ofstream dan ifstream, membuat, membacar [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[64],"tags":[],"class_list":["post-614","post","type-post","status-publish","format-standard","hentry","category-object-oriented-programming-oop"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Belajar C++ File - eko&#039;s Article<\/title>\n<meta name=\"description\" content=\"Belajar file pada c++ dengan menggunakan librari fstream memungkinkan kita bekerja dengan file dan dikombinasikan dengan librari iostream.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/\" \/>\n<meta property=\"og:locale\" content=\"id_ID\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Belajar C++ File - eko&#039;s Article\" \/>\n<meta property=\"og:description\" content=\"Belajar file pada c++ dengan menggunakan librari fstream memungkinkan kita bekerja dengan file dan dikombinasikan dengan librari iostream.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/\" \/>\n<meta property=\"og:site_name\" content=\"eko&#039;s Article\" \/>\n<meta property=\"article:published_time\" content=\"2022-03-28T08:43:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-30T01:13:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ekomartantoh.net\/artikel\/wp-content\/uploads\/2022\/03\/image-edited.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Ditulis oleh\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimasi waktu membaca\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 menit\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\/\/ekomartantoh.net\/artikel\/#\/schema\/person\/60372cd08272aa24bdef56303024243c\"},\"headline\":\"Belajar C++ File\",\"datePublished\":\"2022-03-28T08:43:26+00:00\",\"dateModified\":\"2022-03-30T01:13:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/\"},\"wordCount\":168,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/ekomartantoh.net\/artikel\/#\/schema\/person\/60372cd08272aa24bdef56303024243c\"},\"articleSection\":[\"Object Oriented Programming (OOP)\"],\"inLanguage\":\"id\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/\",\"url\":\"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/\",\"name\":\"Belajar C++ File - eko's Article\",\"isPartOf\":{\"@id\":\"https:\/\/ekomartantoh.net\/artikel\/#website\"},\"datePublished\":\"2022-03-28T08:43:26+00:00\",\"dateModified\":\"2022-03-30T01:13:30+00:00\",\"description\":\"Belajar file pada c++ dengan menggunakan librari fstream memungkinkan kita bekerja dengan file dan dikombinasikan dengan librari iostream.\",\"breadcrumb\":{\"@id\":\"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/#breadcrumb\"},\"inLanguage\":\"id\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ekomartantoh.net\/artikel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Belajar C++ File\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ekomartantoh.net\/artikel\/#website\",\"url\":\"https:\/\/ekomartantoh.net\/artikel\/\",\"name\":\"eko's Article\",\"description\":\"Artikel-artikel artikel bermanfaat bagi pengunung, dari berbagai tema, baik Tekonologi, Edukasi, Politik, Ekonomi, Religi dan lain-lain\",\"publisher\":{\"@id\":\"https:\/\/ekomartantoh.net\/artikel\/#\/schema\/person\/60372cd08272aa24bdef56303024243c\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ekomartantoh.net\/artikel\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"id\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/ekomartantoh.net\/artikel\/#\/schema\/person\/60372cd08272aa24bdef56303024243c\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"id\",\"@id\":\"https:\/\/ekomartantoh.net\/artikel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/70ef551982f01009a8cc91f680dccfb897d7c600c65450567cfe6f2d18547c74?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/70ef551982f01009a8cc91f680dccfb897d7c600c65450567cfe6f2d18547c74?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"logo\":{\"@id\":\"https:\/\/ekomartantoh.net\/artikel\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/ekomartantoh.net\/artikel\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Belajar C++ File - eko's Article","description":"Belajar file pada c++ dengan menggunakan librari fstream memungkinkan kita bekerja dengan file dan dikombinasikan dengan librari iostream.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/","og_locale":"id_ID","og_type":"article","og_title":"Belajar C++ File - eko's Article","og_description":"Belajar file pada c++ dengan menggunakan librari fstream memungkinkan kita bekerja dengan file dan dikombinasikan dengan librari iostream.","og_url":"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/","og_site_name":"eko's Article","article_published_time":"2022-03-28T08:43:26+00:00","article_modified_time":"2022-03-30T01:13:30+00:00","og_image":[{"url":"https:\/\/ekomartantoh.net\/artikel\/wp-content\/uploads\/2022\/03\/image-edited.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Ditulis oleh":"admin","Estimasi waktu membaca":"3 menit"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/#article","isPartOf":{"@id":"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/"},"author":{"name":"admin","@id":"https:\/\/ekomartantoh.net\/artikel\/#\/schema\/person\/60372cd08272aa24bdef56303024243c"},"headline":"Belajar C++ File","datePublished":"2022-03-28T08:43:26+00:00","dateModified":"2022-03-30T01:13:30+00:00","mainEntityOfPage":{"@id":"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/"},"wordCount":168,"commentCount":0,"publisher":{"@id":"https:\/\/ekomartantoh.net\/artikel\/#\/schema\/person\/60372cd08272aa24bdef56303024243c"},"articleSection":["Object Oriented Programming (OOP)"],"inLanguage":"id","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/","url":"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/","name":"Belajar C++ File - eko's Article","isPartOf":{"@id":"https:\/\/ekomartantoh.net\/artikel\/#website"},"datePublished":"2022-03-28T08:43:26+00:00","dateModified":"2022-03-30T01:13:30+00:00","description":"Belajar file pada c++ dengan menggunakan librari fstream memungkinkan kita bekerja dengan file dan dikombinasikan dengan librari iostream.","breadcrumb":{"@id":"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/#breadcrumb"},"inLanguage":"id","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/ekomartantoh.net\/artikel\/2022\/03\/28\/belajar-c-file\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ekomartantoh.net\/artikel\/"},{"@type":"ListItem","position":2,"name":"Belajar C++ File"}]},{"@type":"WebSite","@id":"https:\/\/ekomartantoh.net\/artikel\/#website","url":"https:\/\/ekomartantoh.net\/artikel\/","name":"eko's Article","description":"Artikel-artikel artikel bermanfaat bagi pengunung, dari berbagai tema, baik Tekonologi, Edukasi, Politik, Ekonomi, Religi dan lain-lain","publisher":{"@id":"https:\/\/ekomartantoh.net\/artikel\/#\/schema\/person\/60372cd08272aa24bdef56303024243c"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ekomartantoh.net\/artikel\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"id"},{"@type":["Person","Organization"],"@id":"https:\/\/ekomartantoh.net\/artikel\/#\/schema\/person\/60372cd08272aa24bdef56303024243c","name":"admin","image":{"@type":"ImageObject","inLanguage":"id","@id":"https:\/\/ekomartantoh.net\/artikel\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/70ef551982f01009a8cc91f680dccfb897d7c600c65450567cfe6f2d18547c74?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/70ef551982f01009a8cc91f680dccfb897d7c600c65450567cfe6f2d18547c74?s=96&d=mm&r=g","caption":"admin"},"logo":{"@id":"https:\/\/ekomartantoh.net\/artikel\/#\/schema\/person\/image\/"},"url":"https:\/\/ekomartantoh.net\/artikel\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/ekomartantoh.net\/artikel\/wp-json\/wp\/v2\/posts\/614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekomartantoh.net\/artikel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekomartantoh.net\/artikel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekomartantoh.net\/artikel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ekomartantoh.net\/artikel\/wp-json\/wp\/v2\/comments?post=614"}],"version-history":[{"count":37,"href":"https:\/\/ekomartantoh.net\/artikel\/wp-json\/wp\/v2\/posts\/614\/revisions"}],"predecessor-version":[{"id":658,"href":"https:\/\/ekomartantoh.net\/artikel\/wp-json\/wp\/v2\/posts\/614\/revisions\/658"}],"wp:attachment":[{"href":"https:\/\/ekomartantoh.net\/artikel\/wp-json\/wp\/v2\/media?parent=614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekomartantoh.net\/artikel\/wp-json\/wp\/v2\/categories?post=614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekomartantoh.net\/artikel\/wp-json\/wp\/v2\/tags?post=614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}