Link to home
Start Free TrialLog in
Avatar of bbs97
bbs97

asked on

Reading from file

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
ASKER CERTIFIED SOLUTION
Avatar of Salte
Salte

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bbs97
bbs97

ASKER

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.
Avatar of bbs97

ASKER

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.

   
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
Avatar of bbs97

ASKER

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.
Avatar of bbs97

ASKER

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.

   
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
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