ファイルの読み書きについて(CとC++)


一、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;
}