Hi,
I tried to use boost/thread library to write a program to read a large text
file. The program compiled but did not get the right answer. I am not sure
my usage of the library is correct. I appreciate your helps.
Here are the code:
#include <iostream>
#include <fstream>
#include <string>
#include
AMDG Gang Ma wrote:
infile.seekg(linenum,ios::beg); <snip>
I would like thread1 read linie 0-4 and thread2 read line 5-9. My dummy test file has 10 lines, each line is 1,2, 3...,10 etc.
seekg takes the number of bytes to move the read position. You can't find the beginning of the nth line of a text file without reading the file and counting new line characters. In Christ, Steven Watanabe
"Gang Ma"
Hi, I tried to use boost/thread library to write a program to read a large text file. The program compiled but did not get the right answer. I am not sure my usage of the library is correct. I appreciate your helps. Here are the code:#include <iostream>#include <fstream>#include <string>#include
#include #include #include //#include "stdafx.h"boost::mutex io_mutex;using namespace std;void mtread(char* filename,const int stidx, const int edidx,int id) { fstream infile; infile.open(filename); string line; if (infile.good()){ for (int linenum=stidx; linenum<=edidx;linenum++) { boost::mutex::scoped_lock lock(io_mutex); infile.seekg(linenum,ios::beg); getline(infile,line); cout<
This looks to me like an IO problem, not a thread problem. Replace your threads with direct calls to the functions, and I expect you'll see the same problem. seekg does not seek to a line, it seeks to a byte in the file. Anthony -- Anthony Williams | Just Software Solutions Ltd Custom Software Development | http://www.justsoftwaresolutions.co.uk Registered in England, Company Number 5478976. Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL
Gang Ma wrote:
I tried to use boost/thread library to write a program to read a large text file. The program compiled but did not get the right answer. I am not sure my usage of the library is correct. I appreciate your helps.
<Code snipped> Check the documentation for basic_istream::seekg. It doesn't take a line number.
participants (4)
-
Andrew Holden
-
Anthony Williams
-
Gang Ma
-
Steven Watanabe