Copying info from a .TXT file

PatGPatG Junior MemberPosts: 35

I need to copy an IP address from a txt file that is stored on the master. Any links that would explain this much appreciated, my search has so far been fruitless.

Thanks

Comments

  • HARMAN_ChrisHARMAN_Chris Harman Professional University Posts: 286

    Text file operations are covered in Exercise 7 of the CP232 instructor led course. We are working on capturing this course in an On Demand series and expect it to be available shortly. I presume you will receive code suggestions from forum posters before this goes live, but I would encourage you to check out the CP231 On Demand course once it is released.

  • PatGPatG Junior Member Posts: 35

    OK thanks

  • sentry07sentry07 Junior Member Posts: 61

    Check the help file for FILE_OPEN, FILE_READ_LINE, FILE_CLOSE, etc. Those functions give you what you need to open a file with a file handle, read lines from it, and close the file handle.

  • ericmedleyericmedley Senior Member - 3709 Posts Posts: 4,151

    do NOT forget to close your files when you are done. :)

  • PatGPatG Junior Member Posts: 35

    I can now open/ close and read the file but getting the info out of it and into a structure ( which I'm pretty sure I need to use ) escapes me. Would that course capture be online yet?

  • ericmedleyericmedley Senior Member - 3709 Posts Posts: 4,151

    Post some code and maybe we can help you. It's honestly not difficult.

  • PatGPatG Junior Member Posts: 35

    OK so here's what I have so far:

           nIPfile = FILE_OPEN ('IP_ADDRESSING2.txt', FILE_READ_ONLY)
        nMaster_IP = FILE_READ(nIPFile, cFile_BUFFER, 29) 
        IP_CLIENT_OPEN (cFile_BUFFER)
        nIPfile = FILE_CLOSE (nIPFile) 
        CLEAR_BUFFER cFile_BUFFER
    

    cFile_BUFFER has everything I need in it in the form of a string. So I guess my main problem is using it to open the port.

    For example, if I use

    IP_CLIENT_OPEN(dvLights.PORT,cLightControl_IP,24,IP_TCP

    it works fine, but I didn't get that info from the, file I just put it in there.

    Thanks

  • ericmedleyericmedley Senior Member - 3709 Posts Posts: 4,151

    @PatG said:
    OK so here's what I have so far:

           nIPfile = FILE_OPEN ('IP_ADDRESSING2.txt', FILE_READ_ONLY)
      nMaster_IP = FILE_READ(nIPFile, cFile_BUFFER, 29) 
      IP_CLIENT_OPEN (cFile_BUFFER)
      nIPfile = FILE_CLOSE (nIPFile) 
      CLEAR_BUFFER cFile_BUFFER
    

    cFile_BUFFER has everything I need in it in the form of a string. So I guess my main problem is using it to open the port.

    For example, if I use

    IP_CLIENT_OPEN(dvLights.PORT,cLightControl_IP,24,IP_TCP

    it works fine, but I didn't get that info from the, file I just put it in there.

    Thanks

    Okay, for starters you might not be using Open_File correctly.

    nMaster_IP = FILE_READ(nIPFile, cFile_BUFFER, 29)

    I don't know what nMaster_IP is but the value the function FILE_READ spits out is a signed integer. If the value returned is less than zero it indicates some kind of error occured when attempting to read the file. A value of zero or greater is the number of bytes returned up to the end of file.

    Also in the statement: "IP_CLIENT_OPEN (cFile_BUFFER)" I'm surprised this even compiled.

    The format for this function is;

    result=ip_client_open(integer local_port_id,char IP_ADDRESS,long Server_Port, integer protocol)

    result is a signed integer that reports a value code to let you know the success/failure of the attempt to connect to a remote server port.
    local_port is the port given by the DEFINE_DEVICE section of code you chose to create the client. ex:

    DeFINE_DEVICE

    dv_My_IP_Client = 0:4:0 // an ip client on netlixn ip port 4

    IP_ADDRESSW is a string containing the IP Address of the server you're trying to reach.

    Server_Por is long variable or value that ijndicates the remote server's IP port. (ex: 80 for a web server...)

    protocol is an integer to tell the processor which method to connect to the remote server.
    1 = TCP
    2 = UDP
    3 = UDP with Receive

    Bear in mind the IP_Client_Open command doesn't actually send anything or recieve anything. IP communication is merely established with the remote server. It's up to you to manage the asyncronous communication. Usually you need to set up a data_event that watches the online and offline status of the IP port. when the port comes online you usually need to send something to initiate the conversation (like the GET command of a web server or return key of a telent session or whatnot.) Also you monitor the string returns in the same data_event. To send messages to the server you use the
    Send_String dv_My_IP_Client, 'A string to the IP server'

    you may also need to manage the IP connection itself. In the case of many servers (like a web server) they will only stay alive for a quick session like one message in and a respojnse. then it shuts you down.

    Other servers might allow you to leave the port open as long as you say something every now and then. If you're quiet too long it shuts you down.

    Still other servers will leave you connected as long as you want and never try to shut you down until you say so.

    In each case it's up to you to manage this connection and be proactive about maintaining it until you are done.

    Those are some hopfully helpful hints.
    e

  • richardhermanrichardherman not-so-junior member Posts: 200

    And while were on it, could you post what's in 'IP_ADDRESSING2.txt'?

  • Danny CampbellDanny Campbell Senior Member Posts: 311

    Try something like the code below. It opens a file and will read up to 1000 records with a max length of 200 bytes per line. It assumes it is a DOS text file with CR/LF at the end of each line. The line read gets stored in the array cCFGLINE. Each array element contains one line read from the file. You can then use whatever string functions you need to extract the data you need. If you only need to read one line, don't do the for loop and just read one line the info read will be in that stack_var "line". When it is finished, nCONFIGMAX contains the number of record read from the file.

    I copied this from an existing program and may have missed a variable or two but it should give you an idea of where to start. I use this to have a file of xConfiguration commands for a cisco video codec where I just shoot the lines in the array to the codec to do a configuration. So I'm not grabbing anything out of the line before I send it on. But if you know what you are looking for you can do the string functions to pull what you need from the line.

    DEFINE_VARIABLE

    CHAR strFILE[] = 'CONFIG.TXT'
    INTEGER nCONFIGMAX
    VOLATILE CHAR cCFGLINE[1000] [200]

    DEFINE_FUNCTION CFGLOAD()
    {
    STACK_VAR CHAR line[200]
    STACK_VAR INTEGER i
    STACK_VAR SLONG bytes
    STACK_VAR SLONG file

    bytes = 1
    
    file = FILE_OPEN(strFILE,1)  
    
    IF (file > 0)
    {
          nCONFIGMAX=0
    
                // READ ALL RECORDS
    
        FOR (i = 1; ((bytes > 0) AND i < 1001); i++)
        {
                 bytes=FILE_READ_LINE(file, line, 200)
        IF (bytes > 1)
        {
        cCFGLINE[i]=line
        nCONFIGMAX=nCONFIGMAX+1
        }
    
        }
        FILE_CLOSE(file)
    }          
    

    }

  • PatGPatG Junior Member Posts: 35

    Thanks. I'm probably not explaining myself properly. IP_CLIENT_OPEN (cFile_BUFFER) doesn't compile, I was just giving an example of where the data is and what I need it for.

    nMaster_IP is just a bad choice of words for that variable. I am able to open the file and get the info I need but not into the format that I need it.
    As I mentioned above I can get the port open and have it work and stay open, but that's when I use the line of code that has device and IP address, etc. previously defined. What I need to do is get all that info from the file, not populate it myself. And I am not sure how to get it from the buffer into the formats it should be in.
    I need a device first, then a string, then an integer and then another string from what I can tell but when I take the parts out in order and try to manipulate them from that string it doesn't like anything I try. Also defining a device in this way has me baffled.

    Thanks for your time and patience.

Sign In or Register to comment.