CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Join Date
    Jan 2012
    Location
    Florida, USA
    Posts
    19

    [RESOLVED] Reading a .txt file in real time

    hey guys, I've been trying to find a way to read a .txt file in real time. I've developed a client/server console app program in Visual Studio 2010. I can open and send txt files to my sever program with my client program (tcp, netstream). However, the file that I am calling is constantly being updated with new data. I want to sent that updated data straight to my File Sharing Watcher function but this merely tells me if my file or directory has changed not the actual string of data. Does anyone know of any other function or way of achieving this?

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Reading a .txt file in real time

    It would help to know how the file may be changed. If data is being appended then it is simple just keep track of the filesize and read to the end starting at the position of the size of the file at the last read. If data is being changed within the file then it is a bit more complicated.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Reading a .txt file in real time

    Your server program could write each incoming block of text to a new file. Eg: 1.txt, 2.txt, 3.txt (or even get the system to generate a temp file name for you, so you don't have to keep a counter). Then your FileSystemWatcher could just look for files being created. These files will just have the new text and of course they can be deleted afterwards or appended to a main file and deleted.

    I've done some work with FileSystemWatcher in the past and when opening one of these new files, you may find that it is still locked by the process writing to it and have to catch the exception (only way to do this) wait a second or 2 and then open it again.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  4. #4
    Join Date
    Jan 2012
    Location
    Florida, USA
    Posts
    19

    Re: Reading a .txt file in real time

    I appreciate the replies. DataMiser, I basically want to mimic the function of the Tail application. I am working with sensor readings. These readings are sent to a file in intervals of 8 seconds. My client program reads the .txt file with these sensor readings and sends them to my server. Therefore, I want my program to read the .txt file until the end and wait for the next string to be added. rliq, I understand what you are saying about writing new data to a temp file. However, FileSystemWatcher would not be able to tell me the exact string that was updated right?

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Reading a .txt file in real time

    So new data will always be appended to the end of the file which makes it fairly simple to know what was written.

    Basically what you could do is read the file initially and store the size of the file at that point.
    When the file changes look at the stored file size then read the file starting at that location to the end of the file. Which will in effect be reading only the new data. Update your variable to reflect the new file size as of this read.

    I would use a filestream and binaryreader to do this since it is simple to read based on byte position and number of bytes to read but there are other options.
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Reading a .txt file in real time

    If you write each block of data to a different temp file each time, then the whole of its contents is the string that was written. Then append the whole of that text to the end of another file and delete the temp file. It's not quite the UNIX "tail -f" command, but it may serve your purpose.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Reading a .txt file in real time

    I was under the impression that the file is being written by another program. If you are writing it yourself then the suggestion above is a good one.
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Jan 2012
    Location
    Florida, USA
    Posts
    19

    Re: Reading a .txt file in real time

    Thank you both for your suggestions, they have helped me big time. Yes Datamiser, the file is actually being written by another program. The sensor readings I was telling you guys about actually send the data to the computer via serial communication. Therefore, I figured it would be easier to develop a COM port listener and have my client program read that data as the event happens. I'm having some issues with my COM port listener. Could you help? Thanks.

  9. #9
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Reading a .txt file in real time

    What kind of issues are you having?
    Always use [code][/code] tags when posting code.

  10. #10
    Join Date
    Jan 2012
    Location
    Florida, USA
    Posts
    19

    Re: Reading a .txt file in real time

    Ok so as I mentioned before, I have some sensors sending data to a file. I want to listen to the port directly (COM 4). I set all the parameters and when I try to listen for incoming data, my application shuts down. It doesn't listen at all. Its a small program. I have attached my entire COM listener program. I know the problem is in the part where I receive data, I just dont know why? it seems like everyone is using the DataReceive Method.
    Attached Files Attached Files

  11. #11
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Reading a .txt file in real time

    I have not tried writing this kind of app as a console app and I am far from an expert in C# but I do not see any thing in your program to keep it from just dropping through main and exiting.

    Am I missing something that allows the program to stay running?

    Normally I would do this with a windows forms app and the form being open would stop the project from closing while I wait for events on the port.
    Always use [code][/code] tags when posting code.

  12. #12
    Join Date
    Jan 2012
    Location
    Florida, USA
    Posts
    19

    Re: Reading a .txt file in real time

    Im new at C# as well. I'll try doing this same project with a windows form app. I am assuming the program will keep running or listening since I open the port and never close it. It's actually what I what, but Im not sure if I still need that (close port) to have a working program. Thanks for your insight. I appreciate it.

  13. #13
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Reading a .txt file in real time

    In a forms app the program keeps running until you close the form. The port will keep receiving data so long as the port is open and data is coming into the port.

    How are you intercepting the data from the port? Are you tied into the cable with a splitter or something so you can get the data and the other program still get it as well?
    Always use [code][/code] tags when posting code.

  14. #14
    Join Date
    Jan 2012
    Location
    Florida, USA
    Posts
    19

    Re: Reading a .txt file in real time

    So the sensors are transmitting to a device and this device is connected to my pc via rs 232. The data that is being transmitted by this device is in the for of strings. I have a little third party terminal app that listens to the COM port and it is receiving data so I know its transmitting. My app is the one not receiving anything.

  15. #15
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Reading a .txt file in real time

    I was wondering how you are connected. Have you disabled the other program and now using yours to monitor the port or is the other program still running at the same time? Have you converted to a forms app and still not receive anything? Have you set the receive threshold to a value >0 so as to trigger the data received event when data arrives?
    Always use [code][/code] tags when posting code.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured