{"id":408,"date":"2023-02-25T10:53:25","date_gmt":"2023-02-25T02:53:25","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=408"},"modified":"2023-04-29T21:36:25","modified_gmt":"2023-04-29T13:36:25","slug":"android-ndk-basic-c-file-io","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/android-ndk-basic-c-file-io\/","title":{"rendered":"Android NDK\u57fa\u78408\uff1aC_\u6587\u4ef6IO"},"content":{"rendered":"<h2>\u6587\u4ef6IO<\/h2>\n<pre><code class=\"language-c\">#define _CRT_SECURE_NO_WARNINGS\n#include &lt;stdlib.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n\n\/\/\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\nvoid main() {\n    char *path = &quot;E:\\\\ndk\\\\files\\\\friends.txt&quot;;\n    \/\/\u6253\u5f00\n    FILE *fp = fopen(path, &quot;r&quot;);\n    if (fp == NULL) {\n        printf(&quot;\u6587\u4ef6\u6253\u5f00\u5931\u8d25...&quot;);\n        return;\n    }\n    \/\/\u8bfb\u53d6\n    char buff[50]; \/\/\u7f13\u51b2\n    while (fgets(buff, 50, fp)) { \/\/\u6587\u4ef6\u4e2d\u8bfb\u53d6\u4e00\u5b57\u7b26\u4e32\n        printf(&quot;%s&quot;, buff);\n    }\n    \/\/\u5173\u95ed\n    fclose(fp);\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<p><!-- more --><\/p>\n<pre><code class=\"language-c\">\/\/\u5199\u5165\u6587\u672c\u6587\u4ef6\nvoid main() {\n    char *path = &quot;E:\\\\ndk\\\\files\\\\friends_new.txt&quot;;\n    \/\/\u6253\u5f00\n    FILE *fp = fopen(path, &quot;w&quot;);\n    char *text = &quot;yezhpu@androidios.cc, Android &amp; iOS \u9ad8\u7ea7\u5f00\u53d1\u793e\u533a&quot;;\n    fputs(text, fp);\n\n    \/\/\u5173\u95ed\u6d41\n    fclose(fp);\n    system(&quot;pause&quot;);\n}\n\n\/\/\u8ba1\u7b97\u673a\u7684\u6587\u4ef6\u5b58\u50a8\u5728\u7269\u7406\u4e0a\u90fd\u662f\u4e8c\u8fdb\u5236\n\/\/\u6587\u672c\u6587\u4ef6\u548c\u4e8c\u8fdb\u5236\u4e4b\u5206\uff0c\u5176\u5b9e\u662f\u4e00\u4e2a\u903b\u8f91\u4e4b\u5206\n\/\/C\u8bfb\u5199\u6587\u672c\u6587\u4ef6\u4e0e\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u5dee\u522b\u4ec5\u4ec5\u4f53\u73b0\u5728\u56de\u8f66\u6362\u884c\u7b26\n\/\/\u5199\u6587\u672c\u65f6\uff0c\u6bcf\u9047\u5230\u4e00\u4e2a&#039;\\n&#039;\uff0c\u4f1a\u5c06\u5176\u8f6c\u6362\u6210&#039;\\r\\n&#039;(\u56de\u8f66\u6362\u884c)\n\/\/\u8bfb\u6587\u672c\u65f6\uff0c\u6bcf\u9047\u5230\u4e00\u4e2a&#039;\\r\\n&#039;\uff0c\u4f1a\u5c06\u5176\u8f6c\u6362\u6210&#039;\\n&#039;<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/\u6587\u4ef6\u590d\u5236\nvoid main() {\n    char *read_path = &quot;E:\\\\ndk\\\\files\\\\liuyan.png&quot;;\n    char *write_path = &quot;E:\\\\ndk\\\\files\\\\liuyan_new.png&quot;;\n    \/\/\u8bfb\u7684\u6587\u4ef6 b\u5b57\u7b26\u8868\u793a\u64cd\u4f5c\u4e8c\u8fdb\u5236\u6587\u4ef6binary\n    FILE *read_fp = fopen(read_path, &quot;rb&quot;);\n    \/\/\u5199\u7684\u6587\u4ef6\n    FILE *write_fp = fopen(write_path, &quot;wb&quot;);\n\n    \/\/\u590d\u5236\n    int buff[50]; \/\/\u7f13\u51b2\u533a\u57df\n    int len = 0;  \/\/\u6bcf\u6b21\u8bfb\u5230\u7684\u6570\u636e\u957f\u5ea6\n    while ((len = fread(buff, sizeof(int), 50, read_fp)) != 0){\n        \/\/\u5c06\u8bfb\u5230\u7684\u5185\u5bb9\u5199\u5165\u65b0\u7684\u6587\u4ef6\n        fwrite(buff, sizeof(int), len, write_fp);\n    }\n    \/\/\u5173\u95ed\u6d41\n    fclose(read_fp);\n    fclose(write_fp);\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\nvoid main() {\n    char *read_path = &quot;E:\\\\ndk\\\\files\\\\liuyan.png&quot;;\n    FILE *fp = fopen(read_path, &quot;r&quot;);\n    \/\/\u91cd\u65b0\u5b9a\u4f4d\u6587\u4ef6\u6307\u9488\n    \/\/SEEK_END\u6587\u4ef6\u672b\u5c3e\uff0c0\u504f\u79fb\u91cf\n    fseek(fp, 0, SEEK_END);\n    \/\/\u8fd4\u56de\u5f53\u524d\u7684\u6587\u4ef6\u6307\u9488\uff0c\u76f8\u5bf9\u4e8e\u6587\u4ef6\u5f00\u5934\u7684\u4f4d\u79fb\u91cf\n    long filesize = ftell(fp);\n    printf(&quot;%d\\n&quot;, filesize);\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<h2>\u7efc\u5408\u6848\u4f8b<\/h2>\n<pre><code class=\"language-c\">\/\/\u7ec3\u4e60\uff1a\u6587\u672c\u6587\u4ef6\u52a0\u89e3\u5bc6\n\/\/\u5f02\u6216\n\/\/\u89c4\u5219\uff1a1^1=0, 0^0=0, 1^0=1, 0^1=1 \u540c\u4e3a0\uff0c\u4e0d\u540c\u4e3a1\n\/\/\u52a0\u5bc6\nvoid crpypt(char normal_path[], char crypt_path[]) {\n    \/\/\u6253\u5f00\u6587\u4ef6\n    FILE *normal_fp = fopen(normal_path, &quot;r&quot;);\n    FILE *crypt_fp = fopen(crypt_path, &quot;w&quot;);\n    \/\/\u4e00\u6b21\u8bfb\u53d6\u4e00\u4e2a\u5b57\u7b26\n    int ch;\n    while ((ch = fgetc(normal_fp)) != EOF) { \/\/End of File\n        \/\/\u5199\u5165\uff08\u5f02\u6216\u8fd0\u7b97\uff09\n        fputc(ch ^ 9, crypt_fp);\n    }\n    \/\/\u5173\u95ed\n    fclose(crypt_fp);\n    fclose(normal_fp);\n}\n\n\/\/\u89e3\u5bc6\nvoid decrpypt(char crypt_path[], char decrypt_path[]) {\n    \/\/\u6253\u5f00\u6587\u4ef6\n    FILE *normal_fp = fopen(crypt_path, &quot;r&quot;);\n    FILE *crypt_fp = fopen(decrypt_path, &quot;w&quot;);\n    \/\/\u4e00\u6b21\u8bfb\u53d6\u4e00\u4e2a\u5b57\u7b26\n    int ch;\n    while ((ch = fgetc(normal_fp)) != EOF) { \/\/End of File\n        \/\/\u5199\u5165\uff08\u5f02\u6216\u8fd0\u7b97\uff09\n        fputc(ch ^ 9, crypt_fp);\n    }\n    \/\/\u5173\u95ed\n    fclose(crypt_fp);\n    fclose(normal_fp);\n}\n\nvoid main() {\n    char *normal_path = &quot;E:\\\\ndk\\\\files\\\\friends.txt&quot;;\n    char *crypt_path = &quot;E:\\\\ndk\\\\files\\\\friends_crypt.txt&quot;;\n    char *decrypt_path = &quot;E:\\\\ndk\\\\files\\\\friends_decrypt.txt&quot;;\n\n    \/\/\u52a0\u5bc6\n    \/\/crpypt(normal_path, crypt_path);\n    \/\/\u89e3\u5bc6\n    decrpypt(crypt_path, decrypt_path);\n\n    system(&quot;pause&quot;);\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/\u4e8c\u8fdb\u5236\u6587\u4ef6\u52a0\u89e3\u5bc6\n\/\/\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u65f6\uff0c\u4e00\u4e2a\u4e00\u4e2a\u5b57\u7b26\u8bfb\u53d6\n\/\/\u5bc6\u7801\uff1ailovely\nvoid crpypt(char normal_path[], char crypt_path[], char password[]) {\n    \/\/\u6253\u5f00\u6587\u4ef6\n    FILE *normal_fp = fopen(normal_path, &quot;rb&quot;);\n    FILE *crypt_fp = fopen(crypt_path, &quot;wb&quot;);\n    \/\/\u4e00\u6b21\u8bfb\u53d6\u4e00\u4e2a\u5b57\u7b26\n    int ch;\n    int i = 0; \/\/\u5faa\u73af\u4f7f\u7528\u5bc6\u7801\u4e2d\u7684\u5b57\u6bcd\u8fdb\u884c\u5f02\u6216\u8fd0\u7b97\n    int pwd_len = strlen(password); \/\/\u5bc6\u7801\u7684\u957f\u5ea6\n    while ((ch = fgetc(normal_fp)) != EOF) { \/\/End of File\n        \/\/\u5199\u5165\uff08\u5f02\u6216\u8fd0\u7b97\uff09\n        fputc(ch ^ password[i % pwd_len], crypt_fp);\n        i++;\n    }\n    \/\/\u5173\u95ed\n    fclose(crypt_fp);\n    fclose(normal_fp);\n}\n\n\/\/\u89e3\u5bc6\nvoid decrpypt(char crypt_path[], char decrypt_path[], char password[]) {\n    \/\/\u6253\u5f00\u6587\u4ef6\n    FILE *normal_fp = fopen(crypt_path, &quot;rb&quot;);\n    FILE *crypt_fp = fopen(decrypt_path, &quot;wb&quot;);\n    \/\/\u4e00\u6b21\u8bfb\u53d6\u4e00\u4e2a\u5b57\u7b26\n    int ch;\n    int i = 0; \/\/\u5faa\u73af\u4f7f\u7528\u5bc6\u7801\u4e2d\u7684\u5b57\u6bcd\u8fdb\u884c\u5f02\u6216\u8fd0\u7b97\n    int pwd_len = strlen(password); \/\/\u5bc6\u7801\u7684\u957f\u5ea6\n    while ((ch = fgetc(normal_fp)) != EOF) { \/\/End of File\n        \/\/\u5199\u5165\uff08\u5f02\u6216\u8fd0\u7b97\uff09\n        fputc(ch ^ password[i % pwd_len], crypt_fp);\n        i++;\n    }\n    \/\/\u5173\u95ed\n    fclose(crypt_fp);\n    fclose(normal_fp);\n}\n\nvoid main() {\n    char *normal_path = &quot;E:\\\\ndk\\\\files\\\\liuyan.png&quot;;\n    char *crypt_path = &quot;E:\\\\ndk\\\\files\\\\liuyan_crypt.png&quot;;\n    char *decrypt_path = &quot;E:\\\\ndk\\\\files\\\\liuyan_decrypt.png&quot;;\n\n    \/\/\u52a0\u5bc6\n    \/\/crpypt(normal_path, crypt_path, &quot;appblog.cn&quot;);\n    \/\/\u89e3\u5bc6\n    decrpypt(crypt_path, decrypt_path, &quot;appblog.cn&quot;);\n\n    getchar();\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u4ef6IO #define _CRT_SECURE_NO_WARNINGS #include &lt;stdli [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-408","post","type-post","status-publish","format-standard","hentry","category-android-ndk"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=408"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/408\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}