We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,725 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Auto-update a file that is currently in use in Linux

Hey all!

I am writing an application that will read data coming in through a serial port (RS232) and write that data to a file. This file will then be parsed by a php script and the data will uploaded to a server. Is there a function that allows me to make the file auto-update after each data input?

If this is unclear, let me shed some light. Say you write a piece of data to a file using the << operator with a variable of type ofstream. If you open the file while the application is running, it will show the last data that was input. If you input another piece of data, it will prompt you to "reload" the file to be able to see the new data input. Is there a way to do this automatically so the application does not have to be shutdown every so often? This would also allow the php script to run non-stop as well.

5
Contributors
12
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
13
Views
Idestruction
Newbie Poster
13 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

tip.

You need to use a while loop to keep the programe runing to take all the input and write to disk by closing the file. That is you must have your file operation in a first while loop to reopen the file after every input/data recieved from the serial.

richieking
Master Poster
789 posts since Jun 2009
Reputation Points: 58
Solved Threads: 156
Skill Endorsements: 0

tip.

You need to use a while loop to keep the programe runing to take all the input and write to disk by closing the file. That is you must have your file operation in a first while loop to reopen the file after every input/data recieved from the serial.

Makes sense. The only problem I see is that when you use the .open("filename.filetype here) member function, it will open a new file with the file name given. That is, it will overwrite any file that has the same name and file type. I do not think closing and opening the file would be viable as I foresee it overwriting the current file.

Idestruction
Newbie Poster
13 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

std::ostream::open takes a second argument that says what you want to do with the file you're opening. std::ios::app opens the file in 'append' mode:

std::ofstream myFile;
myFile.open( "filename.txt", std::ios::app );

You can also cause data to be written to the file by calling flush , without closing the file each time you want to write the data to disk.

ravenous
Practically a Master Poster
681 posts since Jul 2005
Reputation Points: 286
Solved Threads: 111
Skill Endorsements: 8

I think ravenous said it all ;-)

richieking
Master Poster
789 posts since Jun 2009
Reputation Points: 58
Solved Threads: 156
Skill Endorsements: 0

If there is no need to store the data for later use in the file you might do better to use a socket or named pipe. Your application can write to the one end of the pipe and the php script can read from the other end when data is available. In fact, the php script will block while reading the pipe/socket until data is available so there is no need to busy loop until that point.

L7Sqr
Practically a Posting Shark
849 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7

If there is no need to store the data for later use in the file you might do better to use a socket or named pipe. Your application can write to the one end of the pipe and the php script can read from the other end when data is available. In fact, the php script will block while reading the pipe/socket until data is available so there is no need to busy loop until that point.

Could you point me in the direction of more information on this? This might actually work.

EDIT:

I have tried doing what ravenous suggested to no avail. If I have my file (in this case it is called log.txt) open while running the application, it will still prompt me to "reload" the file after more data has been written to it to be able to see that data. This is what I am trying to override in my code

Idestruction
Newbie Poster
13 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Your code is editing data in memory, not on disk so in order for others to see the changes the data must be written to disk, and re-loaded by whomever needs it.

Windows: CreateNamedPipe
Linux: Pipes, FIFO, and IPC

L7Sqr
Practically a Posting Shark
849 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7

Your code is editing data in memory, not on disk so in order for others to see the changes the data must be written to disk, and re-loaded by whomever needs it.

Windows: CreateNamedPipe
Linux: Pipes, FIFO, and IPC

Thanks for the info! This may be the way I go. However, is there anything special I have to do for cross-platform pipes? The info will be going from linux-based slave computers to a windows 2003 server running MySQL

Idestruction
Newbie Poster
13 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

at least paste some updated code so far ok?

richieking
Master Poster
789 posts since Jun 2009
Reputation Points: 58
Solved Threads: 156
Skill Endorsements: 0

at least paste some updated code so far ok?

This is all preliminary research for this project. I am still trying to figure out which route is going to be the best for my situation. In a nutshell, I have to read data coming in over an RS232 connection (this application will later be run on 6-7 machines simultaneously), write that data to a file (or to a pipe as I have just recently be told), and that data will be parsed by a PHP script and processed by the server

Idestruction
Newbie Poster
13 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I once had to send data (weight of several silos) received on eight RS232 interfaces to a central server.
What I did was: Write data continuously to file(s). Every (configurable) time interval I closed the files and sent them via FTP to the server which itself was looking for new files every so and so minutes.
If you need real time data you could use sockets - I don't think there are cross platform pipes.(don't know for sure though)
If your server is MySQL you could send the data directly to it.
(http://www.mysql.com/downloads/connector/cpp/)

Caligulaminus
Junior Poster
106 posts since Feb 2011
Reputation Points: 72
Solved Threads: 30
Skill Endorsements: 0

If you are dealing with multiple machines with varying platforms you may be best just opening a network socket and communicating that way. This would serve you best if at some point you needed to support machines hosted in various locations.

L7Sqr
Practically a Posting Shark
849 posts since Feb 2011
Reputation Points: 253
Solved Threads: 155
Skill Endorsements: 7

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0943 seconds using 2.7MB