none
How to read a text file?

    Question

  • Hi I'm completely new to FoxPro with a little bit of programming experience in Java but not too much. I was recently given an assignment by someone attempting to help me learn Foxpro (he doesn't know anything about this program) and I need to know how to read a text file line by line until the end, and make a new text file.

    The specific problem:

    I need to take a text file full of dates in the format MM/DD/YYYY each on one line. I need to produce a separate text file with those dates in the format YYYYMMDD without slashes in between. For example if the first line was 07/01/2011 I would need to put 20110701 on the new text file.

    I think I can figure out how to change the dates into the different format, but I have no idea how to read and append lines from and to a text file. I know how to make a basic .prg file so that's all I'll need to be able to do.

    Any kind of help will be greatly appreciated!

    Friday, July 01, 2011 11:28 PM

Answers

  • You may look at low level file functions in the help (FOPEN, FREAD, FGETS, FPUTS, FCLOSE, ...) or you may use the power of data engine:

     

    SET DATE AMERICAN
    SET STRICTDATE TO 0
    CREATE CURSOR TempData (MyDate char(10))
    APPEND FROM YourTextFile.TXT TYPE SDF
    REPLACE ALL MyDate WITH DTOS(CTOD(MyDate))
    COPY TO NewTextFile.TXT TYPE SDF
    USE
    

     

     

    Saturday, July 02, 2011 7:05 AM
  •  

    That's a very specific solution, but strikingly simple. This shows off how good and rapid you can develop data related stuff in VFP, if you have the knowledge about the language.

    Let me explain a bit on the main ingredients used:

    The conversion from an american short date format MM/DD/YYYY into YYYYMMDD is done by DTOS(CTOD()). The inner CTOD() turns the american format into the native binary date type format of VFP (with american date setting), DTOS() turns that binary format to YYYYMMDD. I bet you'd figured out a way decomposing substrings and putting them back together, but not that one.

    Reading in a text file in the format you gave is easiest done by APPEND and the file type rather compares to SDF files, which are files with a record per line, no comma separation of values but constant field width. But even CSV with only a single field would match that definition. You have a problem, if you haven't told the whole story and the american dates are embedded in a more complex record, eg comma separeted. So report back, if that's what is stopping you to accept pavels solution as an answer, nitrobeat.

    Actually your job definition smells a bit like a preparation step to read the data file into vfp data, as the help on APPEND says about the import file type SDF: 

    "Effective conversion of date data from SDF files to Visual FoxPro tables requires data to be stored in YYYYMMDD format."

    So preparing a file that way makes it easy to consume with VFP and actually that should be done by whoever exports and creates the file you need to import and convert.

    But if the file type is CSV, there is no preprocessing needed. VFP can append any date format CTOD() can handle from delimited files:

    "You can import dates from delimited files if the dates are in proper date format. The date format defaults to mm/dd/yy. Including the century portion of a date is optional. Visual FoxPro will import a date, such as 12/25/95, that doesn't include the century and assumes the date is in the twentieth century. Date delimiters can be any non-numeric character except the delimiter that separates the fields in the delimited file.

    Dates in other formats can be imported if their formats match date formats available in SET DATE. To import dates that are not in the default format, issue SET DATE with the proper date format before using APPEND FROM. To test whether a date format can be successfully imported, use it with CTOD( ). If the date is acceptable to CTOD( ), the date will import properly."

    That means once you have set date american you could also import into a cursor with a date field: CREATE CURSOR TempData (MyDate D) and just set the date format before appending.

    That in mind you could also do 

    CD ("D:\Data\Import Data")
    SET DATE AMERICAN
    SET STRICTDATE TO 0
    CREATE CURSOR ImportData (dMyDate D)
    APPEND FROM SourceTextFile.TXT TYPE DELIMITED
    SELECT DTOS(dMyDate) As cMyDate FROM ImportData INTO CURSOR ExportData
    COPY TO ConvertedTextFile.TXT TYPE SDF
    USE IN ImportData
    USE IN ExportData

    One line more, but one step of conversion with CTOD() is already done by APPEND, so I assume it's a little faster. Also the export file will not have 10 but only the needed 8 chars per line plus CRLF.

    Bye, Olaf.





    Sunday, July 03, 2011 10:01 AM

All replies

  • You may look at low level file functions in the help (FOPEN, FREAD, FGETS, FPUTS, FCLOSE, ...) or you may use the power of data engine:

     

    SET DATE AMERICAN
    SET STRICTDATE TO 0
    CREATE CURSOR TempData (MyDate char(10))
    APPEND FROM YourTextFile.TXT TYPE SDF
    REPLACE ALL MyDate WITH DTOS(CTOD(MyDate))
    COPY TO NewTextFile.TXT TYPE SDF
    USE
    

     

     

    Saturday, July 02, 2011 7:05 AM
  •  

    That's a very specific solution, but strikingly simple. This shows off how good and rapid you can develop data related stuff in VFP, if you have the knowledge about the language.

    Let me explain a bit on the main ingredients used:

    The conversion from an american short date format MM/DD/YYYY into YYYYMMDD is done by DTOS(CTOD()). The inner CTOD() turns the american format into the native binary date type format of VFP (with american date setting), DTOS() turns that binary format to YYYYMMDD. I bet you'd figured out a way decomposing substrings and putting them back together, but not that one.

    Reading in a text file in the format you gave is easiest done by APPEND and the file type rather compares to SDF files, which are files with a record per line, no comma separation of values but constant field width. But even CSV with only a single field would match that definition. You have a problem, if you haven't told the whole story and the american dates are embedded in a more complex record, eg comma separeted. So report back, if that's what is stopping you to accept pavels solution as an answer, nitrobeat.

    Actually your job definition smells a bit like a preparation step to read the data file into vfp data, as the help on APPEND says about the import file type SDF: 

    "Effective conversion of date data from SDF files to Visual FoxPro tables requires data to be stored in YYYYMMDD format."

    So preparing a file that way makes it easy to consume with VFP and actually that should be done by whoever exports and creates the file you need to import and convert.

    But if the file type is CSV, there is no preprocessing needed. VFP can append any date format CTOD() can handle from delimited files:

    "You can import dates from delimited files if the dates are in proper date format. The date format defaults to mm/dd/yy. Including the century portion of a date is optional. Visual FoxPro will import a date, such as 12/25/95, that doesn't include the century and assumes the date is in the twentieth century. Date delimiters can be any non-numeric character except the delimiter that separates the fields in the delimited file.

    Dates in other formats can be imported if their formats match date formats available in SET DATE. To import dates that are not in the default format, issue SET DATE with the proper date format before using APPEND FROM. To test whether a date format can be successfully imported, use it with CTOD( ). If the date is acceptable to CTOD( ), the date will import properly."

    That means once you have set date american you could also import into a cursor with a date field: CREATE CURSOR TempData (MyDate D) and just set the date format before appending.

    That in mind you could also do 

    CD ("D:\Data\Import Data")
    SET DATE AMERICAN
    SET STRICTDATE TO 0
    CREATE CURSOR ImportData (dMyDate D)
    APPEND FROM SourceTextFile.TXT TYPE DELIMITED
    SELECT DTOS(dMyDate) As cMyDate FROM ImportData INTO CURSOR ExportData
    COPY TO ConvertedTextFile.TXT TYPE SDF
    USE IN ImportData
    USE IN ExportData

    One line more, but one step of conversion with CTOD() is already done by APPEND, so I assume it's a little faster. Also the export file will not have 10 but only the needed 8 chars per line plus CRLF.

    Bye, Olaf.





    Sunday, July 03, 2011 10:01 AM