关于文件读写(C和C++)

本文介绍如何使用C和C++进行文件读写操作,包括C语言中的fscanf、fprintf等函数及C++中的ifstream、ofstream等类的使用方法,并提供示例代码。

一、C语言版

在C语言中stdio.h头文件里包含了文件读写操作。主要是通过FILE*指针进行文件操作。通过fscanf和fprintf对文件进行格式化的读写,或通过fread 和 fwrite对文件进行二进制读写。(在数据量比较大时,一般建议用后者,因为格式化在输入时需要将ASCII码转为二进制形式,在输出时需要将二进制形式转为ASCII码,花费较大系统时间。fread 将文件的内容直接读入到一个指针中,fwrite将一个结构体的内容存放到文件中。

size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
size表示针对的单个单元或结构体的大小的字符数。
count表示单元的个数。 (当然只要size*count积一定即可,可以让size赋给count,然后count赋给size)
和C++不同之处在于:
FILE * fin=fopen("文件地址“,"r”);// 需要将函数的返回值赋给一个指针变量
fscanf(fin,"%d %d",&Row,&Column); // 需要注意的是需要用&,传递地址
fclose(fin);//是将FILE型指针 fin传给fclose函数 
二、C++版
#include <fstream>  引入文件操作头文件
ofstream 对于输出,ifstream对于输入,fstream既可输出又可以输入。
ifstream fin;
fin.open("E://C++//FileControl//FileControl//Debug//Matrix.txt");//调用成员函数
fin>>Row>>Column;//直接按流输出
fin.close(); //调用成员函数关闭

// FileControl.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"
#include <fstream> //For C++ File Control
#include <iostream>

#include <stdio.h>	//For C File Control
using namespace std;

/* You Can Change the Size to Meet your needs*/
#define MATRIX_LARGEST_SIZE 500  //MATRIX_ROW * MATRIX_COLUMN <= MATRIX_LARGEST_SIZE

/*here you can choose use C OR use C++ to realize the file control operation */
#if 1
#define C_Mode
#endif
#if 0
#define CPlusPlus_Mode
#endif

int _tmain(int argc, _TCHAR* argv[])
{
#ifdef CPlusPlus_Mode
	int Row, Column;
	int Matrix[MATRIX_LARGEST_SIZE];
	ifstream fin;// input file
	ofstream fout;//output file
	/*you can change the File String to your file location
	 ATTENTION: should be two '/'-->//  as the fileFolder seperate
	 */
	fin.open("E://C++//FileControl//FileControl//Debug//Matrix.txt");
	fin>>Row>>Column;
	cout<<"Row:"<<Row<<"/tColumn:"<<Column<<endl;
	/*
	Read All the Matrix Element Into the Matrix int array
	*/
	for(int i=0;i<Row;i++)
	{	for(int j=0;j<Column;j++)
		{
		 fin>>Matrix[i*Column+j];
		}
	}

	/*
	we print the Matrix in the Console
	*/
	for(int i=0;i<Row;i++)
	{	
		for(int j=0;j<Column;j++)
		{
			cout<<Matrix[i*Column+j]<<"/t";
		}
		cout<<endl;
	}
	
	/*
	You can write the Matrix to a file
	*/
	fout.open("E://C++//FileControl//FileControl//Debug//OutputCPlusPlus.txt");
	for(int i=0;i<Row;i++)
	{	
		for(int j=0;j<Column;j++)
		{
			fout<<Matrix[i*Column+j]<<"/t";
		}
		fout<<endl;
	}
	fin.close();
	fout.close();
	cin>>Row;// I add this just want the Application to stop here~~~So you can see the output in the console.
#endif 
#ifdef C_Mode
	int Row=0, Column=0;
	int Matrix[MATRIX_LARGEST_SIZE];
	FILE * fin;// input file
	FILE * fout;//output file
	/*you can change the File String to your file location
	 ATTENTION: should be two '/'-->//  as the fileFolder seperate
	 */
	fin=fopen("E://C++//FileControl//FileControl//Debug//Matrix.txt","r");
	fscanf(fin,"%d %d",&Row,&Column);
	printf("Row:%d/tColumn:%d/n",Row,Column);
	/*
	Read All the Matrix Element Into the Matrix int array
	*/
	for(int i=0;i<Row;i++)
	{	for(int j=0;j<Column;j++)
		{
		 fscanf(fin,"%d",&Matrix[i*Column+j]);
		}
	}

	/*
	we print the Matrix in the Console
	*/
	for(int i=0;i<Row;i++)
	{	
		for(int j=0;j<Column;j++)
		{
			printf("%d/t",Matrix[i*Column+j]);
		}
		printf("/n");
	}
	
	/*
	You can write the Matrix to a file
	*/
	fout=fopen("E://C++//FileControl//FileControl//Debug//OutputC.txt","w");
	for(int i=0;i<Row;i++)
	{	
		for(int j=0;j<Column;j++)
		{
			fprintf(fout,"%d/t",Matrix[i*Column+j]);
		}
		fprintf(fout,"/n");
	}
	fclose(fin);
	fclose(fout);
	scanf("%d",Row);
#endif

	return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值