C++檔案讀寫詳解(ofstream,ifstream,fstream)
記錄學習心得,資料轉自:晨雪無痕 原創: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;
}
相關文章
- fstream中ifstream和ofstream的簡單用法
- (轉載)C++ ofstream和ifstream詳細用法C++
- 入門計劃->使用(C++庫)fstream讀寫檔案 (轉)C++
- 使用ifstream和getline讀取檔案內容[c++]C++
- 入門計劃->使用(C++庫)ifstream讀檔案資料 (轉)C++
- C++檔案讀寫C++
- C++讀寫檔案C++
- 入門計劃->使用(C++庫)ofstream寫檔案資料 (轉)C++
- 詳解python檔案讀寫操作Python
- C++讀寫檔案操作C++
- C++檔案讀寫操作C++
- C/C++ 檔案讀寫C++
- C++檔案讀寫總結C++
- Flutter 檔案讀寫---path_provider詳解FlutterIDE
- PHP檔案讀寫鎖的問題詳解PHP
- C++ 中檔案流(fstream)的使用方法及示例C++
- C++ 檔案操作詳解C++
- 【C++基礎】檔案流讀寫操作C++
- C/C++中檔案的讀寫格式C++
- C++學習筆記----讀寫檔案C++筆記
- Python檔案讀寫詳解及設定檔案的字元編碼Python字元
- std::ofstream由二進位制流寫檔案的問題
- 使用C++讀寫TDM以及TDMS檔案薦C++
- C++中對檔案進行讀寫操作C++
- 檔案讀寫
- php讀取大檔案詳解PHP
- C/C++ 二進位制讀寫 png 檔案C++
- 瞭解如何用 Bash 讀寫檔案
- C++寫檔案操作C++
- 檔案排版(文字檔案讀寫)
- Golang 讀、寫檔案Golang
- keras讀寫檔案Keras
- perl 讀寫檔案
- 檔案讀寫IO
- 檔案的讀寫
- Java C++ 讀寫檔案大小端格式是不同的JavaC++
- API讀取寫入 ini檔案內容的方法函式詳解API函式
- C++檔案操作實戰:建立、寫入、讀取、修改檔案一應俱全C++