CSV file not Appending

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • CSV file not Appending

    I found this code in a July 2009 thread to output data to a CSV file.
    First time executing the program everything works fine (created the file and put the data in).
    Problem is on subsequent runs it is overwriting the data, not appending.

    Code:
    ASSIGN/V1=C2.INPUT
                ASSIGN/V2=LOC1.T.DEV
                ASSIGN/V3=LOC6.X.DEV
                ASSIGN/V4=LOC12.Z.MEAS
                ASSIGN/VALL=V1+","+V2+","+V3+","+V4
    VEXSIT1    =FILE/EXISTS,C:\DATAFOLDER\\DATADUMPFILE2.CSV
                IF/VEXIST1==0
    FPTR       =FILE/OPEN,C:\DATAFOLDER\\DATADUMPFILE2.CSV,WRITE
                FILE/WRITELINE,FPTR,SETMASTER_CTQ
                FILE/WRITELINE,FPTR,Run #,T,X,Z
                FILE/CLOSE,FPTR,KEEP
                END_IF/
    FPTR       =FILE/OPEN,C:\DATAFOLDER\DATADUMPFILE2.CSV,APPEND
                FILE/WRITELINE,FPTR,VALL
                FILE/CLOSE,FPTR,KEEP
    Any ideas would be appreciated.
    I can explain it to you, but I can't understand it for you.

  • #2
    Well if you want to Append, tell it to append:
    Code:
    FPTR       =FILE/OPEN,C:\DATAFOLDER\\DATADUMPFILE2.CSV,APPEND

    Comment


    • #3
      At first glance you have two filepointers with the same name FPTR.
      Try naming them FPTR1 and FPTR2 depending on which one you want to use (WRITE vs APPEND).

      Second, some of your file directories are separated by "\\", shouldn't there only be one "\" ? (or is it the other way???)

      Also, try separating your math operations with a space. Instead of
      ASSIGN/VALL=V1+","+V2+","+V3+","+V4
      Try
      ASSIGN/VALL=V1 + "," + V2 + "," + V3 + "," + V4

      Sometimes PCdmis doesn't work right with no spaces.
      Last edited by TheyCallMeWoody; 06-09-2015, 10:52 AM.

      Comment


      • #4
        The file directories work fine.
        The spaces in the variables had no effect.
        If I delete the excel file, PCdmis creates one. That line of code works.
        It fills in the header info (the FPTR in the IF/END_IF statement).
        It fills in the data (the VALL in the second FPTR statement).

        When I execute again and open the excel file there is only one line of data.
        V1 is a serial number. So each time I execute I change that number but the only one showing in excel is the last one.

        When I changed the FPTR's to FPTR1 and FPTR2 I got all kinds of messages saying it couldn't read or write the files.
        Would I also need the change the writelines to the corresponding FPTR1 and FPTR2?
        I can explain it to you, but I can't understand it for you.

        Comment


        • #5
          Are you running with admin rights to be able to write to C:\DATAFOLDER?

          Comment


          • #6
            Yes. If I delete the DATADUMPFILE2.CSV from the DATAFOLDER PCdmis will create a new one and fill in all the data.
            It's just every run after that. There's only one line of data in the .CSV and the serial number changes after each execution.
            I can explain it to you, but I can't understand it for you.

            Comment


            • #7
              Did you copy/paste that section of code into your program? I know this seems weird but sometimes I have trouble copy/pasting IF/ statements. If I copy/paste IF/ statements, I go through and click on the IF/ line and press F9 and then without changing anything, click OK. If I don't do this, it seems to ignore some of my IF/ lines for some reason I haven't been able to figure out. I would think if this was happening you would be seeing 2 lines of data rather than one, though. Maybe put the last three lines in an ELSE/END_ELSE?

              Comment


              • #8
                Aha!
                The tiny friggin' details always get us!
                You were not getting your headers written during the file creation step because they need "quotes" to be text strings suitable for writing to file, just like you concatenated VALL with variable values and text strings of ","
                Code:
                ASSIGN/V1=C2.INPUT
                            ASSIGN/V2=LOC1.T.DEV
                            ASSIGN/V3=LOC6.X.DEV
                            ASSIGN/V4=LOC12.Z.MEAS
                            ASSIGN/VALL=V1+","+V2+","+V3+","+V4
                VEXSIT1    =FILE/EXISTS,C:\DATAFOLDER\\DATADUMPFILE2.CSV
                            IF/VEXIST1==0
                FPTR       =FILE/OPEN,C:\DATAFOLDER\\DATADUMPFILE2.CSV,WRITE
                            FILE/WRITELINE,FPTR,"SETMASTER_CTQ"
                            FILE/WRITELINE,FPTR,"Run #,T,X,Z"
                            FILE/CLOSE,FPTR,KEEP
                            END_IF/
                FPTR       =FILE/OPEN,C:\DATAFOLDER\DATADUMPFILE2.CSV,APPEND
                            FILE/WRITELINE,FPTR,VALL
                            FILE/CLOSE,FPTR,KEEP


                Did you get that VALL junk from that JEC_31 nutjob? I think he needs to spend less time on teh intardwebz.
                Nowdays I would skip the extra steps of assigning variables and simply write
                Code:
                            FILE/WRITELINE,FPTR,C2.INPUT+","+LOC1.T.DEV+","+LOC6.X.DEV+","+LOC12.Z.MEAS

                Comment


                • #9
                  Yep that's the thread I followed.
                  I'll give this a try.
                  I can explain it to you, but I can't understand it for you.

                  Comment


                  • #10
                    Originally posted by bphillips View Post
                    When I changed the FPTR's to FPTR1 and FPTR2 I got all kinds of messages saying it couldn't read or write the files.
                    Would I also need the change the writelines to the corresponding FPTR1 and FPTR2?
                    Yes you need to change the corresponding filepointer names.

                    They're like any other name in PCDMIS or other programming, everything must match.

                    Comment


                    • #11
                      Document.jpg
                      Here is what the excel file looks like.
                      Row 3 is the serial number and data.
                      That is the line that changes with each run.
                      Adding the ELSE/END_ELSE gave me an Improper argument.
                      I can explain it to you, but I can't understand it for you.

                      Comment


                      • #12
                        Originally posted by TheyCallMeWoody View Post
                        Yes you need to change the corresponding filepointer names.

                        They're like any other name in PCDMIS or other programming, everything must match.
                        Actually, he doesn't in this case.

                        Filepointer FPTR is used to create the blank file with header, then the file is closed - releasing the Filepointer.

                        Then for the data collection phase, Filepointer FPTR is re-initialized and used to open the file perform the Appending.
                        Once Appending is completed, another File/Close and Filepointer FPTR is again released from usage.

                        Now if you need to have multiple files open at the same time for reading & writing, then you must use unique-name Filepointers for each of them...
                        ...which always screws me up because when you insert new File I/O commands they always default to FPTR and I forget to edit them.

                        Comment


                        • #13
                          Originally posted by bphillips View Post
                          [ATTACH=CONFIG]10529[/ATTACH]
                          Here is what the excel file looks like.
                          Row 3 is the serial number and data.
                          That is the line that changes with each run.
                          Adding the ELSE/END_ELSE gave me an Improper argument.
                          So, the data line is being overwritten each run?

                          Comment


                          • #14
                            So here's the changes:


                            Code:
                            ASSIGN/V1=C2.INPUT
                                            ASSIGN/V2=LOC1.T.DEV
                                            ASSIGN/V3=LOC6.X.DEV
                                            ASSIGN/V4=LOC12.Z.MEAS
                                            ASSIGN/VALL=V1+" , "+V2+" , "+V3+" , "+V4
                            VEXSIT1    =FILE/EXISTS,C:\DATAFOLDER\\DATADUMPFILE2.CSV
                                           IF/VEXIST1==0
                            FPTR1       =FILE/OPEN,C:\DATAFOLDER\\DATADUMPFILE2.CSV,WRITE
                                           FILE/WRITELINE,FPTR1,"SETMASTER_CTQ"
                                           FILE/WRITELINE,FPTR1,"Run #,T,X,Z"
                                           FILE/CLOSE,FPTR1,KEEP
                                           END_IF/
                            FPTR2       =FILE/OPEN,C:\DATAFOLDER\DATADUMPFILE2.CSV,APPEND
                                           FILE/WRITELINE,FPTR2,VALL
                                           FILE/CLOSE,FPTR2,KEEP
                            This also seems to be overwriting. I only get one line of data even though the serial number keeps changing for each run I try.
                            I can explain it to you, but I can't understand it for you.

                            Comment


                            • #15
                              Correct Josh.
                              I can run the program four times (serial number 1,2,3...)
                              Open the excel file and only one data line of whatever serial number I ran last.
                              I can explain it to you, but I can't understand it for you.

                              Comment

                              Related Topics

                              Collapse

                              Working...
                              X