C++文件读写详解(ofstream,ifstream,fstream)

来源:互联网 发布:英语学习软件开发 编辑:程序博客网 时间:2024/05/02 01:08

记录学习心得,资料转自晨雪无痕 原创: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 0char 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;// 16out << line2;// 16out.close();}#elif 0// text filechar 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-rwxXStint 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 filelong 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 readchar * 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 writeconst 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  6unsigned 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"#endifreturn 0;}



0 0