?
Solved

Reading from file

Posted on 2003-02-19
8
Medium Priority
?
195 Views
Last Modified: 2010-04-01
Hi,

I have two servers, one java (server1), one C++ (server2).
I wish server 2 to read from a text file. Entries are written to the file by server1 in the format:

Received from user : IP : at time xx.yy.zz

Each entry is appended so that there may be several lines of the above in a single session.
I want a certain action to be performed every time an entry is read, i.e. line by line. So when server2 reads the first entry, it performs the action. The next time I want it to check the next line in the file for an entry (which may or may not be there) so as not to keep reading the first entry and performing the action.

How do I accomplish this: Here is my code so far:

ifstream ComplaintFile;

ComplaintFile.open("C:\\Files\\project\\User.txt",ios::in);

  if(!ComplaintFile)
  {
     cerr << "File could not be opened" << endl;
               
     exit (1);  
  }    

  String s;
                     
  while(ComplaintFile >> s)
  {

   QLevel += QLevel; // action performed for each entry

  }
                       
    // ComplaintFile.close();

  return 0;

Thanks
0
Comment
Question by:bbs97
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 12

Accepted Solution

by:
Salte earned 360 total points
ID: 7980908
First off, is this file written by server1, does it ever remove a line from the file or are they just appended?

I presume you have some job that once in a while move the files over to some other place so that you at those times start afresh with new files. This job is presumably run at a time when none of the servers are active and all the entries in the files has been properly processed.

So, the net result is that you have a fairly static file in which one process append to it but never remove any entries and the other process read information from the file one line at a time but never changes the file.

I suggest you remember the point in the file where you left off last time (it was eof at that time) and then seekg() to that point next time you read the file, this way you continue where you left off. If the file has more data to read you will read those data then, if not the read will give an immediate end of file and you know there's nothing new to process.

If you did find something to process you process it and when you have read all the lines until eof you remember where you left off and close the file. Next time you open the file you seekg() until the place you left off last time etc.

Now, what to do with that file position information? You can store it in the server but if the server crashes you might want to keep it outside so that you won't process over again entries that you have already processed. So you might want to have a second file which is only used to server process 2 (the reader). This file is opened for read by process 2 when it starts up and then closed. It is used to initialize the variable that indicate where to seekg() for the previous file. It is important that when you do the aforementioned job of moving the files you 1) make sure that the server has processed all entries and that 2) the file with file pos info is removed when you move the files. If the file doesn't exist when server 2 starts up it should initialize the file pos info with '0' meaning the first entry is not yet processed.

Now, ever time you have successfully processed a line in such a way that you don't want to process it again, you write to that file pos info file and store the new file position. You don't keep that file open in your server but opens it, write the info and closes it every time. Since this is a costly operation you might want to do it so that you only do that every 10 times, if you do you must have some mechanism to deal with the fact that you may have already processed 9 of the lines you seekg() to at the first time after startup. I.e. some manner of detecting that must be present. Another way is to keep the file open but if so you must open it in a way so that other processes can read it and you must seekp(0) every time you write the information over again. If your file system allow for sharing options you can use those but C++ has no way to specify that a file is opened with sharing options so you probably have to open the file outside of C++ and then attach it to an iostream after if necessary.

If you process the line but with error or some other way so that you want to process the line later you can either avoid storing new info or you can instead of simply having just a position of 'next line to process' have a list of 'entries to process'. If so this second file essentially have a table of entries that you need to process and the last entry of that table is the file position of the EOF mark of last time you opened the file. In this case you will push an entry into that list if it needs further processing, you might even indicate what kind of processing etc. All depending on the logic of your program.

An alternative to using a second file to store this info is to store it in some other permanent place such as in windows registry etc. This is essentially just like using a file so I won't go into details here.

Alf
0
 

Author Comment

by:bbs97
ID: 7993349
Hi,

  Thank you for your detailed response, I hadn't thought of that. Lines are just appended by server1, it never removes lines. I understand the second file but could u show me some of how it is implemented as I'm kind of confused as to the variable initialisation to indicate where to seekg().

  Thanks again.
0
 

Author Comment

by:bbs97
ID: 8011466
Hi,

  Can I simply read in the text line by line and perform the action each time a new line/entry is found? How would I implement this given the code above?

  Thanks again.

   
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:Salte
ID: 8011934
The idea is simple enough.

Just have one file holding the file position and possibly other data, maybe even the filename, date/time last written etc..

Then when you start up you open that file and retrieve all the parameters from it.

Then when you are ready you open the other file, seekg() to the position indicated by the parameters you retrieved and if no longer at eof you read that line process it and update that file containing the seekpos and other info.

NOt really much point for me to show any example, I assume you know how to read/write to a file and there are so many ways to do this and each and every one of them would be correct.

Also, one good thing when you never modify and remove earlier lines is that the file position for a specific line is fixed. Even if code later add new lines, the old eof will then indicate the position where you haven't yet read and processed lines.

If you remove lines this is much harder to keep track as the file position would change.

Alf
0
 

Author Comment

by:bbs97
ID: 8012276
Hi,

  I have just been running the servers in parallel using the following code:

char buffer[256];
 
  while (! ComplaintFile.eof() )
  {
     ComplaintFile.getline (buffer,100);
     cout << buffer << endl;

     QLevel += QLevel;
  }

This reads in the whole content of the file and performs the operation.
There are no file sharing violations, it is reading the file fine and as far as I know performing the function i.e. increasing the quality by 1 level. Are u sure I need to create a second file to indicate the last position? I know how to read/write fine. Could I not use a simple counter to tell the server to read the next line each time?

Thanks again.
0
 

Author Comment

by:bbs97
ID: 8016167
Hi,

  Would you help me with the implementation as I'm not sure exactly how as my file I/O knowledge is not as broad in C++ as in Java.

   
0
 
LVL 2

Expert Comment

by:udil
ID: 8867125
This question has been abandoned. I will make a recommendation to the moderators on its resolution in a week or two. I appreciate any comments that would help me to make a recommendation.

In the absence of responses, I may recommend DELETE unless it is clear to me that it has value as a PAQ. Silence = you don't care

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Udil
EE Cleanup Volunteer
0
 
LVL 2

Expert Comment

by:udil
ID: 8950749
This question didn't show any activity for more than 21 days. I will ask Community Support to close it unless you finalize it yourself within 7 days.
Unless there is objection or further activity,  I will suggest to

    "Answered by: Salte"

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Udil
EE Cleanup Volunteer
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question