ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

C_IFS_FWRITE and C_IFS_FOPEN

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

  • C_IFS_FWRITE and C_IFS_FOPEN

    Hello,

    I'm using C_IFS_FWRITE and C_IFS_FOPEN to write some CSV files in the IFS.
    It creates the files in the IFS and I can see the content, the ccsid is 37.
    If I download these to my laptop and try to open they show invalid data.
    ​​​​​​​How do I set the correct CCSID?

  • #2
    How does it looks like if your open it via WRKLNK?
    How do you download it to your laptop via ACS, FTP?
    CCSID 37 is fine in general. But if you want to open it from your PC you need a translation to ASCI or UTF-8.
    FTP can do this automatically.

    You can change the CCSID by:
    HTML Code:
    CHGATR OBJ('your/file') ATR(*CCSID) VALUE(1208)
    But this only changes the CCSID attribute, not the content.
    You need to convert the content to UTF-8 bevore your write it to the IFS file.

    I prefer using SQL to get this job done.
    One is the SQL Procedure:

    HTML Code:
    CALL QSYS2.IFS_WRITE_UTF8('/your/file.txt', 'UTF-8 Content', END_OF_LINE => 'CRLF')
    Another one is in RPG:

    Code:
    DCL-S l_ClobFile SQLTYPE(Clob_File) CCSID(1208);
    DCL-S L_Text Char(50);
    
    Clear l_ClobFile;
    
    l_ClobFile_Name = '/your/file.txt';
    l_ClobFile_NL = %Len(%Trim(l_ClobFile_Name));
    l_ClobFile_FO = SQFOVR; //Replace existing files
    
    L_Text = 'Your UTF-8 content';
    
    Exec SQL Set :l_ClobFile = :L_Text;
    ?

    Comment


    • #3
      Depends. If the file already exists, you can change it's CCSID via the CHGATR command (as shown by Andreas Prouza).

      If it doesn't exist and you wish to create it with the IFS version of the fopen() routine (i.e. _C_IFS_fopen) then you would handle it like this:

      Code:
      // creae the file with CCSID 1208 (UTF-8)
      
      myfile = fopen('/path/to/file': 'w, o_ccsid=1208');
      fclose(myfile);
      
      // open it in text mode to translate from the job CCSID
      
      myfile = fopen('/path/to/file': 'w, o_ccsid=0');
      // write to file here
      fclose(myfile);
      The first call to fopen() just creates the file and assigns ccsid 1208 to the file object. You don't write anything at that point (because it would assume that your program is also using CCSID 1208 if you did -- and most likely the program is EBCDIC -- so you just close it).

      The second call to fopen() says that the program is using CCSID 0, which is a special value meaning "my job's CCSID". Since the file already exists and is CCSID 1208, it will automatically translate from the job CCSID to 1208 when writing. Though, I recommend using fputs() insterad of fwrite().

      Comment


      • #4
        Originally posted by Scott Klement View Post
        Depends. If the file already exists, you can change it's CCSID via the CHGATR command (as shown by Andreas Prouza).

        If it doesn't exist and you wish to create it with the IFS version of the fopen() routine (i.e. _C_IFS_fopen) then you would handle it like this:

        Code:
        // creae the file with CCSID 1208 (UTF-8)
        
        myfile = fopen('/path/to/file': 'w, o_ccsid=1208');
        fclose(myfile);
        
        // open it in text mode to translate from the job CCSID
        
        myfile = fopen('/path/to/file': 'w, o_ccsid=0');
        // write to file here
        fclose(myfile);
        The first call to fopen() just creates the file and assigns ccsid 1208 to the file object. You don't write anything at that point (because it would assume that your program is also using CCSID 1208 if you did -- and most likely the program is EBCDIC -- so you just close it).

        The second call to fopen() says that the program is using CCSID 0, which is a special value meaning "my job's CCSID". Since the file already exists and is CCSID 1208, it will automatically translate from the job CCSID to 1208 when writing. Though, I recommend using fputs() insterad of fwrite().
        It was missing the o_ccsid!

        Thanks!

        Comment

        Working...
        X