C++檔案讀寫詳解(ofstream,ifstream,fstream)

andi_song發表於2016-10-21

記錄學習心得,資料轉自晨雪無痕 原創:C++檔案讀寫詳解(ofstream,ifstream,fstream)

 一、在Ubuntu平臺編譯執行驗證

1. 編譯執行命令

clear \r
rm  ./out/* \r
g++  -o ./out/hello hello.c && ./out/hello \r

2.標頭檔案

hello.h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

#include <sys/stat.h>
#include <iostream>
#include <fstream>
using namespace std;


二、函式驗證

test_fstream

int test_fstream()
{<span style="font-family: Arial, Helvetica, sans-serif;">//From: http://blog.csdn.net/kingstar158/article/details/6859379/</span>
	char line1[]="This is a line.\n";
	char line2[]="This is another line.\n";
	const char *f = "file_out.txt";
	const char *filename = "file_out.bin";
#if 0
	char line1[]="This is a line.\n";
	char line2[]="This is another line.\n";
	ofstream out("file_out.txt");
	if(out.is_open())
	{
		printf("line1.lenth = %d\n",strlen(line1));
		printf("line2.lenth = %d\n",strlen(line2));
		out << line1;	// 16
		out << line2;	// 16
		out.close();
	}
#elif 0	// text file
	char buffer[256];

	struct stat st;
	stat(f,&st);
	printf("st_mode = %x\n", st.st_mode);
	char m=6;
//	int v= chmod(f, m<<6|m<<3|m); 		//  -rwxrwxrwx		-rwxXSt
	int v= chmod(f, 0777);
	printf("st_mode = %x\n", st.st_mode);



	ifstream in(f);

	if(! in.is_open())
	{
		cout << "Error: opening file fail!\n\n";
		cout << "in.bad()  "<< in.bad() << endl;
		cout << "in.fail() "<< in.fail() << endl;
		cout << "in.eof()  "<< in.eof() << endl;
		cout << "in.good() "<< in.good() << endl;
		exit(1);
	}
	while(!in.eof())
	{
		in.getline (buffer,100);
		cout << buffer << endl;
	}
#elif 0// 1 // binary file
		long l,m;
		ifstream in(filename, ios::in|ios::binary);
		if (!in.is_open())
		{
			printf("Error: ");
			cout << "in.bad()  "<< in.bad() << endl;
			cout << "in.fail() "<< in.fail() << endl;
			cout << "in.eof()  "<< in.eof() << endl;
			cout << "in.good() "<< in.good() << endl;
		//	exit(1);
		}
		l = in.tellg();
		in.seekg(0, ios::end);
		m = in.tellg();
		in.close();

		cout << "size of " << filename;
		cout << " is: " << (m-1) << " Bytes.\n";
#elif 0 // 2 // binary file read
	char * buffer;
	long size;
//	fstream in(filename, ios::in|ios::binary|ios::ate);
	fstream in(filename, ios::binary|ios::ate|/*ios::out|*/ios::in);
	if (!in.is_open())
	{
		printf("Error: ");
		cout << "in.bad()  "<< in.bad() << endl;
		cout << "in.fail() "<< in.fail() << endl;
		cout << "in.eof()  "<< in.eof() << endl;
		cout << "in.good() "<< in.good() << endl;
	//	exit(1);
	}
	size = in.tellg();
	cout << "size = " << size << endl;
	in.seekg(0,ios::beg);

	buffer = new char [size];
	in.read(buffer,size);

	cout << "the complete file is in a buffer\n";
	printf("buffer[%ld] = \n%s\n", size, buffer);
	in.close();
	delete[] buffer;
#elif 3 // binary file write
	const char *filenameOut = "file_out2.bin";
	ofstream out(filenameOut, ios::binary|ios::ate|ios::out);
	if (!out.is_open())
	{
		printf("Error: ");
		cout << "in.bad()  "<< out.bad() << endl;
		cout << "in.fail() "<< out.fail() << endl;
		cout << "in.eof()  "<< out.eof() << endl;
		cout << "in.good() "<< out.good() << endl;
	//	exit(1);
	}
	#define MAX 	 6
		unsigned char data[]={ 0xb8,0x3d,0x4e,0x01,0x02,0x03,0};
	//printf("buffer[%d] = \n%s\n", MAX, data);
		printf("buffer[%d] = { ",MAX);
		unsigned char i=0;
		while(data[i])
		{
			printf("0x%x, ", data[i++]);
		}
		printf(" };\n");

		out.seekp(0,ios::beg);

		out.write((char *)&data[0],MAX);
		out.tellp();
	out.close();

#elif 4 // 測試 <span style="font-family: Arial, Helvetica, sans-serif;">ftell()獲取檔案大小</span>

<span style="white-space:pre">			</span>long fsize;
<span style="white-space:pre">			</span>#define FILE_NAME<span style="white-space:pre">	</span>"file_out3.bin"
<span style="white-space:pre">			</span>FILE *stream;
<span style="white-space:pre">			</span>stream= fopen(FILE_NAME, "r");
<span style="white-space:pre">			</span>if(NULL != stream)
<span style="white-space:pre">			</span>{
<span style="white-space:pre">				</span>fsize = ftell(stream);
<span style="white-space:pre">				</span>printf("fsize = %ld\n",fsize);
<span style="white-space:pre">				</span>fseek(stream, 0 ,SEEK_END);
<span style="white-space:pre">				</span>fsize = ftell(stream);
<span style="white-space:pre">				</span>printf("fsize = %ld\n",fsize);
<span style="white-space:pre">			</span>fclose(stream);


<span style="white-space:pre">			</span>}
#else
	#error: "Not set"
#endif
	return 0;
}



相關文章