{"id":663,"date":"2022-04-11T02:32:06","date_gmt":"2022-04-11T02:32:06","guid":{"rendered":"https:\/\/ekomartantoh.net\/artikel\/?p=663"},"modified":"2022-04-11T02:32:11","modified_gmt":"2022-04-11T02:32:11","slug":"c-operasi-file-crud","status":"publish","type":"post","link":"https:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/","title":{"rendered":"C++ Operasi File CRUD"},"content":{"rendered":"\n<p>Dibawah ini code program untuk operasi file CRUD (Create, Read, Update, dan Delete) dan juga ada pencariannya.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;iostream&gt; \/\/simple input dan output\n#include &lt;fstream&gt; \/\/Membuat, menulis, dan membaca file\n#include &lt;iomanip&gt;\n#include &lt;conio.h&gt;\n\nusing namespace std;\n\nclass item{\n    \/\/Data members\nprivate:\n    int code;\n    char name&#91;15];\n    int qty;\n    \/\/member function\npublic:\n    void put_item(void);\n    void get_item(void);\n    int get_code (void){\n        return code;\n    };\n\n    int update_qty(int num){\n        qty = qty-num;\n        return qty;\n    }\n\n    void inputCode(){\n    while (!(cin &gt;&gt; code))\n        {\n            cout &lt;&lt; &quot;ERROR: a number must be entered: &quot;;\n            cin.clear();\n            cin.ignore(132, '\\n');\n        }\n}\n\n};\n\n\/\/Member fungtion class item di luar class\n\nvoid item::get_item(void){\n        cout &lt;&lt; &quot;Masukkan Kode Item: &quot;;\n        inputCode();\n        \/\/fflush(stdin);\n        cout &lt;&lt; &quot;Masukkan Nama Item: &quot;; cin &gt;&gt; name;\n        cout &lt;&lt; &quot;Masukkan Qty: &quot;; cin &gt;&gt; qty;\n    }\n\nvoid item::put_item(void){\n    cout&lt;&lt;setw(6)&lt;&lt;code&lt;&lt;setw(15)&lt;&lt;name&lt;&lt;setw(6)&lt;&lt;qty&lt;&lt;endl; \/\/setup dengan lebar field membutuhkan library iomanip\n}\n\n\/\/Fuction Prototype\nvoid addRecord(void);\nvoid showAll(void);\nvoid showRecord(void);\nvoid deleteRecord(void);\nvoid modifRecord(void);\n\n\/\/Global Declaration\nitem it;        \/\/membuat object item\nfstream file;   \/\/membuat object fstream\n\nint main()\n{\n    int option;\n    while(1){\n        system(&quot;cls&quot;);\n        cout&lt;&lt;&quot;***MENU***&quot;&lt;&lt;endl;\n        cout&lt;&lt;&quot;1. Tambah Record Baru&quot;&lt;&lt;endl;\n        cout&lt;&lt;&quot;2. Tampil Semua Data&quot;&lt;&lt;endl;\n        cout&lt;&lt;&quot;3. Cari Record Tertentu&quot;&lt;&lt;endl;\n        cout&lt;&lt;&quot;4. Hapus Record&quot;&lt;&lt;endl;\n        cout&lt;&lt;&quot;5. Ubah Record&quot;&lt;&lt;endl;\n        cout&lt;&lt;&quot;6. Keluar&quot;&lt;&lt;endl;\n        cout&lt;&lt;&quot;Masukkan nomor opsi: &quot;; cin&gt;&gt;option;\n        switch(option){\n            case 1:{\n                addRecord();\n                cout&lt;&lt;&quot;Tekan sembarang untuk kembali ke menu ...&quot;;\n                getch(); \/\/butuh library conio.h\n                break;\n            }\n            case 2:{\n                cout&lt;&lt;&quot;-----------------------------------------\\n&quot;;\n                cout&lt;&lt;setw(6)&lt;&lt;&quot;Kode&quot;&lt;&lt;setw(15)&lt;&lt;&quot;Nama&quot;&lt;&lt;setw(6)&lt;&lt;&quot;Qty&quot;&lt;&lt;endl;\n                cout&lt;&lt;&quot;-----------------------------------------\\n&quot;;\n                showAll();\n                cout&lt;&lt;&quot;-----------------------------------------\\n&quot;;\n                cout&lt;&lt;&quot;Tekan sembarang untuk kembali ke menu ...&quot;;\n                getch();\n                break;\n            }\n            case 3:{\n                showRecord();\n                cout&lt;&lt;&quot;Tekan sembarang untuk kembali ke menu ...&quot;;\n                getch();\n                break;\n            }\n            case 4:{\n                deleteRecord();\n                cout&lt;&lt;&quot;Tekan sembarang untuk kembali ke menu ...&quot;;\n                getch();\n                break;\n            }\n            case 5:{\n                modifRecord();\n                cout&lt;&lt;&quot;Tekan sembarang untuk kembali ke menu ...&quot;;\n                getch();\n                break;\n            }\n            case 6:{\n                exit(0);\n            }\n            default:{\n                cout&lt;&lt;&quot;Opsi pilihan tidak ada, Tekan sembarang untuk kembali ke menu ...&quot;;\n                getch();\n                break;\n            }\n        }\n    }\n\n    return 0;\n}\n\nvoid addRecord(){\n    char ch = 'y';\n    file.open(&quot;stock.dat&quot;,ios::app|ios::binary);\n    while(ch=='y'||ch=='Y'){\n        it.get_item();\n        \/\/write object into file\n        file.write((char*)&amp;it, sizeof(it));\n        cout&lt;&lt;&quot;Tambah Record lagi ... (y\/n)?&quot;;\n        cin&gt;&gt;ch;\n    }\n    file.close();\n}\n\nvoid showAll(){\n    file.open(&quot;stock.dat&quot;, ios::in|ios::binary);\n    if(!file)\n    {\n        cout &lt;&lt;&quot;File not Found&quot;;\n        exit(0);\n    }else{\n        file.read((char*)&amp;it, sizeof(it));\n        while (!file.eof()){\n            it.put_item();\n            file.read((char*)&amp;it, sizeof(it));\n        }\n    }\n    file.close();\n}\n\nvoid showRecord(void){\n    int no, flag = 0;\n    file.open(&quot;stock.dat&quot;, ios::in|ios::binary);\n    if (!file){ \/\/Jika file tidak bisa dibuka\n        cout&lt;&lt;&quot;file tidak ditemukan&quot;;\n        exit(0);\n    }else{\n        cout&lt;&lt;&quot;Masukkan code yang dicari: &quot;;\n        cin&gt;&gt;no;\n        \/\/Baca record dari file dan dimasukkan ke object\n        while(!file.eof()){ \/\/ketika akhir dari file\n            if(no==it.get_code()){\n                flag=1;\n                cout&lt;&lt;&quot;-----------------------------------------\\n&quot;;\n                cout&lt;&lt;setw(6)&lt;&lt;&quot;Kode&quot;&lt;&lt;setw(15)&lt;&lt;&quot;Nama&quot;&lt;&lt;setw(6)&lt;&lt;&quot;Qty&quot;&lt;&lt;endl;\n                cout&lt;&lt;&quot;-----------------------------------------\\n&quot;;\n                it.put_item();\n                cout&lt;&lt;&quot;-----------------------------------------\\n&quot;;\n                break;\n            }\n            file.read((char*)&amp;it, sizeof(it));\n        }\n        if (flag==0){\n            cout&lt;&lt;&quot;Item tidak ditemukan ....\\n&quot;;\n        }\n    }\n    file.close();\n}\n\nvoid deleteRecord(void){\n    int no;\n    cout&lt;&lt;&quot;Masukkan kode item untuk dihapus: &quot;  ;\n    cin&gt;&gt;no;\n    ofstream file2;  \/\/stream object\n    \/\/open new.dat file for write operation\n    file2.open(&quot;new.dat&quot;,ios::binary);\n    file.open(&quot;stock.dat&quot;, ios::in|ios::binary);\n    if(!file)    \/\/if open file fails\n    {\n        cout&lt;&lt;&quot;file not found&quot;;\n        exit(0);\n    }else{\n        \/\/read record form stock.dat file into object\n        file.read((char*)&amp;it, sizeof(it));\n        while(!file.eof()){\n            if(no != it.get_code()){\n                file2.write((char*)&amp;it, sizeof(it));\n            }\n            file.read((char*)&amp;it, sizeof(it));\n        }\n    }\n    file2.close();\n    file.close();\n    remove(&quot;stock.dat&quot;);\n    rename(&quot;new.dat&quot;,&quot;stock.dat&quot;);\n}\n\nvoid modifRecord(void){\n    int no, num;\n    cout&lt;&lt;&quot;Masukkan kode item untuk dirubah: &quot;; cin&gt;&gt;no;\n    cout&lt;&lt;&quot;Masukkan Jumlah item yang akan di keluarkan: &quot;; cin&gt;&gt;num;\n    file.open(&quot;stock.dat&quot;, ios::in|ios::out|ios::binary);\n    if(!file){\n        cout&lt;&lt;&quot;File not found&quot;;\n        exit(0);\n    }\n    while(file.read((char*)&amp;it, sizeof(it))){\n        if(it.get_code()==no){\n            it.update_qty(num);\n            int pos = sizeof(it);\n            file.seekp(-pos, ios::cur);\n            file.write((char*)&amp;it, sizeof(it));\n        }\n    }\n    file.close();\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Dibawah ini code program untuk operasi file CRUD (Create, Read, Update, dan Delete) dan juga ada pencariannya.<\/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-663","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>C++ Operasi File CRUD - eko&#039;s Article<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/\" \/>\n<meta property=\"og:locale\" content=\"id_ID\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Operasi File CRUD - eko&#039;s Article\" \/>\n<meta property=\"og:description\" content=\"Dibawah ini code program untuk operasi file CRUD (Create, Read, Update, dan Delete) dan juga ada pencariannya.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/\" \/>\n<meta property=\"og:site_name\" content=\"eko&#039;s Article\" \/>\n<meta property=\"article:published_time\" content=\"2022-04-11T02:32:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-11T02:32:11+00:00\" \/>\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\":\"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\/\/ekomartantoh.net\/artikel\/#\/schema\/person\/60372cd08272aa24bdef56303024243c\"},\"headline\":\"C++ Operasi File CRUD\",\"datePublished\":\"2022-04-11T02:32:06+00:00\",\"dateModified\":\"2022-04-11T02:32:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/\"},\"wordCount\":21,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/ekomartantoh.net\/artikel\/#\/schema\/person\/60372cd08272aa24bdef56303024243c\"},\"articleSection\":[\"Object Oriented Programming (OOP)\"],\"inLanguage\":\"id\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/\",\"url\":\"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/\",\"name\":\"C++ Operasi File CRUD - eko's Article\",\"isPartOf\":{\"@id\":\"https:\/\/ekomartantoh.net\/artikel\/#website\"},\"datePublished\":\"2022-04-11T02:32:06+00:00\",\"dateModified\":\"2022-04-11T02:32:11+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/#breadcrumb\"},\"inLanguage\":\"id\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ekomartantoh.net\/artikel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Operasi File CRUD\"}]},{\"@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":"C++ Operasi File CRUD - eko's Article","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":"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/","og_locale":"id_ID","og_type":"article","og_title":"C++ Operasi File CRUD - eko's Article","og_description":"Dibawah ini code program untuk operasi file CRUD (Create, Read, Update, dan Delete) dan juga ada pencariannya.","og_url":"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/","og_site_name":"eko's Article","article_published_time":"2022-04-11T02:32:06+00:00","article_modified_time":"2022-04-11T02:32:11+00:00","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":"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/#article","isPartOf":{"@id":"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/"},"author":{"name":"admin","@id":"https:\/\/ekomartantoh.net\/artikel\/#\/schema\/person\/60372cd08272aa24bdef56303024243c"},"headline":"C++ Operasi File CRUD","datePublished":"2022-04-11T02:32:06+00:00","dateModified":"2022-04-11T02:32:11+00:00","mainEntityOfPage":{"@id":"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/"},"wordCount":21,"commentCount":0,"publisher":{"@id":"https:\/\/ekomartantoh.net\/artikel\/#\/schema\/person\/60372cd08272aa24bdef56303024243c"},"articleSection":["Object Oriented Programming (OOP)"],"inLanguage":"id","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/","url":"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/","name":"C++ Operasi File CRUD - eko's Article","isPartOf":{"@id":"https:\/\/ekomartantoh.net\/artikel\/#website"},"datePublished":"2022-04-11T02:32:06+00:00","dateModified":"2022-04-11T02:32:11+00:00","breadcrumb":{"@id":"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/#breadcrumb"},"inLanguage":"id","potentialAction":[{"@type":"ReadAction","target":["http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/ekomartantoh.net\/artikel\/2022\/04\/11\/c-operasi-file-crud\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ekomartantoh.net\/artikel\/"},{"@type":"ListItem","position":2,"name":"C++ Operasi File CRUD"}]},{"@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\/663","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=663"}],"version-history":[{"count":1,"href":"https:\/\/ekomartantoh.net\/artikel\/wp-json\/wp\/v2\/posts\/663\/revisions"}],"predecessor-version":[{"id":664,"href":"https:\/\/ekomartantoh.net\/artikel\/wp-json\/wp\/v2\/posts\/663\/revisions\/664"}],"wp:attachment":[{"href":"https:\/\/ekomartantoh.net\/artikel\/wp-json\/wp\/v2\/media?parent=663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekomartantoh.net\/artikel\/wp-json\/wp\/v2\/categories?post=663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekomartantoh.net\/artikel\/wp-json\/wp\/v2\/tags?post=663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}