开发必备之输入输出【c++】

在C语言中,处理输入和输出的方式多种多样,主要通过标准库函数来实现。这些函数主要分为控制台输入输出和文件输入输出两类。以下是几种常见的处理输入输出的方式:

1. 控制台输入输出

printfscanf
  • printf:用于将格式化的数据输出到标准输出(通常是屏幕)。

printf("Hello, World!\n"); // 输出 "Hello, World!" 到屏幕
int num = 10;
printf("Number: %d\n", num); // 输出整数 num

scanf:用于从标准输入(通常是键盘)读取格式化数据。

int num;
printf("Enter a number: ");
scanf("%d", &num); // 从用户输入读取整数并存储到 num
putchargetchar
  • putchar:用于输出单个字符到标准输出。

putchar('A'); // 输出字符 'A'

getchar:用于从标准输入读取单个字符。

char ch;
printf("Enter a character: ");
ch = getchar(); // 读取一个字符并存储到 ch
putsgets
  • puts:用于输出字符串到标准输出,自动在字符串末尾加上换行符。

puts("Hello, World!"); // 输出字符串并自动换行

gets(不推荐使用):用于从标准输入读取字符串,直到遇到换行符为止。由于gets不做边界检查,容易导致缓冲区溢出,建议使用fgets替代。

char str[100];
gets(str); // 读取用户输入的字符串(不安全,建议使用fgets)

2. 文件输入输出

fopen, fclose
  • fopen:用于打开文件,返回文件指针。可以指定不同的模式(如读、写、追加)。

    FILE *fp;
    fp = fopen("example.txt", "r"); // 以读模式打开文件
    

  • fclose:用于关闭文件,释放资源。

fprintffscanf
  • fprintf:用于将格式化的数据写入文件。

FILE *fp = fopen("example.txt", "w");
fprintf(fp, "Number: %d\n", 123); // 将数据写入文件
fclose(fp);
  • fscanf:用于从文件中读取格式化的数据。

FILE *fp = fopen("example.txt", "r");
int num;
fscanf(fp, "%d", &num); // 从文件中读取整数并存储到 num
fclose(fp);

 

fgetsfputs
  • fgets:用于从文件或标准输入读取一行文本。

    FILE *fp = fopen("example.txt", "r");
    char buffer[100];
    fgets(buffer, 100, fp); // 读取一行文本到 buffer
    fclose(fp);
    

  • fputs:用于将字符串写入文件或标准输出。

FILE *fp = fopen("example.txt", "w");
fputs("Hello, World!\n", fp); // 将字符串写入文件
fclose(fp);
freadfwrite
  • fread:用于从文件中读取二进制数据。

FILE *fp = fopen("example.bin", "rb");
int buffer[10];
fread(buffer, sizeof(int), 10, fp); // 读取10个整数到buffer中
fclose(fp);
  • fwrite:用于将二进制数据写入文件。

FILE *fp = fopen("example.bin", "wb");
int data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
fwrite(data, sizeof(int), 10, fp); // 将10个整数写入文件
fclose(fp);

3. 标准I/O缓冲机制

C语言的标准输入输出系统是缓冲的。这意味着printfscanffwritefread等函数在执行I/O操作时,实际上是先将数据存放在缓冲区中,当缓冲区满或显式调用fflush时,才会将数据写入目标文件或设备。了解这一机制可以帮助开发者更好地控制I/O操作的性能和行为。

4. Error Handling

处理I/O操作时,需要检查每个操作的返回值,以确保操作成功。例如:

  • fopen失败时返回NULL
  • fscanf返回读取成功的项目数量,读取失败时返回EOF
FILE *fp = fopen("example.txt", "r");
if (fp == NULL)
{
    perror("Error opening file");
    return -1;
}

5. 格式控制

使用printfscanffprintf等格式化I/O函数时,需要特别注意格式控制符,如%d%s%f等,以确保数据的正确解析和输出。

6. 缓冲区安全

避免使用gets函数而推荐使用fgets,原因是gets不检查输入大小,可能导致缓冲区溢出。fgets可以指定读取字符的最大数量,从而避免这种情况。

char buffer[100];
fgets(buffer, 100, stdin); // 安全地读取字符串

 

在C++中,处理输入输出有多种方式,涵盖从控制台到文件的各种场景。以下是几种常见的C++输入输出处理方式:

1. 控制台输入输出

std::cinstd::cout
  • std::cin:用于从标准输入设备(通常是键盘)读取数据。
  • std::cout:用于将数据输出到标准输出设备(通常是屏幕)。
#include <iostream>

int main() {
    int num;
    std::cout << "Enter a number: ";
    std::cin >> num;  // 从标准输入读取整数
    std::cout << "You entered: " << num << std::endl;  // 输出结果
    return 0;
}
  • std::endl:用于输出换行符,并刷新缓冲区。
std::cerrstd::clog
  • std::cerr:用于输出错误信息,输出到标准错误流,通常不经过缓冲,直接输出。
  • std::clog:用于输出日志信息,输出到标准错误流,经过缓冲后输出。
std::cerr << "Error occurred!" << std::endl;
std::clog << "Log message" << std::endl;

2. 文件输入输出

std::ifstreamstd::ofstream
  • std::ifstream:用于从文件中读取数据。
  • std::ofstream:用于将数据写入文件。
#include <iostream>
#include <fstream>

int main() {
    std::ofstream outFile("example.txt");
    if (outFile.is_open()) {
        outFile << "Writing this to a file.\n";
        outFile.close();  // 关闭文件
    } else {
        std::cerr << "Unable to open file for writing." << std::endl;
    }

    std::ifstream inFile("example.txt");
    if (inFile.is_open()) {
        std::string line;
        while (getline(inFile, line)) {
            std::cout << line << std::endl;  // 从文件中读取并输出到控制台
        }
        inFile.close();
    } else {
        std::cerr << "Unable to open file for reading." << std::endl;
    }

    return 0;
}
std::fstream
  • std::fstream:同时支持读写操作的文件流,可以用于同时进行文件的输入和输出。
#include <iostream>
#include <fstream>

int main() {
    std::fstream file("example.txt", std::ios::in | std::ios::out | std::ios::app);
    if (file.is_open()) {
        file << "Appending text to file." << std::endl;  // 写入文件
        file.seekg(0);  // 将文件指针移到文件开始
        std::string line;
        while (getline(file, line)) {
            std::cout << line << std::endl;  // 读取文件内容
        }
        file.close();
    } else {
        std::cerr << "Unable to open file." << std::endl;
    }

    return 0;
}

 

3. 字符串流

std::istringstreamstd::ostringstream
  • std::istringstream:用于从字符串读取数据。
  • std::ostringstream:用于将数据写入字符串。
#include <iostream>
#include <sstream>

int main() {
    std::ostringstream oss;
    oss << "This is a test: " << 42;
    std::string str = oss.str();  // 获取字符串
    std::cout << str << std::endl;

    std::istringstream iss(str);
    std::string word;
    while (iss >> word) {
        std::cout << word << std::endl;  // 逐个读取单词
    }

    return 0;
}

4. 格式化输入输出

std::setw, std::setprecision, std::fixed
  • std::setw:设置输出字段的宽度。
  • std::setprecision:设置浮点数的输出精度。
  • std::fixed:以固定点方式输出浮点数。
#include <iostream>
#include <iomanip>

int main() {
    double pi = 3.14159;
    std::cout << "Pi with precision 2: " << std::setprecision(2) << pi << std::endl;
    std::cout << "Pi with precision 4: " << std::fixed << std::setprecision(4) << pi << std::endl;
    std::cout << "Right-aligned number with width 10: " << std::setw(10) << 42 << std::endl;
    return 0;
}

5. 操作符重载用于输入输出

C++允许重载<<>>操作符,使得类可以自定义输入输出操作。

#include <iostream>

class Point {
public:
    int x, y;

    friend std::ostream& operator<<(std::ostream& os, const Point& p) {
        os << "Point(" << p.x << ", " << p.y << ")";
        return os;
    }

    friend std::istream& operator>>(std::istream& is, Point& p) {
        is >> p.x >> p.y;
        return is;
    }
};

int main() {
    Point p;
    std::cout << "Enter point coordinates (x y): ";
    std::cin >> p;
    std::cout << "You entered: " << p << std::endl;
    return 0;
}

 

6. Error Handling and Checking Stream State

  • 在处理输入输出时,检查流的状态非常重要,以确保操作成功完成。
std::ifstream file("example.txt");
if (!file) {
    std::cerr << "Error opening file." << std::endl;
} else {
    std::string line;
    while (std::getline(file, line)) {
        std::cout << line << std::endl;
    }
    if (file.bad()) {
        std::cerr << "Error reading file." << std::endl;
    }
    file.close();
}

总结

C++提供了多种输入输出处理方式,包括控制台输入输出、文件输入输出、字符串流、格式化输入输出等。这些机制允许开发者灵活处理数据流、文件操作以及用户交互,从而编写更加健壮和灵活的应用程序。

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值