LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-20-2007, 07:08 AM   #1
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 10.04, Crunchbang Statler
Posts: 3,325

Rep: Reputation: 168Reputation: 168
[two apps writing to file]Is this possible?


Two applications writing data to a file (under windows).
  1. I use teraterm pro (ttpro; terminal program) that writes received data to a file.
  2. I have my own TCL application that writes timestamps to the same file by opening the file, writing the timestamp and closing the file.
Guess what, it does not do what it's supposed to do I can see the timestamp when I open the file with an editor (pfe32) just after the timestamp is written. However new data from ttpro overwrites it. I assume that this is because ttpro does not close the file after a write and therefore the filepointer is still pointing to the last position.

Did I overlook something or is it simply not possible? If not possible and I'm correct with the assumption that ttpro does not close the file, is anybody aware of a terminal program including logging capabilities that does close the file after a write and works under Windows?

PS
hyperterm is even worse as it locks the file (a MS trademark?) so the other app gets a permission denied
 
Old 09-20-2007, 09:07 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,528

Rep: Reputation: 60
If you open the file in append mode, it will automatically write to the end of the file. You still might have to cope with problems where the writes happen at the same time though.

If you can't open the file in append mode, seek to the end before you write.
 
Old 09-21-2007, 12:04 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 10.04, Crunchbang Statler
Posts: 3,325

Original Poster
Rep: Reputation: 168Reputation: 168
My app does write (I can see it), but the second app (ttpro) does not seem to go to the end of the file before it writes, hence overwriting the timestamp.

I know that concurrent writing will give issues, but those issues are preferable over not having a timestamp at all.

Thanks for the thoughts.
 
Old 09-21-2007, 12:16 AM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,858
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Please consider:
a) opening (in append mode),
b) writing, and
c) closing the file
for each individual write.

Otherwise:
a) you'll need some kind of locking to synchronize writes between the two programs
b) you'll need to explicitly "flush" after each write
... and ...
c) you'll need to consider the unfortunate circumstance that even an explicit "flush" doesn't always write as promptly as you'd wish it to ("close", on the other hand, is much more reliable).

Just a thought .. pSM
 
Old 09-21-2007, 02:54 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 10.04, Crunchbang Statler
Posts: 3,325

Original Poster
Rep: Reputation: 168Reputation: 168
I did try a flush, but it did not help. I think that my side of the code is OK (tcl write procedure below), but I can not influence the other app (maybe if I can find the source code but in that case I can just as well write the whole app myself).

Code:
###########################################################
# write to a file
###########################################################
proc write_file {filename data} {

    # open output file
    # put filename between double quotes so it will handle spaces correctly
    if [catch "open \"$filename\" \"a\"" fpout] {
        # error opening
        puts "File error: $fpout"
        return 1
    }

    # prevent translation of '\n' to '\r\n'
#    fconfigure $fpout -translation binary

    puts $fpout $data
    close $fpout
    return 0
}
 
Old 09-21-2007, 10:11 AM   #6
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,858
Blog Entries: 1

Rep: Reputation: Disabled
Yup - you're already doing exactly what I was suggesting.

So I agree - your side of the code looks OK. It looks the other app is the culprit.

Good luck - and sorry I couldn't be of more help :-(

Your .. PSM

Last edited by paulsm4; 09-21-2007 at 10:13 AM.
 
Old 09-21-2007, 04:01 PM   #7
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 33
fdatasync() is guaranteed on POSIX-compliant boxes to write all data associated with the incore descriptor for the file (REALTIME), of which your process has one copy.

If it fails and sets errno to EINVAL, then your box does not support fully synchronized I/O.

Otherwise everybody;s data is guaranteed to be written to disk. Thre only weenie is if the other process is "manually" positioning its file pointer relative to the data it just wrote. Which is not very likely.

Try calling fdatasync(), then write your stuff, then fdatasync() again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
writing an xml file? ati Programming 3 05-05-2006 09:14 AM
writing and reading from a file! sahel Programming 1 12-27-2005 01:33 PM
Where to Start: Writing C++ KDE Apps BuckRogers01 Programming 28 10-19-2005 03:52 PM
C File reading and writing AbhishekSamuel Programming 3 05-03-2005 03:59 PM
writing to file ej25 Programming 17 11-19-2004 11:23 AM


All times are GMT -5. The time now is 02:44 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration