473,386 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C++ / Problems using "seekg()" method

Need some help with this code / Using UNIX.

The goals is to read a big file (2,000,000) lines, using fork() to create children to help in the process.

I am able to get the program (C++) running and this is what it does:
1) creates the number of children the user indicates
2) can read the number lines in the TXT file
3) tells how many lines each child will read

This is how I understand what the program will do and I want to make sure thatI am right or wrong:

As an example, lets assume that we have: 2 children & 10 TXT lines, each child will read five lines.

When the loop creates a child, that child will go to the TXT file will read its own set of lines (in this case 5) and will copy them to the pipe. After this, the loop will continue and each new child will do the same, with its own set of lines.
Is this correct?...

If this is correct... then I need help in defining the propper way to have the 2nd child created, continue reading the next line, meaning:
Child 1 reads lines: 0,1,2,3,4
Child 2 reads lines: 5,6,7,8,9 <<< how to make the second child go to the proper line to start?

I know that the seekg method goes to a specific line, still I am having problems adding this functionallity to the code.

Please advise...

Thank you...
Sep 15 '07 #1
1 2368
RRick
463 Expert 256MB
Seekg will take you to a specific byte location, which can be in the middle of a line. Your idea will work easily if all of the lines are exactly the same size. Then you simply seek to lineNo * lineSize.

If the lines are of a different size, you will have to search for a line after you do the initial seek. You will also have to make sure that no lines fall through the cracks between the different processes.


There is another way to process the lines without this type of complication. You have one master process that reads in the lines from the file and then sends each line out to other processes. You can communicate over stdin/stdout for this type of communication. This will work if the IO between processes is not too large. Also, the master process can determine how many processes are needed.

This type of processing can also be done with threads. In fact, I would suggest using threads in this case. This type of reader thread and multiple process threads is very common for threaded programs. If interested, take a look at the linux pthreads doc and online tutorials.
Sep 17 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Christopher Reeve | last post by:
Hi, I wonder if anyone could help me. I am moving accross from C to C++ and am writing a program that reads and writes data into a file. I am aware of the old C ways of doing it but am trying to...
14
by: J. Campbell | last post by:
I posted a question some time back about accessing a char array as an array of words. In order not to overrun the char array, I padded it with enough 0x00 bytes to ensure that when accessed as...
10
by: ZafT | last post by:
Thanks in advance for any tips that might get me going in the right direction. I am working on a simple exercise for school that is supposed to use read to read a file (about 10 MB). I am...
25
by: rokia | last post by:
in a project, I use many,many stl such as stack,list,vctor etc. somewhere the vector's size is more than 2K. is this a efficient way?
4
by: Jon Hyland | last post by:
Hi all, I'm looking for the fastest way to write (and/or read) binary to a file in VC++. I've been using the fstream object in this way: //unsigned char *pDataOut and long iLength initilized...
16
by: kate | last post by:
salve. per favore rispondete alla mia domanda: Come faccio a ottenere le dimensioni di un file?(con c/c++) risp presto grazie
6
by: Tom | last post by:
Given: A binary data file of records. Task: Random access using seekg(). I've never found any documentation stating that the offset from begin file marker is safe on a fragmented file. I've...
9
by: Robin | last post by:
// Pls note that test.txt exists. #include <iostream> #include <fstream> #include <string> using namespace std; int main() { fstream fs("test.txt", ios_base::in | ios_base::app);
12
by: Julian | last post by:
Hi, I am having problems with a function that I have been using in my program to read sentences from a 'command file' and parse them into commands. the surprising thing is that the program works...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.