读写文件精华

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;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值