seek_tel.cpp

  name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1194442938015&lmt=1194190197&format=336x280_as&output=html&correlator=1194442937843&url=file%3A%2F%2F%2FC%3A%2FDocuments%2520and%2520Settings%2Flhh1%2F%E6%A1%8C%E9%9D%A2%2FCLanguage.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=583001034.1194442938&ga_sid=1194442938&ga_hid=1942779085&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency"> #include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

void main(int argc, char *argv[])
 {
   if(argc!=4)
    {
      cout << "Usage: change <filename> <byte> <character>" << endl;
      exit(1);
    }
   fstream out(argv[1], ios::in | ios::out | ios::binary);
   if(!out)
    {
      cout << "Cannot open file!";
      exit(1);
    }
   out.seekp(atoi(argv[2]), ios::beg);
   out.put(*argv[3]);
   cout << "Current position is: " << out.tellp() << endl;
   out.close();
 }

`f_test.seek(0)`和`f_test.truncate()`是文件操作中常用的两个函数。 `f_test.seek(0)`的作用是将文件指针移动到文件的开头,即文件偏移量设置为0。这个函数通常用于重复读取文件的场景,使用`seek(0)`函数可以将文件指针重新移动到文件开头,从而重新读取文件。 `f_test.truncate()`的作用是截断文件。这个函数通常用于清空文件内容或者截取文件的一部分。如果不传入参数,则默认截断到文件指针的当前位置。如果传入参数,则会将文件截断到指定长度。 下面是一个示例代码,演示了如何使用这两个函数: ```python # 打开文件 with open('test.txt', 'w') as f_test: # 写入一些数据 f_test.write('Hello, world!\n') f_test.write('How are you?\n') # 将文件指针移动到开头 f_test.seek(0) # 读取文件内容 content = f_test.read() print(content) # 截断文件 f_test.truncate(5) # 将文件指针移动到开头 f_test.seek(0) # 读取文件内容 content = f_test.read() print(content) ``` 在上面的代码中,我们首先打开一个名为`test.txt`的文件,并写入了一些数据。然后使用`seek(0)`函数将文件指针移动到开头,并读取文件的内容。接着使用`truncate(5)`函数将文件截断到5个字节,再次将文件指针移动到开头,并读取文件的内容。最终输出的结果为: ``` Hello, world! How are you? Hello ``` 可以看到,第一次读取文件时,读取了文件的全部内容;第二次读取文件时,由于使用了`truncate(5)`函数,所以只读取到了文件的前5个字节,即`Hello`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值