• C_C++文件,字符串和控制台格式化处理总结


           在实际业务开发中经常会用到文件,字符串和控制台格式化操作,格式化操作无非就是将数据转成指定格式存储在文件或者字符串,或者显示在控制台上,或者反过来。本篇结合实际工作将C/C++语言中常用的文件,字符串和控制台常用格式化方法进行整理和介绍。

    目录

    1. 控制台格式化

    1.1 输入格式化

    1.1.1 C语言常用函数

    1.1.2  C++常用函数

    1.2 输出格式化

    1.2.1 C语言常用函数

    1.2.2 C++常用函数

    2. 字符格式化

    2.1 输入格式化

    2.1.1 C语言常用函数

    2.1.2 C++常用函数

    2.2 输出格式化

    2.2.1 C语言常用函数

    2.2.2 C++常用函数

    3. 文件格式化

    3.1 输入格式化

    3.1.1 C语言常用函数

    3.1.2 C++常用函数

    3.2 输出格式化

    3.2.1 C语言常用函数

    3.2.2 C++常用函数

    4. 调用举例

    4.1 C语言调用 

    4.2 C++调用


    1. 控制台格式化

    1.1 输入格式化

    1.1.1 C语言常用函数

    scanf

    1.1.2  C++常用函数

    cin

    1.2 输出格式化

    1.2.1 C语言常用函数

    printf

    1.2.2 C++常用函数

    cout

    2. 字符格式化

    2.1 输入格式化

    2.1.1 C语言常用函数

    sscanf

    2.1.2 C++常用函数

    stringstream / istringstream

    2.2 输出格式化

    2.2.1 C语言常用函数

    sprintf

    2.2.2 C++常用函数

    stringstream / ostringstream

    3. 文件格式化

    3.1 输入格式化

    3.1.1 C语言常用函数

    fscanf

    3.1.2 C++常用函数

    fstream / ifstream

    3.2 输出格式化

    3.2.1 C语言常用函数

    fprintf

    3.2.2 C++常用函数

    fstream / ofstream

    4. 调用举例

    如下分别使用C和C++来实现对文件,字符串和控制台的格式化输入输出,用两种语言实现同样的功能以做对比

    (1)对于文件:先格式化输出内容到文件,在从文件格式化输入到内存中各个对应变量中,其中会过滤到特殊字符,只提取关心的数据

    (2)对于字符串:会从字符串中格式化提取到对应变量,然后再格式化输出到字符串中

    (3)对于控制台:就是最常用的接收接盘输入,然后在输出

    4.1 C语言调用 

    1. #include
    2. #include
    3. int main() {
    4. //================ C语言文件输入输出格式化 ===================
    5. char str1[128] = {0}, str2[128] = {0}, str3[128] = {0}, str4[128] = {0};
    6. int num1 = 0, num2 = 0, num3 = 0;
    7. FILE *fp = fopen("test.txt", "w+");
    8. fputs("2022 11 30 rewrw 57567\n\r", fp);
    9. //格式化输出
    10. fprintf(fp, "%d + %d = %d\n", 1, 2, 3);
    11. fprintf(fp, "%s + %s = %s\n", "abc", "def", "abcdef");
    12. rewind(fp);
    13. fscanf(fp, "%s %s %s %s %d", str1, str2, str3, str4, &num1);
    14. printf("[%s] [%s] [%s] [%s] [%d]\n", str1, str2, str3, str4, num1);
    15. //fscanf(fp, "%*[\n]%*[\r]%[^ ] %*[+] %[^ ] %*[=] %[0-9]", str1, str2, str3);
    16. // %*[\n\r]:表示过滤\r\n %*[+]:过滤+ %*[=]:过滤=
    17. fscanf(fp, "%*[\r\n]%[0-9] %*[+] %[0-9] %*[=] %[0-9]", str1, str2, str3);
    18. printf("[%s] [%s] [%s]\n", str1, str2, str3);
    19. fscanf(fp, "%*[\n]%s %*[+] %s %*[=] %s", str1, str2, str3);
    20. printf("[%s] [%s] [%s]\n\n", str1, str2, str3);
    21. fclose(fp);
    22. //============ C语言字符串输入输出格式化 =================
    23. char* strIp = "192.168--10.10";
    24. int first = 0, second = 0, third = 0, fouth = 0;
    25. //注意其中的空格
    26. sscanf(strIp, "%d.%d- %d.%d", &first, &second, &third, &fouth);
    27. printf("[%d] [%d] [%d] [%d]\n", first, second, third, fouth);
    28. char *strTime = "2022-11-30_19:46:37_345";
    29. int year = 0, month = 0, day = 0, hour = 0, minute = 0, seconds = 0, mseconds = 0;
    30. //数值输入
    31. sscanf(strTime, "%d-%d-%d_%d:%d:%d_%d",
    32. &year, &month, &day, &hour, &minute, &seconds, &mseconds);
    33. printf("%d %d %d %d %d %d %d\n", year, month, day, hour, minute, seconds, mseconds);
    34. char buf1[128] = {0}, buf2[32] = {0}, buf3[32] = {0}, buf4[32] = {0}, buf5[32] = {0}, buf6[32] = {0}, buf7[32] = {0};
    35. //字符串输入
    36. sscanf(strTime, "%[^-]-%[^-]-%[^_]_%[^:]:%[^:]:%[^_]_%s", buf1, buf2, buf3, buf4, buf5, buf6, buf7);
    37. printf("%s %s %s %s %s %s %s\n", buf1, buf2, buf3, buf4, buf5, buf6, buf7);
    38. char *strHttpReq = "GET /index.htm HTTP/1.1";
    39. sscanf(strHttpReq, "%s %s %s", buf1, buf2, buf3);
    40. printf("%s-%s-%s\n", buf1, buf2, buf3);
    41. sprintf(buf1, "%s, %s, %s, today is %d-%d-%s\n\n",
    42. "andy", "hello", "很高兴认识你", 2022, 11, "30");
    43. printf(buf1);
    44. //================ C语言控制台格式化输入输出 =================
    45. scanf("%s %d %s", buf1, &num1, buf2);
    46. printf("%s-%d-%s\n", buf1, num1, buf2);
    47. return 0;
    48. }

    运行结果如下:

    4.2 C++调用

    1. #include
    2. #include
    3. #include
    4. #include //setw setfill
    5. using namespace std;
    6. int main() {
    7. //================ C++文件输入输出格式化 ===================
    8. std::string str1, str2, str3, str4, str5;
    9. int num1 = 0, num2 = 0, num3 = 0, num4 = 0;
    10. std::fstream fStream("test.txt", std::ios::out | std::ios::in);
    11. if (fStream) {
    12. //输出操作
    13. fStream << 2022 << " " << 11 << " " << 30 << " rewrw 57567" << std::endl;
    14. fStream << 1 << " + " << 2 << " = " << 3 << std::endl;
    15. fStream << "abc" << " " << "def" << " " << "abcdef" << std::endl;
    16. //输入操作
    17. fStream.seekg(0, ios::beg);
    18. fStream >> str1 >> str2 >> str3 >> str4 >> num1;
    19. std::cout << str1 << "-" << str2 << "-" << str3 << "-" << str4 << "-" << num1 << std::endl;
    20. fStream.seekg(0, ios::beg);
    21. fStream >> num1 >> num2 >> num3 >> str1 >> num4;
    22. std::cout << num1 << "-" << num2 << "-" << num3 << "-" << str1 << "-" << num4 << std::endl;
    23. fStream >> str1 >> str2 >> str3 >> str4 >> str5;
    24. std::cout << str1 << "|"<< str2 << "|" << str3 << "|" << str4
    25. << "|" << str5 << std::endl;
    26. fStream >> str1 >> str2 >> str3;
    27. std::cout << str1 << "|"<< str2 << "|" << str3 << std::endl << std::endl;
    28. fStream.close();
    29. }
    30. //============ C++字符串输入输出格式化 =================
    31. std::string strIp = "192.168--10.10";
    32. int first = 0, second = 0, third = 0, fouth = 0;
    33. std::stringstream ss(strIp);
    34. //提取IP地址
    35. ss >> first >> setw(1) >> str1 >> second >> setw(1) >> str2 >> third >> setw(1) >> str3 >> fouth;
    36. std::cout << first << "." << second << "." << third << "." << fouth<< std::endl;
    37. //提取时间
    38. ss.clear();
    39. std::string strTime = "2022-11-30_19:46:37_345";
    40. ss.str(strTime);
    41. int year = 0, month = 0, day = 0, hour = 0, minute = 0, seconds = 0, mseconds = 0;
    42. ss >> year >> setw(1) >> str1 >> month >> setw(1) >> str1 >> day >> setw(1) >> str1 >>
    43. hour >> setw(1) >> str1 >> minute >> setw(1) >> str1 >> seconds >> setw(1) >> str1 >> mseconds;
    44. std::cout << year << '-' << month << '-' << day << '_' << hour <<
    45. ':' << minute << ':' << seconds << '_'<< mseconds << std::endl;
    46. ss.clear();
    47. ss.str("GET /index.htm HTTP/1.1");
    48. ss >> str1 >> str2 >> str3;
    49. std::cout << str1 << "|" << str2 << "|" << str3 << std::endl;
    50. ss.clear();
    51. ss.str("");
    52. ss << "andy, " << "hello, " << "很高兴认识你, today is " << 2022 << 11 << "30";
    53. std::cout << ss.str() << std::endl;
    54. //拆分字符串
    55. ss.clear();
    56. ss.str("123|456|789|101111|131415|161718|192021");
    57. std::string item;
    58. //std::getline第一个参数是输入流 cin istringstream ifstream参数均可以
    59. while (std::getline(ss, item, '|')) {
    60. std::cout << item << " ";
    61. }
    62. std::cout << std::endl << std::endl;
    63. //================ C++控制台格式化输入输出 =================
    64. std::cin >> str1 >> num1 >> str2;
    65. std::cout << str1 << "=" << num1 << "=" << str2 << std::endl;
    66. //从键盘获取一行文本,遇到\n结束,文本之间可以有空格
    67. std::getline(std::cin, str1);
    68. std::cout << str1 << std::endl;
    69. //遇到空格就结束取值,空格(包括)的输入不会存储在str1中
    70. //std::getline(std::cin, str1, ' ');
    71. return 0;
    72. }

    运行结果如下:

  • 相关阅读:
    动手实践:从栈帧看字节码是如何在 JVM 中进行流转的
    Spring面试题:(八)Spring事务
    400电话-申请400电话-400电话如何申请-400电话申请指南:简单步骤助您顺利获得400电话
    13.4-软件测试标准 13.5-测试过程标准 13.6-测试文档标准 13.7-测试技术标准
    华为云云耀云服务器L实例评测 | Linux系统宝塔运维部署H5游戏
    PySpark数据分析基础:pyspark.mllib.regression机器学习回归核心类详解(一)+代码详解
    在Mysql中一条查询SQL的执行过程是怎样的
    Android APP稳定性测试工具Fastbot
    简单解决网页的验证码
    Go语言 rand(随机数)包
  • 原文地址:https://blog.csdn.net/hsy12342611/article/details/128138689