为什么cpg文档没有显示#include<iostream> #include<fstream> using namespace std; int main() { fstream fout("e:\cp.txt",ios::out|ios::binary); if(!fout) { cerr<<"文件打开失败"<<endl; return 1; } char ch='a'; for(int i=0;i<26;i++) { fout.put(ch); ch++; } fout.close(); fstream fin("e:\cpg.txt",ios::out|ios::binary); if(!fin) { cerr<<"文件打开失败"<<endl; return 1; } char chr; while(fin.get(chr)) cout.put(chr-32); cout<<endl; fin.close(); system("pause"); return 0; }
时间: 2024-01-18 18:01:48 浏览: 26
可能是因为你在阅读的地方没有将文件名中的反斜杠(\) 转义。在 C++ 中,反斜杠是用来转义特殊字符的,例如换行符(\n)和制表符(\t)等。因此,如果你想在字符串中包含反斜杠,你需要在其前面加上另一个反斜杠,例如:
```cpp
fstream fout("e:\\cp.txt",ios::out|ios::binary);
```
同样地,在第二个文件名中也需要将反斜杠转义。
相关问题
为什么cpg文件没显示#include<iostream> #include<fstream> using namespace std; int main() { fstream fout("e:\\cp.txt",ios::out|ios::binary); if(!fout) { cerr<<"文件打开失败"<<endl; return 1; } char ch='a'; for(int i=0;i<26;i++) { fout.put(ch); ch++; } fout.close(); fstream fin("e:\\cpg.txt",ios::out|ios::binary); if(!fin) { cerr<<"文件打开失败"<<endl; return 1; } char chr; while(fin.get(chr)) cout.put(chr-32); cout<<endl; fin.close(); system("pause"); return 0; }
可能是因为你复制粘贴的时候,程序中的双引号被替换成了中文引号。在 C++ 中,只能使用双引号来表示字符串,不能使用中文引号或单引号。因此,你需要手动将程序中的中文引号替换成双引号。以下是修改后的代码:
```cpp
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream fout("e:\\cp.txt", ios::out | ios::binary);
if (!fout) {
cerr << "文件打开失败" << endl;
return 1;
}
char ch = 'a';
for (int i = 0; i < 26; i++) {
fout.put(ch);
ch++;
}
fout.close();
fstream fin("e:\\cpg.txt", ios::out | ios::binary);
if (!fin) {
cerr << "文件打开失败" << endl;
return 1;
}
char chr;
while (fin.get(chr)) {
cout.put(chr - 32);
}
cout << endl;
fin.close();
system("pause");
return 0;
}
```
#include<iostream> #include<fstream> using namespace std; int main() { fstream fout("e:\\cp.txt",ios::out|ios::binary); if(!fout) { cerr<<"文件打开失败"<<endl; return 1; } char ch='a'; for(int i=0;i<26;i++) { fout.put(ch); ch++; } fout.close(); fstream fin("e:\\cpg.txt",ios::out|ios::binary); if(!fin) { cerr<<"文件打开失败"<<endl; return 1; } char chr; while(fin.get(chr)) cout.put(chr-32); cout<<endl; fin.close(); system("pause"); return 0; }怎么cpg文档没有大写
在代码中,第10行打开文件时,文件名应该是"cpg.txt"而不是"e:\\cpg.txt",因此程序写入的内容实际上是存储在"cp.txt"文件中,而不是"cpg.txt"文件中。因此,读取"cpg.txt"文件时,文件是空的,所以输出没有大写字母。修改第10行的代码,将文件名改为"cpg.txt",重新运行程序即可。