鱼C论坛

 找回密码
 立即注册
查看: 1704|回复: 4

[已解决]如何写这个程序(详细进贴)

[复制链接]
发表于 2018-11-11 10:51:55 | 显示全部楼层 |阅读模式
5鱼币
本帖最后由 还差几 于 2018-11-11 10:59 编辑

程序:从一个.txt文件中,读取一个字符串,存入a变量中,使b变量等于a变量,再把b变量的字符串写入另一个.txt文件中(想问我为什么不直接把a变量写入另一个.txt文件中,而中间还加一个b变量,是因为我在将b变量等于a变量时,对b变量进行一些改动)
我知道这很基本,但我就是写不成功


最佳答案
2018-11-11 10:51:56
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>

  4. std::string ReadFile(const std::string &filename)
  5. {
  6.         std::ifstream in(filename, std::ios::binary);
  7.         if(!in)
  8.                 return std::string();
  9.         std::istreambuf_iterator<char> beg(in), end;
  10.         std::string strdata(beg, end);
  11.         in.close();
  12.         return strdata;
  13. }

  14. bool WriteFile(const std::string &filename, std::string data)
  15. {
  16.         std::ofstream out(filename, std::ios::binary);
  17.         if(!out)
  18.                 return false;
  19.         out << data;
  20.         out.close();
  21.         return true;
  22. }

  23. int main()
  24. {
  25.         std::string data = ReadFile("main.cpp");
  26.         std::cout << data << std::endl;
  27.         std::cout << WriteFile("Copy.cpp", data) << std::endl;
  28.         return 0;
  29. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-11-11 10:51:56 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>

  4. std::string ReadFile(const std::string &filename)
  5. {
  6.         std::ifstream in(filename, std::ios::binary);
  7.         if(!in)
  8.                 return std::string();
  9.         std::istreambuf_iterator<char> beg(in), end;
  10.         std::string strdata(beg, end);
  11.         in.close();
  12.         return strdata;
  13. }

  14. bool WriteFile(const std::string &filename, std::string data)
  15. {
  16.         std::ofstream out(filename, std::ios::binary);
  17.         if(!out)
  18.                 return false;
  19.         out << data;
  20.         out.close();
  21.         return true;
  22. }

  23. int main()
  24. {
  25.         std::string data = ReadFile("main.cpp");
  26.         std::cout << data << std::endl;
  27.         std::cout << WriteFile("Copy.cpp", data) << std::endl;
  28.         return 0;
  29. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-11-11 12:06:28 | 显示全部楼层
没有给汉字加密码的程序吗
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-11-11 14:58:21 | 显示全部楼层

有C语言的吗?
怪我没说清楚
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-11-11 15:23:25 | 显示全部楼层
还差几 发表于 2018-11-11 14:58
有C语言的吗?
怪我没说清楚

这很难吗?
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <stdbool.h>


  5. int32_t GetFileSize(const char *filename)
  6. {
  7.         FILE *file = fopen(filename, "r");
  8.         if(!file)
  9.                 return -1;

  10.         fseek(file, 0, SEEK_END);
  11.         int32_t size = ftell(file);
  12.         fclose(file);
  13.         return size;
  14. }

  15. bool ReadFile(const char *filename, unsigned char *data, size_t size)
  16. {
  17.         FILE *file = fopen(filename, "rb");
  18.         if(!file)
  19.                 return false;
  20.        
  21.         fread(data, 1, size, file);
  22.         fclose(file);
  23.         return true;
  24. }

  25. bool WriteFile(const char *filename, const unsigned char *data, size_t size)
  26. {
  27.         FILE *file = fopen(filename, "wb");
  28.         if(!file)
  29.                 return false;

  30.         fwrite(data, 1, size, file);
  31.         fclose(file);
  32.         return true;
  33. }

  34. int main(void)
  35. {
  36.         int32_t size = GetFileSize("main.c");
  37.         unsigned char *buf = malloc(size);
  38.         printf("ReadFile: %d\n", ReadFile("main.c", buf, size));
  39.         printf("WriteFile: %d\n", WriteFile("Copy.c", buf, size));
  40.         fwrite(buf, 1, size, stdout);
  41.         free(buf);
  42.         return 0;
  43. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-13 02:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表