讀寫文件精華

ASCII   輸出:
ofstream fout;
fout.open("output.txt");
//ofstream fout("output.txt");
int num = 150;
char name[] = "John Doe";
fout << "Here is a number: " << num << "/n";
fout << "Now here is a string: " << name << "/n";
fout << flush;
fout.close();
//Here is a number: 150 Now here is a string: John Doe


ASCII  輸入:
//12 GameDev 15.45 L This is really awesome!
ifstream fin("input.txt");
int number;
float real;
char letter, word[8];
fin >> number; fin >> word; fin >> real; fin >> letter;
//fin >> number >> word >> real >> letter;
文件的每個空白之後, ">>" 操作符會停止讀取內容, 直到遇到另一個>>操作符. 因爲我們讀取的每一行都被換行符分割開(是空白字符), ">>" 操作符只把這一行的內容讀入變量。這就是這個代碼也能正常工作的原因。
如果你想把整行讀入一個char數組, 我們沒辦法用">>"?操作符,因爲每個單詞之間的空格(空白字符)會中止文件的讀取。爲了驗證:
fin.getline(sentence, 100);

二進制 輸入輸出
ofstream fout("file.dat", ios::binary);
int number = 30; fout.write((char *)(&number), sizeof(number));
二進制文件最好的地方是可以在一行把一個結構寫入文件。 如果說,你的結構有12個不同的成員。 用ASCII?文件,你不得不每次一條的寫入所有成員。 但二進制文件替你做好了。 看這個。

struct OBJECT { int number; char letter; } obj;
obj.number = 15;
obj.letter = ‘M’;
fout.write((char *)(&obj), sizeof(obj));
  這樣就寫入了整個結構! 接下來是輸入. 輸入也很簡單
ifstream fin("file.dat", ios::binary); fin.read((char *)(&obj), sizeof(obj));

 

更多方法
 

檢查文件
你已經學會了open() 和close() 方法, 不過這裏還有其它你可能用到的方法。
方法good() 返回一個布爾值,表示文件打開是否正確。
類似的,bad() 返回一個布爾值表示文件打開是否錯誤。 如果出錯,就不要繼續進一步的操作了。
最後一個檢查的方法是fail(), 和bad()有點相似, 但沒那麼嚴重。

讀文件
方法get() 每次返回一個字符。
方法ignore(int,char) 跳過一定數量的某個字符, 但你必須傳給它兩個參數。第一個是需要跳過的字符數。 第二個是一個字符, 當遇到的時候就會停止。 例子,

fin.ignore(100, ‘/n’);
會跳過100個字符,或者不足100的時候,跳過所有之前的字符,包括 ‘/n’。
方法peek() 返回文件中的下一個字符, 但並不實際讀取它。所以如果你用peek() 查看下一個字符, 用get() 在peek()之後讀取,會得到同一個字符, 然後移動文件計數器。
方法putback(char) 輸入字符, 一次一個, 到流中。我沒有見到過它的使用,但這個函數確實存在。

寫文件
只有一個你可能會關注的方法.?那就是 put(char), 它每次向輸出流中寫入一個字符。

打開文件
當我們用這樣的語法打開二進制文件:

ofstream fout("file.dat", ios::binary);
  "ios::binary"是你提供的打開選項的額外標誌. 默認的, 文件以ASCII方式打開, 不存在則創建, 存在就覆蓋. 這裏有些額外的標誌用來改變選項。

ios::app 添加到文件尾
ios::ate 把文件標誌放在末尾而非起始。
ios::trunc 默認. 截斷並覆寫文件。
ios::nocreate 文件不存在也不創建。
ios::noreplace    文件存在則失敗。

文件狀態
  我用過的唯一一個狀態函數是eof(), 它返回是否標誌已經到了文件末尾。 我主要用在循環中。 例如, 這個代碼斷統計小寫‘e’ 在文件中出現的次數。

ifstream fin("file.txt");
char ch; int counter;
while (!fin.eof()) {
      ch = fin.get();
      if (ch == ‘e’) counter++;
}
fin.close();
  我從未用過這裏沒有提到的其他方法。 還有很多方法,但是他們很少被使用。參考C++書籍或者文件流的幫助文檔來了解其他的方法。

結論
  你應該已經掌握瞭如何使用ASCII文件和二進制文件。有很多方法可以幫你實現輸入輸出,儘管很少有人使用他們。 我知道很多人不熟悉文件I/O操作,我希望這篇文章對你有所幫助。 每個人都應該知道. 文件I/O還有很多顯而易見的方法,?例如包含文件 <stdio.h>. 我更喜歡用流是因爲他們更簡單。 祝所有讀了這篇文章的人好運, 也許以後我還會爲你們寫些東西。 
 
特殊
 get()成員函數會在文件讀到默尾的時候返回假值,所以我們可以利用它的這個特性作爲while循環的終止條件,


 string s;
 getline( cin, s );
cout 
<< "You entered " << s << endl;

//s input to buffer .
in:123 456 789
out:you entered 123 456 789 


char c[10];

   cin.getline( 
&c[0], 5'' );
   cout 
<< c << endl;

// max is 5, when  'a  '(<5) is come , input is over .




getline ();



there 
is a way to read in the whole line, and it is the method getline(). This is how we would do it.


fin.getline(sentence, 
100);


Here are the parameters to the function. The first parameter 
is obviously the char array we want to read in to. The second is the maximum number of characters we will read in until we encounter a new line. So now sentence contains "This is really awesome!" just like we wanted.



memset()
///Sets buffers to a specified character.

char buffer[] = "This is a test of the memset function";

   printf( "Before: %s/n", buffer );
   memset( buffer, '*', 4 );
   printf( "After:  %s/n", buffer );

 

 

 //char buf[30];
 //memset( buf, '/0', 30 );   //set '/0' to buffer   
 //string str = "Trying is the first step towards failure.";
 //str.copy( buf, 1 );
 //cout << buf << endl;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章