C++ 文件操作
2014-09-21 20:31
169 查看
<pre name="code" class="cpp">ofstream类支持磁盘文件输出 //文件的输出 #include <fstream> using namespace std; void main() //程序从这里开始运行 { ofstream SaveFile("cpp-home.txt"); SaveFile <<"Hello World, from www.cpp-home.com and Loobian!"; SaveFile<<endl; SaveFile<<"the end"<<endl; SaveFile.close(); } //文件的读入 #include<iostream> #include <fstream> using namespace std; void main() //程序从这里开始 { ifstream OpenFile("cpp-home.txt"); char ch; while(!OpenFile.eof()) { OpenFile.get(ch); cout<< ch; } OpenFile.close(); } //C语言写入 #include <stdio.h> int main() { FILE * fp =fp=fopen("cpp-home.txt","w");; fprintf(fp,"%s\n","hello!"); fclose(fp); return 0; } //c 文件的读取 /* //cpp-home.txt 1 2 3 4 5 6 7 8 */ #include "stdio.h" int main() { FILE *fp; int a[2][4]={0}; int i,j; if((fp=fopen("cpp-home.txt","rt"))==NULL) { printf("cannot open file\n"); return 1; } for(i=0;i<2;i++) { for(j=0;j<4;j++) fscanf(fp,"%d",&a[i][j]); fscanf(fp,"\n"); } for(i=0;i<2;i++) { for(j=0;j<4;j++) printf("%d ",a[i][j]); printf("\n"); } fclose(fp); return 0; } 使用width控制输出宽度 cout.width(10); cout.fill('*'); cout<<values[i]<<'\n'; 使用setw指定宽度 cout<<setw(6)<<names[i]<<setw(10)<<values[i]<<endl;
相关文章推荐
- C++中基本的文件操作
- C++中的文件操作
- c++文件常见操作示例
- borland c++ bulder的文件操作总结
- C++ 文件操作
- c++ 资源文件操作
- C++文件操作之二 : C++ 的fstream类提供的文件操作
- C++简易文件操作源代码
- C/C++文件操作
- C++中的文件操作
- DirectShow : AVI文件操作 -- C++ 类
- c++ 操作.ini 文件(适用平台linux windows)
- C++基本文件操作
- C++中关于文件操作的函数
- (补课)borland c++ bulder的文件操作总结-4
- (补课)borland c++ bulder的文件操作总结-3
- c++中关于文件操作资料收集(值得收藏)
- C++中文件操作的一些函数
- c++的文件操作
- (补课)borland c++ bulder的文件操作总结-1