C++标准输入输出、文件操作、字符数组与动态内存管理
立即解锁
发布时间: 2025-08-20 01:48:55 阅读量: 1 订阅数: 3 


懒惰程序员的C++入门指南
### C++ 标准输入输出、文件操作、字符数组与动态内存管理
#### 1. 标准输入输出与文件操作
在 C++ 编程中,标准输入输出(I/O)是基础操作,同时文件操作也十分重要。下面为你详细介绍相关内容。
##### 1.1 文件字符大写转换
可以编写程序将文件中的字符转换为大写形式。示例代码如下:
```cpp
//Program to produce an ALL CAPS version of a file
// -- from _C++ for Lazy Programmers_
#include <iostream>
#include <cctype> //for toupper
using namespace std;
int main ()
{
while (cin) //for each char in file
{
char ch = cin.get(); //read in char
ch = toupper (ch); //capitalize
if (cin) cout << ch; //cin still OK? Then print
}
return 0;
}
```
此程序会逐个读取文件中的字符,将其转换为大写后输出。
##### 1.2 计算文件中数字的平均值
计算文件中数字平均值时,可能会遇到一些问题。以下是有问题的代码:
```cpp
double total = 0.0; //initialize total and howMany
int howMany = 0;
while (cin) //while there are numbers in file
{
int num; cin >> num; //read one in
total += num; //keep running total
++howMany;
}
```
若输入文件内容为:
```
1
2
```
得到的平均值可能会是 1.6667,这显然是错误的。问题在于程序在文件末尾判断不准确,可能会重复读取最后一个数字。正确的做法是每次读取后检查输入是否有效:
```cpp
int num; cin >> num; //read one in
if (cin) //still no problems with cin, right?
{
total += num; //keep running total
++howMany;
}
```
##### 1.3 标准 I/O 练习
下面是一些使用标准 I/O 的练习:
1. 读取一系列数字并以逆序输出。可声明一个足够大的数组(如大小为 100)来存储这些数字。
2. 统计文件中的字符数量。
3. 统计文件中除空白和标点符号之外的字符数量。
##### 1.4 使用文件名进行文件操作
每次都使用重定向操作符(>)来指定输入输出文件比较麻烦。可以通过以下步骤使用命名文件:
1. 包含 `<fstream>` 头文件,该文件包含所需的定义。
2. 声明输入文件变量,如 `ifstream inFile;`;若为输出文件,则使用 `ofstream`。
3. 打开文件,如 `inFile.open ("level1.txt");`,此操作将文件与文件名关联,并确保文件存在。
4. 验证文件是否成功打开。对于输入文件,可能的错误是文件不存在或不在预期的文件夹中;对于输出文件,可能存在磁盘问题或文件为只读。验证方法如下:
```cpp
if (! inFile) // handle error
```
5. 将 `cin` 替换为 `inFile`,若为输出文件,则将 `cout` 替换为 `outFile`。
6. 完成操作后,关闭文件:`inFile.close ();`。
以下是一个使用命名文件的示例代码:
```cpp
//A (partial) game with killer robots
// meant to demonstrate use of file I/O
//This loads 3 points and prints a report
// -- from _C++ for Lazy Programmers_
#include <iostream>
#include <fstream> //1. include <fstream>
using namespace std;
struct Point2D { int x_=0, y_=0; };
int main ()
{
//an array of robot positions
enum { MAX_ROBOTS = 3 };
Point2D robots[MAX_ROBOTS];
//2. Declare file variables.
//3. Open the files.
//Here's two ways to do both; either's fine
ifstream inFile; inFile.open("level1.txt");
ofstream outFile ("saveGame1.txt");
//4. Verify the files opened without error
if (!inFile)
{
cout << "Can't open level1.txt!\n"; return 1;
// 1 is a conventional return value for error
}
if (! outFile)
{
cout << "Can't create file saveGame1.txt!"; return 1;
}
//5. Change cin to inFile, cout to outFile
int whichRobot = 0;
//while there's input and array's not full...
while (inFile && whichRobot < MAX_ROBOTS)
{
int x, y;
inFile >> x >> y; //read in an x, y pair
if (inFile) //if we got valid input (not at eof)
{ //store what we read
robots[whichRobot] = {x, y};
++whichRobot; //and remember there's 1 more robot
}
}
for (int i = 0; i < MAX_ROBOTS; ++i)
outFile << robots[i].x_ << ' '
<< robots[i].y_ << endl;
//6. When done, close the files
inFile.close(); outFile.close();
//can still use cout for other things
cout << "Just saved saveGame1.txt.\n";
return 0;
}
```
这个程序从 `level1.txt` 文件中读取机器人的位置信息,并将其保存到 `saveGame1.txt` 文件中。
##### 1.5 文件操作练习
以下是一些文件操作的练习:
1. 编写程序判断两个文件是否相同。
2. 编写并测试从 `Point2D` 文件中读取数据和向文件中写入数据的函数。
3. 掷 2 个骰
0
0
复制全文
相关推荐









