对于我正在编写的程序,计算文件大小对我来说很有用,我使用iostream的tellg和seekg函数计算文件大小,但这会导致-Wstack-protector发出警告。以下代码重现了“问题”:#includestd::streamsizeget_file_size(std::ifstream&ifs){//line12(inwarning,below)conststd::streamsizestart=ifs.tellg();ifs.seekg(0,std::ios::end);conststd::streamsizeend=ifs.tellg();ifs.seekg(start);r
我正在尝试将24位位图图像转换为灰度图像。#include#include#include#includeusingnamespacestd;classpixel{public:unsignedcharb;unsignedcharg;unsignedcharr;voiddisplay(){cout当我删除couttellg语句时,循环只运行两次!我不明白删除cout语句有什么不同?结果:只有一个像素变为灰度。我在这里找到了我的问题的一个更简单的版本Readingandwritingtofilessimultaneously?但是没有找到解决方案... 最佳答
我正在尝试创建一个直接从原始内存缓冲区读取的istream。我在这里的另一篇文章中找到了一个很好的方法来做到这一点:classmembuf:publicbasic_streambuf{public:membuf(char*p,size_tn){setg(p,p,p+n);}};然后我使用这个membuf创建我的istream:membufmb(dataPointer,dataLength);istreamreader(&mb);然后我使用getline()和>>>运算符阅读,一切都很棒。但是,我似乎无法使用seekg()返回缓冲区的开头,并且istream::tellg()总是返回-1
我有以下令我惊讶的代码(使用libstdc++4.8)...#include#include#includeusingnamespacestd;intmain(){std::strings("somecontent");std::stringstreamss(s,std::ios::in|std::ios::ate);std::istream&file=ss;//ss.clear();Makesnodifference...std::cout...具有以下输出。tellg()pos:0此行为与使用std::ifstream(std::ios::ate)时不同。此行为是否正确/符合预期?
以下代码适用于gcc4.4。但是gcc4.7会给出断言失败。#include#include#includeusingnamespacestd;intmain(){stringinput("abcdefg");stringstreamiss(input);ostringstreamoss;oss在gcc4.7中,如果istream已经到达EOF,tellg()将返回-1。不会调用pubseekoff()和seekoff()在gcc4.4中这不是问题。应该是gcc4.4还是gcc4.7?为什么? 最佳答案 根据C++11第27.7.2
这段代码:#include#include#include#includeintmain(){std::remove("test.txt");std::fstreamf("test.txt",std::ios::in|std::ios::out|std::ios::binary|std::ios::trunc);std::cout>s;std::cout在gcc-4.4.5中给出以下输出14444即tellg和tellp都返回了预期的流位置4。虽然gcc-4.6.0给予:144-14我在哪里可以找到引用来告诉:第一种情况是正确的(gcc-4.6中的错误)第二种情况是正确的(gcc两种情
读取.cpp#include#includeusingnamespacestd;intmain(void){intid;charname[50];ifstreammyfile("savingaccount.txt");//openthefilemyfile>>id;cout储蓄账户.txt1800567HoRuiJang21FemaleMalaysian012-499819220,Lorong13,TamanPataniJanamMelakaSungaiDulong问题我希望tellg()从第一行1800567开始返回7或87位数字,因此流指针应放在此数字之后和字符串"HoRuiJan
这个问题不太可能帮助任何future的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visitthehelpcenter.关闭9年前。我只是想知道为什么成员函数tellg()在basic_istream中定义类和tellp()在basic_ostream中定义类有不同的名称。是因为basic_fstream源自basic_istream和basic_ostream?
我以二进制模式打开了一个文件,进行以下操作得到了x的负值。我打开的文件大小约为2.5GB。infile.seekg(0,ios::end);__int64x=infile.tellg();我需要infile来读取字节(无符号字符),所以我通过以下方式将其定义为uifstream:typedefbasic_ifstream>uifstream;这基本上是一个标准的ifstream,但使用unsignedchars而不是chars。编辑:我正在使用VisualStudio2005并将uofstream更正为uifstream。 最佳答案
我知道这个标题听起来很疯狂,但我现在正在亲body验这一点,我想不出任何失败的原因。我正在使用getline()读取文件在阅读结束时,我调用了tellg()。但是,此调用总是失败(返回值为-1)。tellg()不能与getline()一起使用是已知问题还是我做错了什么?我用的代码很简单,基本上while(getline(file,line)){//tokenizeanddootherthings}cout有问题的文件是普通磁盘上的一个简单的txt文件,我尝试了一个带和不带CRLF的文件,但没有任何区别。编辑:附加信息gcc/g++4.1.2,Linux(RHEL5)编辑2:根据这个线程