C/C++文件操作

#include <cstdlib>
#include <cstdio>
#include <stdio.h>
#include <iostream>

using namespace std;

int main(int argc,char* argv){
	FILE *pFile=fopen("c:\\a.txt","r+");
	if(NULL==pFile){
		cout<<"打开文件失败!"<<endl;		
		getchar();
		return 0;
	}

	char str[256];
	while(EOF!=fscanf(pFile,"%s",str)){
		cout<<str<<endl;	
	}
	fprintf(pFile,"END");
	fclose(pFile);

	cout<<"110707-01------------>"<<endl;
	getchar();
}

 

 

#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;

int main(int argc,char* argv){	
	fstream file;
	file.open("c:/a.txt");	
	if(file.fail()){
		cout<<"打开文件失败!"<<endl;
		getchar();
		return 0;
	}

	char str[256];
	while(file>>str){
		cout<<str<<endl;
	}
	file<<"END";	
	file.close();
		
	cout<<"110707-02--------------->"<<endl;
	getchar();
}

 

你可能感兴趣的:(c/c++)