文件管理 · 2022年8月8日

fstream读二进制文件|C++中如何利用<fstream>从txt文件中读入和输出完整的一句话

A. C++中如何利用<fstream>从txt文件中读入和输出完整的一句话

#include<iostream>#include<fstream>#include<string>using namespace std;int main(){ const char* filename="C:\\1.txt";//打开c盘根目录下的1.txt文件,请确保该文件存在 ifstream in(filename,ios::in);//创建输入流 if(!in) { cout<<"打开文件失败!\n"; system("pause"); return 0; } while(!in.eof())//读文件 { string words; in>>words; cout<<words<<endl;//打印读取的内容 } system("pause"); return 0;}

B. 用fstream把一个结构体用二进制存进文件,但是读取时失败。

student是个类,这样存取应该是不行的,类的sizeof也是个很不精确的概念关于类的流化,MFC有自己专门的办法。当然你也可以自己把类内部的数据保存到一个文件中,然后读取的时候先创建类,再读取数据赋值进去。

C. c++ fstream 程序不能正常的读写二进制

这个结构体有问题struct User{ int num; string name;};你把string name 改成 char name[20];数组长度你自己确定就好

D. 如何使用std ifstream读取一个带有宽字符串路径的二进制文件

比如int,也许有四个字节,我们只需要把它的地址换成char×,并且写入4个字节就行了,读出也是一样的。代码[cpp] view plain #include <iostream> #include <fstream> using namespace std; int main(int argc, char** argv) { int a[5] = {1,2,3,4,5}; int b[5]; ofstream ouF; ouF.open("./me.dat", std::ofstream::binary); ouF.write(reinterpret_cast<const char*>(a), sizeof(int)*5); ouF.close(); ifstream inF; inF.open("./me.dat", std::ifstream::binary); inF.read(reinterpret_cast<char*>(b), sizeof(int)*5); inF.close(); for (int i = 0; i < 5; i++) { cout << b[i] << endl; } return 0; }

E. C++二进制写入和读入怎么实现呢

#include <fstream>写二进制文件应该使用ofstream类,文件的打开模式一定要是 binary,如果传入的不是 binary, 文件将以ASCII方式打开。下面是示例代码,用于写入文件。std::ofstream fout("a.dat", std::ios::binary);int nNum = 20;std::string str("Hello, world");fout.write((char*)&nNum, sizeof(int));fout.write(str.c_str(), sizeof(char) * (str.size()));fout.close();而写文本文件则比较简单,如下:std::ofstream fout("b.dat");int nNum = 20;std::string str("Hello, world");fout << nNum <<","<< str << std::endl;fout.close();读二进制文件读取二进制文件可以使用ifstream 类来进行,文件的打开模式一定要是 binary,如果传入的不是 binary, 文件将以ASCII方式打开。下面是示例代码:std::ifstream fin("a.dat", std::ios::binary);int nNum;char szBuf[256] = {0};fin.read((char*)&nNum, sizeof(int));fin.read(szBuf, sizeof(char) * 256);std::cout <<"int = "<< nNum << std::endl;std::cout <<"str = "<< szBuf << std::endl;fin.close();

F. C++读取一个未知大小和结构的二进制文件

#include <fstream> using namespace std;…// AuthInfo 是自定义的 struct struct AuthInfo auth_info; string susername, spassword;/* 写文件 */ // 清零 ZeroMemory ( &auth_info, sizeof ( auth_info ) );susername = "[email protected]"; spassword = "000000";// 内存拷贝 memcpy(auth_info.username, susername.c_str(), susername.length()); memcpy(auth_info.password, spassword.c_str(), spassword.length());// 定义打开输出流 ofstream fout("mbc.dat", ios::binary);// 写入 fout.write((char *)(&auth_info), sizeof(auth_info));// 关闭输出流 fout.close();/* 读文件 */ ZeroMemory ( &auth_info, sizeof ( auth_info ) );ifstream fin ( "mbc.dat", ios::binary );fin.read((char *)(&auth_info), sizeof(auth_info));susername = auth_info.username; spassword = auth_info.password;ZeroMemory ( auth_info.username, 100 ); // AuthInfo.username[100] ZeroMemory ( auth_info.password, 50 ); // AuthInfo.password[50]memcpy(auth_info.username, susername.c_str(), susername.length()); memcpy(auth_info.password, spassword.c_str(), spassword.length()); fin.close();

G. 怎样用fstream对二进制文件进行内容修改和删除

fstream f; f.open("1.txt", ios::in | ios::binary); if (!f.is_open()) // 检查文件是否成功打开 cout << "cannot open file." << endl; ios::in与ios::bianry均为int型,定义文件打开的方式。 ios::in — 打开文件用于读。 ios::out — 打开文件用于写,如果文件不存在,则新建一个;存在则清空其内容。 ios::binary — 以二进制bit流方式进行读写,默认是ios::text,但最好指定这种读写方式,即使要读写的是文本。因为在ios::text模式下,在写入时’\ n’字符将转换成两个字符:回车+换行(HEX: 0D 0A) 写入,读入时作逆转换,这容易引起不必要的麻烦。ios::app — 打开文件在文件尾进行写入,即使使用了seekp改变了写入位置,仍将在文件尾写入。 ios::ate — 打开文件在文件尾进行写入,但seekp有效。 读写位置的改变 f.seekg(0, ios::beg); // 改变读入位置 g mean Get f.seekp(0, ios::end); // 改变写入位置 p mean Put 第一个参数是偏移量offset(long),第二个参数是offset相对的位置,三个值: ios::beg — 文件头 ios::end — 文件尾 ios::cur — 当前位置 文件读写 char s[50]; f.read(s, 49); s[50] = ’\0’; // 注意要自己加上字符串结束符 char *s = "hello"; f.write(s, strlen(s)); 补充 记得读写完成后用f.close()关闭文件。 例子 下面的程序用于删除带有行号的源程序中的行号。#include <iostream> #include <fstream> using namespace std; //定义要删除的行号格式,下面定义的是型如: #0001 的行号 const int LINE_NUM_LENGTH = 5; const char LINE_NUM_START = ’#’; int main(int argc, char *argv[]) { fstream f; char *s = NULL; int n; for (int i = 1; i < argc; i++) { cout << "Processing file " << argv[i] << "……"; f.open(argv[i], ios::in | ios::binary); if (!f.is_open()){ cout << "CANNOT OPEN"<< endl; continue; } f.seekg(0, ios::end); n = f.tellg(); // 文件大小 s = new char[n+1]; f.seekg(0, ios::beg); f.read(s, n); s[n] = ’\0’; f.close(); // 采用一种简单的判断,遇到LINE_NUM_START后接一个数字, // 则认为它是一个行号. for (int j = 0; j < n; j++) { if (s[j] == LINE_NUM_START && (s[j+1] >= ’0’ && s[j+1] <= ’9’)) { for (int k = j; k < j + LINE_NUM_LENGTH; k++) s[k] = ’ ’; } } f.open(argv[i], ios::out | ios::binary); if (!f.is_open()) { cout << "CANNOT OPEN" << endl; delete[] s; continue; } f.write(s, n); f.close(); cout << "OK" << endl; delete[] s; } return 0;

H. C++中怎样进行二进制文件的读写

#include<fstream>写二进制文件应该使用ofstream类,文件的打开模式一定要是binary,如果传入的不是binary,文件将以ASCII方式打开。下面是示例代码,用于写入文件。std::ofstreamfout("a.dat",std::ios::binary);intnNum=20;std::stringstr("Hello,world");fout.write((char*)&nNum,sizeof(int));fout.write(str.c_str(),sizeof(char)*(str.size()));fout.close();而写文本文件则比较简单,如下:std::ofstreamfout("b.dat");intnNum=20;std::stringstr("Hello,world");fout<<nNum<<","<<str<<std::endl;fout.close();读二进制文件读取二进制文件可以使用ifstream类来进行,文件的打开模式一定要是binary,如果传入的不是binary,文件将以ASCII方式打开。下面是示例代码:std::ifstreamfin("a.dat",std::ios::binary);intnNum;charszBuf[256]={0};fin.read((char*)&nNum,sizeof(int));fin.read(szBuf,sizeof(char)*256);std::cout<<"int="<<nNum<<std::endl;std::cout<<"str="<<szBuf<<std::endl;fin.close();二进制文件是指包含在ASCII及扩展ASCII字符中编写的数据或程序指令的文件。计算机文件基本上分为二种:二进制文件和ASCII(也称纯文本文件),图形文件及文字处理程序等计算机程序都属于二进制文件,这些文件含有特殊的格式及计算机代码。ASCII则是可以用任何文字处理程序阅读的简单文本文件。简单的说,如果一个文件专门用于存储文本字符的数据,没有包含字符以外的其他数据,我们就称之为文本文件,除此之外的文件就是二进制文件。广义的二进制文件即指文件,由文件在外部设备的存放形式为二进制而得名。狭义的二进制文件即除文本文件以外的文件。文本文件是一种由很多行字符构成的计算机文件。文本文件存在于计算机系统中,通常在文本文件最后一行放置文件结束标志。文本文件的编码基于字符定长,译码相对要容易一些;二进制文件编码是变长的,灵活利用率要高,而译码要难一些,不同的二进制文件译码方式是不同的。从本质上来说他们之间没有什么区别,因为他们在硬盘上都有一种的存放方式–二进制,但是如果要对他们有些区分的话,那可以这样理解。每个字符由一个或多个字节组成,每个字节都是用的-128—127之间的部分数值来表示的,也就是说,-128——127之间还有一些数据没有对应任何字符的任何字节。如果一个文件中的每个字节的内容都是可以表示成字符的数据,我们就可以称这个文件为文本文件,可见,文本文件只是二进制文件中的一种特例,为了与文本文件相区别,人们又把除了文本文件以外的文件称为二进制文件,由于很难严格区分文本文件和二进制文件的概念,所以我们可以简单地认为,如果一个文件专门用于存储文本字符的数据,没有包含字符以外的其他数据,我们就称之为文本文件,除此之外的文件就是二进制文件。

I. C++读取二进制文件问题

c/c++从来是这样,刚好读到文件尾的时候不会有任何反应在文件末端再读取一次,读取失败的情况下in.eof()会返回真,c的FILE也是同样的情况你可以这么读#include<fstream>#include<iostream>usingnamespacestd;intmain(){ifstreamin;in.open("book.dat",ios_base::in|ios_base::binary);charc;while(1){in>>c;//读取if(in.eof())break;//说明上次读取无效cout<<c;//有效,}//下面这段代码会多输出一次c,就是因为已经到文件尾了eof也不会返回真/*while(!in.eof()){in>>c;cout<<c;}*/}或者这么读#include<fstream>#include<iostream>usingnamespacestd;intmain(){ifstreamin;in.open("book.dat",ios_base::in|ios_base::binary);charbuffer[4];//假如二进制文件全是4字节数据;那么每次读一个4字节块intnRead;while(nRead=in.read(buffer,sizeof(buffer)).gcount())if(sizeof(buffer)!=nRead){cout<<"incorrectlength"<<endl;break;}else;//处理}

J. 如何快速读取二进制文件

快速读取二进制文件的方法如下:1、打开文件。打开文件可以有两种方式,第一种可以使用fstream类的构造函数。fstream file("test.dat",ios_base::in|ios_base::out|ios_base::app); 另外一种方法就是使用open函数。fstream file;file.open("test.dat",ios_base::in|ios_base::out|ios_base::app);这样就可以打开一个可读写的文件了。如果文件不存在的话,就会创建一个新文件并且以读写方式打开。这里需要说明一点,如果文件不存在的话,open函数中第二个参数必须包含ios_base::out|ios_base::app,否则就不能正确创建文件。2、写文件。先进性写文件的操作否则读一个空文件是没有意义的。既然是写二进制文件可以向文件中写入一个整形值。写二进制字符只能使用write函数。但是write函数的原形是write(const char * ch, int size)。第一个参数是char *类型,所以需要把将要写入文件的int类型转换成char *类型。这里的转换困扰了我好几天,不过终于弄明白了。代码如下。int temp; file.write((char *)(&temp),sizeof(temp));3、读文件。可以写文件了,读文件就好办多了。读文件需要用到read函数。其参数和write大致相同,read(const char * ch, int size)。要把内容读到int类型变量中同样涉及到一个类型转换的问题。和写文件一样。int readInt; file.read((char(&readInt),sizeof(readInt)); 这样文件中的int值就读入到int型变量readInt中了。4、文件指针。在文件的读写过程中往往需要对文件进行选择性读取。所以需要进行文件指针的移动。这是需要用到seekg和seekp函数。在fstream类中有两个文件指针,一个是读取文件的指针,一个是写文件的指针分别用tellg和tellp文件来取得指针的位置。同样seekg和seekp两个函数分别是对这两个指针进行移动的函数。这两个函数的参数都是一样的。先对几个枚举类型进行一下说明:ios_base::beg ——文件开始位置ios_base::cur ——文件当前位置ios_base::end ——文件末尾位置下面以seekg为例说明一下指针移动的方法:file.seekg(3) ——指针移动到第三个字符的位置file.seekg(ios_base::beg) ——指针移动到文件开头file.seekg(ios_base::end) ——指针移动到文件末尾file.seekg(-3,ios_base::cur) ——指针当前位置向前移动三个字符file.seekg(3,ios_base::cur) ——指针当前位置向后移动三个字符file.seekg(3,file.tellg()) ——指针当前位置向后移动三个字符file.seekg(file.tellg()+3) ——指针当前位置向后移动三个字符5、对文件操作完毕后别忘了关闭文件。