今天想写一个多线程读取一个文本文件,需要fstream对象作为函数参数,出现了编译错误,查询了网上很多资料,在此汇总一下。

<span style="font-size:18px;">#include <boost/thread/thread.hpp>
#include <boost/atomic.hpp>
#include <iostream>
#include <time.h>
#include <string>
#include <boost/bind.hpp>
#include <fstream>
using namespace std;
boost::mutex mt;
void Print(fstream fr)
	string sth;
	mt.lock();
	getline(fr, sth);
	cout << sth << endl;
	mt.unlock();
int main()
	fstream fr;
	fr.open("E:\\test\\theap\\image.txt", ios_base::in);
	Print(fr);
	th1.join();
	th2.join();
	return 0;
}</span>
上面的代码编译出错。原因就是fstream 对象没有复制构造函数,因此不能执行复制操作,而函数是实参复制给形参,因此这里就出错。因此,我们必须使用引用的方式传递参数。

子函数修改为:

<span style="font-size:18px;">void Print(fstream& fr)// 此处必须使用引用方式传递参数
	string sth;
	mt.lock();
	getline(fr, sth);
	cout << sth << endl;
	mt.unlock();
}</span>

这样修改后,普通调用就没有问题了!

修改后的完整代码:

<span style="font-size:18px;">#include <boost/thread/thread.hpp>
#include <boost/atomic.hpp>
#include <iostream>
#include <time.h>
#include <string>
#include <boost/bind.hpp>
#include <fstream>
using namespace std;
boost::mutex mt;
void Print(fstream& fr)
	string sth;
	mt.lock();
	getline(fr, sth);
	cout << sth << endl;
	mt.unlock();
int main()
	fstream fr;
	fr.open("E:\\test\\theap\\image.txt", ios_base::in);
	//Print(fr);
	boost::thread th1(boost::bind(Print, fr));
	boost::thread th2(boost::bind(Print, fr));
	th1.join();
	th2.join();
	return 0;
}</span>
我使用了boost::bind(Print,fr),这里又报错了。具体可参考: http://blog.csdn.net/yuan1164345228/article/details/49176029
解决方法就是,使用 用std::ref(或者boost::ref)将fstream对象包装。

std::ref 用于包装按引用传递的值。

std::cref 用于包装按const 引用传递的值。

修改两行代码:
boost::thread th1(boost::bind(Print, boost::ref(fr)));
boost::thread th2(boost::bind(Print, boost::ref(fr)));
Hi I'm a c++ beginner and this is one of my assignments and I'm a bit stuck. This isn't my entire code it's just a snippet of what I need help with. What I'm trying to do is have one function dedicate... fstream 引用作为函数返回值 问题 先贴出的我代码:代码中以o fstream 的引用和一个销售数据类的引用作为入参,函数中返回输出数据后的流引用:o fstream & print(o fstream & o,const Sale_data& rhs) return o<<rhs.isbn()<<":"<<rhs.units_sold<<" "<<rhs.revenue<<" "<<rhs.avg error: no matching function for call to ‘std::basic_i fstream ::open(std::__cxx11::string&, const openmode&)’ 最近感觉C++忘的太多,老喜欢在C++里面使用C风格的代码,现在连 fstream 都不会写了 i fstream fp; string path = "laji.txt";... 文章目录目的前提实现结论 对接自动化交易,多线程下写文件单,用o fstream 追加写的时候,会出现写漏的情况,怀疑是多线程下,两个线程的文件光标位置一样了,导致有一个被覆盖了。 因为根据账号不同写不同的路劲下的文件,所以想到根据账号不同加相应的锁。 没有用文件锁。因为是o fstream ,所以直接用了boost中的递归锁。 大概代码如下: map<string, boost::... ghijklmn 定义out为 fstream 对象 ,与输出文件file2.txt建立关联。当文件打开成功后将file1.txt 文件的内容转换成大写字母,输出到file2.txt文件中。 #include<iostream> using namespace std; #include< fstream > #include<c 库包含了三个基本的类:i fstream , o fstream fstream 。这三个类分别代表一个输入文件,一个输出文件,以及一个输入输出文件。I fstream 类支持>>操作符,o fstream 类支持>和 对象 都能够把一个文件名当成构造函数的变量,并能够自动的打开文件,如: std::o fstream dictionary("myfile.txt"); 类的析构函数自动地添加清除文件 2、o fstream 向一个给定文件写入数据 3、 fstream 可以读写给定文件 fstream 除了继承了iostream类型的行为外,还增加了一些新的功能,所以IO类型不能调用这些新的功能: 1、使用文件流 对象 输入流in用于初始化从文件中读取数据。在C++新标准中,文件名既可以是库类型str... 文件流介绍 在标准模板库中,常见的文件流 对象 fstream 、i fstream 、o fstream 三种,我们可以用文件流的方式去操作文件,比如写文件和读文件,文件流类继承图如下: i fstream 继承于istream,实现高层文件流输入(input)操作,它能读取文件中的数据到变量,可以用于读文件,其默认的openmode是in。 o fstream 继承于ostream,实现高层文件流输出(... ■p33,L4读者来函: 我买了先生出版的 Essential C++ 和 C++ Primer,其中有一段程式码是这样的 (在 C++ Primer 1102页,和 Essential C++ 1.7节内) #include using namespace std; void main() { ... Ubuntu下用gcc和makefile编译C语言 在Windows环境下通过虚拟机软件(我用的是VMware)安装Ubuntu 18.04 Desktop,并通过Vim编写C语言,再用gcc和makefile分别编译,并作出区别解析。 编写C文件 用 fstream 的read函数读取结构体时,只有结构体中第一个数据变量读取正确,后面的所有数据变量的值都读取错误。 上述 问题 是单字节内存对齐影响的结果 在结构体开头和结尾加上两行代码即可: