C++ 无法在c+中完全读取文件+;?
我有以下从文件读取的代码C++ 无法在c+中完全读取文件+;?,c++,file,C++,File,我有以下从文件读取的代码 我在 文件中字符串的大小大于113,但小于代码打印的113 为什么 #include <iostream> #include <sstream> #include <fstream> #include <string> #include <bitset> #include <stdlib.h> using namespace std; int main() { ifstream inf("
我在
文件中字符串的大小大于113,但小于代码打印的113
为什么强>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <bitset>
#include <stdlib.h>
using namespace std;
int main()
{
ifstream inf("Sample.txt");
string str;
string file_contents ="";
while ( getline(inf, str ) )
{
file_contents += str;
file_contents.push_back('\n');
}
cout << file_contents.size() << endl; // print 113
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream-inf(“Sample.txt”);
字符串str;
字符串文件_contents=“”;
while(getline(inf,str))
{
文件内容+=str;
文件内容。推回('\n');
}
你可以一行一行地阅读,用'\n'
将它们连接在一起,但是在你的环境中可能有一种二进制到文本的转换:在文本文件上,某些操作系统用CR/LF(windows)或LF/CR(mac OS)序列表示“新行”,而不仅仅是LF(linux)
这会导致文件比实际读取的文件长。您逐行读取,并使用'\n'
将它们连接在一起,但在您的环境中可能存在二进制到文本的转换:在文本文件上,某些操作系统使用CR/LF(windows)或LF/CR(mac OS)序列表示“新行”,而不仅仅是LF(linux)
这会导致文件比实际读取的文件长。如果您想一点一点地读取它,这不是最好的方法
您可能想尝试使用ifstream read
#include <iostream>
#include <string>
#include <fstream>
#include <ios>
using namespace std;
//takes char to write byte to, and the current stream
//returns true if successfully read, else return false (aka. no more bytes to read)
bool readChar(unsigned char &r,ifstream &data){
if(data.read((char*)&r,sizeof(unsigned char))){
return true;
}
return false;
}
int main(){
ifstream sampleStream("Sample.txt",ios::binary|ios::in);
unsigned char tmp;
int byteCount = 0;
while (readChar(tmp,sampleStream))
{
byteCount++;
//If you'd like to read bits, use some bit masking here over tmp
//and iterate over bits
}
cout<<byteCount<<endl; // yields 6715
}
#包括
#包括
#包括
#包括
使用名称空间std;
//获取要写入字节的字符和当前流
//如果成功读取,则返回true,否则返回false(即,没有更多字节可读取)
bool readChar(无符号字符&r、ifstream和数据){
if(data.read((char*)&r,sizeof(unsigned char))){
返回true;
}
返回false;
}
int main(){
ifstream-sampleStream(“Sample.txt”,ios::binary | ios::in);
无符号字符tmp;
int字节数=0;
while(readChar(tmp、sampleStream))
{
字节计数++;
//如果您想读取位,请在tmp上使用一些位屏蔽
//并对位进行迭代
}
cout如果你想一点一点地阅读,这不是最好的方式
您可能想尝试使用ifstream read
#include <iostream>
#include <string>
#include <fstream>
#include <ios>
using namespace std;
//takes char to write byte to, and the current stream
//returns true if successfully read, else return false (aka. no more bytes to read)
bool readChar(unsigned char &r,ifstream &data){
if(data.read((char*)&r,sizeof(unsigned char))){
return true;
}
return false;
}
int main(){
ifstream sampleStream("Sample.txt",ios::binary|ios::in);
unsigned char tmp;
int byteCount = 0;
while (readChar(tmp,sampleStream))
{
byteCount++;
//If you'd like to read bits, use some bit masking here over tmp
//and iterate over bits
}
cout<<byteCount<<endl; // yields 6715
}
#包括
#包括
#包括
#包括
使用名称空间std;
//获取要写入字节的字符和当前流
//如果成功读取,则返回true,否则返回false(即,没有更多字节可读取)
bool readChar(无符号字符&r、ifstream和数据){
if(data.read((char*)&r,sizeof(unsigned char))){
返回true;
}
返回false;
}
int main(){
ifstream-sampleStream(“Sample.txt”,ios::binary | ios::in);
无符号字符tmp;
int字节数=0;
while(readChar(tmp、sampleStream))
{
字节计数++;
//如果您想读取位,请在tmp上使用一些位屏蔽
//并对位进行迭代
}
你真的分配了一个无符号字符吗
?我编辑了这个问题,请重新打开它。你真的分配了一个无符号字符吗
?我编辑了这个问题,请重新打开它。那么我如何读取它呢?我想读取所有内容文件,并将它们放在一个字符串中。以“二进制”形式读取它抑制翻译:尝试以std::ifstream-inf(“13886560748.txt”,std::ios::binary)”的形式打开它;
那么我如何读取它呢?我想读取所有内容文件并将它们放在一个字符串中。以“binary”的形式读取它抑制翻译:尝试以std::ifstream-inf(“13886560748.txt,std::ios::binary”)的形式打开它
我看到问题现在已编辑,无论如何,如果您想“一点一点地读取文件”,这可能会很有用。确定已编辑,因为避免关闭它。+1和谢谢…我看到问题现在已编辑,无论如何,如果您想“一点一点地读取文件”,这可能会很有用。确定已编辑,因为避免关闭它。+1和谢谢。。。