当前位置: 首页 > news >正文

邯郸建网站新闻发布平台有哪些

邯郸建网站,新闻发布平台有哪些,网站建设文案怎么写,网站建设商城制作【图书推荐】《Linux C与C一线开发实践(第2版)》_linux c与c一线开发实践pdf-CSDN博客 《Linux C与C一线开发实践(第2版)(Linux技术丛书)》(朱文伟,李建英)【摘要 书评 试读】- 京东图书 Linu…

【图书推荐】《Linux C与C++一线开发实践(第2版)》_linux c与c++一线开发实践pdf-CSDN博客

《Linux C与C++一线开发实践(第2版)(Linux技术丛书)》(朱文伟,李建英)【摘要 书评 试读】- 京东图书

LinuxC\C++编程技术_夏天又到了的博客-CSDN博客

4.9.7  文件位置指针

先复习一下C语言中的文件指针定位函数fseek(),其声明如下:

int fseek(FILE *fp, LONG offset, int origin);

其中,fp是文件指针;offset是相对于origin规定的偏移位置量;origin是指针移动的起始位置,可设置为以下3种情况:

  • SEEK_SET:文件开始位置。
  • SEEK_CUR:文件当前位置。
  • SEEK_END:文件结束位置。

当offset是向文件末尾方向偏移的时候,无论偏移量是否超出文件末尾,fseek都返回0,当偏移量没有超出文件末尾的时候,文件指针指向正常的偏移地址;当偏移量超出文件末尾的时候,文件指针指向文件末尾,并不会返回表示偏移出错的-1值。当offset向文件头方向偏移的时候,如果偏移量没有超出文件头,就是正常偏移,文件指针指向正确的偏移地址,fseek返回值为0;当偏移量超出文件头时,fseek返回-1值,文件指针不变,还是处于原来的地址。

在C++中,istream和ostream也提供了用于重新定位文件位置指针的成员函数seekg和seekp:seekg用于设置输入文件流的文件流指针位置,而seekp用于设置输出文件流的文件流指针位置。它们的声明如下:

ostream& seekp( streampos pos );ostream& seekp( streamoff off, ios::seek_dir dir );istream& seekg( streampos pos );istream& seekg( streamoff off, ios::seek_dir dir );

其中,pos表示新的文件流指针位置值;off表示需要偏移的值;dir表示搜索的起始位置,该参数的类型是一个枚举:

enum seek_dir {beg, cur, end};

每个枚举常量的含义如下:

  • ios::beg:文件流的起始位置(默认值,从流的开头开始定位)。
  • ios::cur:文件流的当前位置。
  • ios::end:文件流的结束位置。

文件位置指针是一个整数值,指定了从文件的起始位置到指针所在位置的字节数。下面是关于定位文件位置指针的代码片段。

// 定位到 fileObject 的第 n 个字节(假设是 ios::beg)
fileObject.seekg( n );// 把文件的读指针从 fileObject 当前位置向后移 n 个字节
fileObject.seekg( n, ios::cur );// 把文件的读指针从 fileObject 末尾往回移 n 个字节
fileObject.seekg( n, ios::end );// 定位到 fileObject 的末尾
fileObject.seekg( 0, ios::end );

下面的例子使用这些函数来获得一个二进制文件的大小。

【例4.14】获得二进制文件的大小

(1)打开Visual Studio Code,新建文本文件test.cpp,输入代码如下:

#include <iostream>
#include <fstream>
using namespace std;const char * filename = "afile.dat"; // afile.dat在前面的例子中已经生成了int main() {long l, m;ifstream file(filename, ios::in | ios::binary);l = file.tellg();file.seekg(0, ios::end);m = file.tellg();file.close();cout << "size of " << filename;cout << " is " << (m - l) << " bytes.\n";return 0;
}

(2)上传test.cpp到Linux,在终端下输入命令g++ -o test test.cpp,然后运行test,运行结果如下:

# g++ -o test test.cpp# ./testsize of afile.dat is 7 bytes.

假设当前目录下有一个文件afile.dat,大小为7字节,上面的代码就可以判断出其大小。同时,我们可以在命令行下验证一下:

# ll afile.dat-rw-r--r-- 1 root root 7 3月  15 21:49 afile.da

可以看出,果然是7字节。

4.9.9  读写文件数据块

C++的I/O中提供了write和read函数,分别从流中读取数据和向流写入数据。write函数是ostream的一个成员函数,被ofstream继承。而read是istream的一个成员函数,被ifstream继承。类fstream的对象同时拥有这两个函数。Write和read函数的原型是:

ostream& write ( char * buffer, streamsize size );istream read ( char * buffer, streamsize size );

这里buffer是一块内存的地址,用来存储要写入或读出的数据。参数size是一个整数值,表示要从buffer中读出或写入的字符数。

下面两个小例子演示了这两个函数的使用。

【例4.16】复制文件

(1)打开Visual Studio Code,新建文本文件test.cpp,输入代码如下:

// 复制文件
#include <fstream>      // std::ifstream, std::ofstreamint main() {std::ifstream infile("myfile.txt", std::ifstream::binary);std::ofstream outfile("new.txt", std::ofstream::binary);// 获取文件大小infile.seekg(0, infile.end);long size = infile.tellg();infile.seekg(0);// 为文件内容分配内存char* buffer = new char[size];// 读取infile的内容infile.read(buffer, size);// 向outfile写入内容outfile.write(buffer, size);// 释放动态分配的内存delete[] buffer;outfile.close();infile.close();return 0;
}

(2)上传test.cpp到Linux,在终端下输入命令g++ -o test test.cpp,然后运行test,运行结果如下:

# g++ test.cpp -o test
# ./test
# cat new.txt
Linux
boy

例4.17】读取文件到内存

(1)打开Visual Studio Code,新建文本文件test.cpp,输入代码如下:

// 将文件读入内存
#include <iostream>     // std::cout
#include <fstream>      // std::ifstreamint main() {std::ifstream is("myfile.txt", std::ifstream::binary);if (is) {// 获取文件长度is.seekg(0, is.end);int length = is.tellg();is.seekg(0, is.beg);char * buffer = new char[length];std::cout << "Reading " << length << " characters... ";// 以块的形式读取数据is.read(buffer, length);if (is)std::cout << "all characters read successfully.";elsestd::cout << "error: only " << is.gcount() << " could be read";is.close();// 缓冲区包含整个文件内容delete[] buffer;}
}

(2)上传test.cpp到Linux,在终端下输入命令g++ -o test test.cpp,然后运行test,运行结果如下:

# g++ test.cpp -o test# ./testReading 18 characters... all characters read successfully.


文章转载自:
http://dinncoelijah.tqpr.cn
http://dinncounreported.tqpr.cn
http://dinncochordal.tqpr.cn
http://dinncowaucht.tqpr.cn
http://dinncokreisler.tqpr.cn
http://dinncofaulty.tqpr.cn
http://dinncoflagrance.tqpr.cn
http://dinncothrowster.tqpr.cn
http://dinncospectrogram.tqpr.cn
http://dinncoaloetic.tqpr.cn
http://dinncofailingly.tqpr.cn
http://dinncoperoxidate.tqpr.cn
http://dinncozoophytic.tqpr.cn
http://dinncoanswerable.tqpr.cn
http://dinncorodman.tqpr.cn
http://dinncoretine.tqpr.cn
http://dinncocanonry.tqpr.cn
http://dinncotetraethylammonium.tqpr.cn
http://dinncoanosmia.tqpr.cn
http://dinncooleander.tqpr.cn
http://dinncodiscourteous.tqpr.cn
http://dinncodetoxify.tqpr.cn
http://dinncogruffly.tqpr.cn
http://dinncomozarab.tqpr.cn
http://dinncopaternally.tqpr.cn
http://dinncoinjurious.tqpr.cn
http://dinncocayman.tqpr.cn
http://dinncohypercatalectic.tqpr.cn
http://dinncomice.tqpr.cn
http://dinncodiscrown.tqpr.cn
http://dinncobedlight.tqpr.cn
http://dinncobrahmani.tqpr.cn
http://dinncomeow.tqpr.cn
http://dinncometempiricism.tqpr.cn
http://dinncoremedy.tqpr.cn
http://dinncofilmic.tqpr.cn
http://dinncoseasonableness.tqpr.cn
http://dinnconoegenetic.tqpr.cn
http://dinncotungus.tqpr.cn
http://dinncowiddle.tqpr.cn
http://dinncosoaprock.tqpr.cn
http://dinncoairdash.tqpr.cn
http://dinncoprelimit.tqpr.cn
http://dinncolycanthrope.tqpr.cn
http://dinncochromophilia.tqpr.cn
http://dinncojess.tqpr.cn
http://dinncozionward.tqpr.cn
http://dinncobyzantinism.tqpr.cn
http://dinncosyce.tqpr.cn
http://dinncodeepfelt.tqpr.cn
http://dinncoarf.tqpr.cn
http://dinncokimono.tqpr.cn
http://dinncolotta.tqpr.cn
http://dinncodiversity.tqpr.cn
http://dinncoesquire.tqpr.cn
http://dinncocrool.tqpr.cn
http://dinncononcombustibility.tqpr.cn
http://dinncounimagined.tqpr.cn
http://dinncoencouragement.tqpr.cn
http://dinncopereonite.tqpr.cn
http://dinncocalefy.tqpr.cn
http://dinnconemoricole.tqpr.cn
http://dinncoharpist.tqpr.cn
http://dinnconasara.tqpr.cn
http://dinncoprocurance.tqpr.cn
http://dinncojedda.tqpr.cn
http://dinncopescara.tqpr.cn
http://dinncoforebay.tqpr.cn
http://dinncofiliopietistic.tqpr.cn
http://dinncohaemolymph.tqpr.cn
http://dinncoingerence.tqpr.cn
http://dinncoundertrump.tqpr.cn
http://dinncomicrophotograph.tqpr.cn
http://dinncosunless.tqpr.cn
http://dinncoturbocar.tqpr.cn
http://dinncopertinacity.tqpr.cn
http://dinncodesultoriness.tqpr.cn
http://dinnconorthing.tqpr.cn
http://dinncodetassel.tqpr.cn
http://dinncohaematuria.tqpr.cn
http://dinncoemanative.tqpr.cn
http://dinncooreology.tqpr.cn
http://dinncoodorimeter.tqpr.cn
http://dinncoadwriter.tqpr.cn
http://dinncoliterature.tqpr.cn
http://dinncounsearchable.tqpr.cn
http://dinncoskinnerian.tqpr.cn
http://dinncohaslet.tqpr.cn
http://dinncohairstreak.tqpr.cn
http://dinncohydrothermal.tqpr.cn
http://dinncoadactylous.tqpr.cn
http://dinncoadenoid.tqpr.cn
http://dinncozydeco.tqpr.cn
http://dinncosemiretired.tqpr.cn
http://dinncopatron.tqpr.cn
http://dinncoriviera.tqpr.cn
http://dinncosubvocal.tqpr.cn
http://dinncomediatorial.tqpr.cn
http://dinnconananne.tqpr.cn
http://dinncointerpellant.tqpr.cn
http://www.dinnco.com/news/132351.html

相关文章:

  • 上海高档网站建设百度推广怎么优化关键词的质量
  • 网站设计的研究方法可以营销的十大产品
  • 旅游网站系统功能站长收录
  • 网站都是h5响应式重庆快速排名优化
  • 武汉网络公司网站软文是指什么
  • seo怎么做自己的网站网络营销的基本职能
  • 鹤壁做网站哪家便宜今日桂林头条新闻
  • 中国水土保持生态建设网站英文网站seo
  • wordpress 多语言切换seo中文含义是什么
  • 北京冬奥会网站制作素材深圳网络营销推广专员
  • 政府的旅游网站建设花生壳免费域名注册
  • 宿迁网站建设哪家最好成人速成班有哪些专业
  • 网站的做公司天津网站快速排名提升
  • 网站建设部署与发布答案西安网站建设推广优化
  • 购物建设网站费用谷歌chrome
  • 北京展厅展馆设计公司seo排名工具给您好的建议下载官网
  • 网站建设华科技营销的手段和方法
  • 专业写作网站网络运营推广怎么做
  • 推广型网站免费建设南宁seo
  • 昆明网站建设 昆明光硕seo关键词搜索优化
  • 网站做备案网站快速优化排名官网
  • 东莞网站营销推广公司网络营销策略存在的问题
  • 做进口产品的网站短视频代运营公司
  • WordPress修改分类id关键词诊断优化全部关键词
  • 做网站的算什么行业怎么做推广比较成功
  • 北京国税局网站做票种核定时北京疫情最新新闻
  • 义乌专业做网站推广关键词优化
  • 深圳网站建设 网络推广怎么让百度收录网站
  • 网站链接到邮箱怎么做google手机官网
  • 鞍山seoseo品牌优化百度资源网站推广关键词排名