ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

C++ Program to read from IFS

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

  • C++ Program to read from IFS

    While I do realize we can call the C functions from a RPG program to read the IFS files, I am trying to write a pure C/C++ code to read a file from the IFS and having a bit of a problem

    Following is my code
    Code:
    #include<fstream.h>
    void main()
    {
       fstream f1,f2;
       char input;
       f1.open("\dev\myfolder\test.txt",ios::in);
       f1>>input;
       cout<<input;
       f1.close();
    
       f2.open("\dev\myfolder\test1.txt",ios:out);
       f2<<"this is test text";
       f2.close();
    }
    Now my issue here is when reading the file opened using f1, I am not getting the required characters. However if I open test1.txt using wrklnk or notepad, I see the text correctly.

    Edit1: One possible way I found is that my C++ program is able to read files with CCSID of 37 only. The others are giving me garbage characters as its not able to recognize it.

    Any other way around this?

  • #2
    Re: C++ Program to read from IFS

    Hello,

    I'm not a C++ programmer but I gave it a try a few years ago, and from what I see in my example you have to use ios::text like that

    Code:
    f1.open("\dev\myfolder\test.txt",ios::in||ios::text);
    Nicolas

    Comment


    • #3
      Re: C++ Program to read from IFS

      Thanks a lot for the info. I finally got it to work by actually accessing the C functions with a RPG program since it seemed like my pure C code will not suffice for what I wanted to do.

      Comment


      • #4
        Re: C++ Program to read from IFS

        Can you post a snippet of what you ended up with?

        jamie
        All my answers were extracted from the "Big Dummy's Guide to the As400"
        and I take no responsibility for any of them.

        www.code400.com

        Comment


        • #5
          Re: C++ Program to read from IFS

          Originally posted by vikramx
          While I do realize we can call the C functions from a RPG program to read the IFS files, I am trying to write a pure C/C++ code to read a file from the IFS and having a bit of a problem

          Following is my code
          Code:
          #include<fstream.h>
          void main()
          {
             fstream f1,f2;
             char input;
             f1.open("\dev\myfolder\test.txt",ios::in);
             f1>>input;
             cout<<input;
             f1.close();
          
             f2.open("\dev\myfolder\test1.txt",ios:out);
             f2<<"this is test text";
             f2.close();
          }
          Now my issue here is when reading the file opened using f1, I am not getting the required characters. However if I open test1.txt using wrklnk or notepad, I see the text correctly.

          Edit1: One possible way I found is that my C++ program is able to read files with CCSID of 37 only. The others are giving me garbage characters as its not able to recognize it.

          Any other way around this?
          If I understand what you are trying to do, then you are missing one step in your C program. You have to perform an ascii to ebcdic conversion on a file that is in ascii format. The reason for this is that, even though you are using C to do this, the platform in which your C program is running is EBCDIC. So, by default, it believes its going to be reading in EBDIC. If the file is in ascii format, you will have to read it, then translate the contents from ascii to ebcdic.

          The reason a CCSID 37 file works is because that is ebcdic, and no translation is needed.
          Michael Catalani
          IS Director, eCommerce & Web Development
          Acceptance Insurance Corporation
          www.AcceptanceInsurance.com
          www.ProvatoSys.com

          Comment


          • #6
            Re: C++ Program to read from IFS

            As kind of a backwards example, look at this code:

            Code:
                                                                                          
            /* COMPILE OPTIONS  -  CRTBNDC SYSIFCOPT(*IFSIO)                          */  
                                                                                          
            #include <stdio.h>                                                            
            int main(void)                                                                
            {                                                                             
                FILE *fd1;                                                                
                char SomeData[256];                                                       
                int x;                                                                    
                fd1 = fopen( "/ifstest/newfile.txt" , "w" );                              
                fprintf( fd1 , "Some Test Data \n" );                                     
                fclose(fd1);                                                              
                fd1 = fopen( "/ifstest/newfile.txt" , "r" );                              
                fgets( SomeData, 256, fd1 );                                              
                                                                                          
                printf( "Data From File: %s\n" , SomeData );                              
                   printf( "\n" );                                                        
                   fclose( fd1 );                                                         
               }
            This will write data to a file on the IFS. (You would have to create a folder on the IFS named "IFSTEST" for it to work. And Make sure to see the compile options at the top of the program.) Data will be written to the file in EBCDIC. If you use notepad on this file, it will show garbage. If you copy this garbage data into an online translator (ie http://www.pacsys.com/ebccnv.htm) it will show you the correct data.
            Michael Catalani
            IS Director, eCommerce & Web Development
            Acceptance Insurance Corporation
            www.AcceptanceInsurance.com
            www.ProvatoSys.com

            Comment


            • #7
              Re: C++ Program to read from IFS

              Thanks Michael. I thought it was a translation problem but felt that if the C APIs within the RPG could read it, the normal C program could also be able to.

              @Jamie
              I basically used your method to access IFS files using the C API functions (read, open, close). Using a similar logic to your readifs2 program you posted on the thread on multi-format csvs (Hope you didnt mind me taking a little hint from you).

              Code:
              0003.00,D OpenFile        PR            10i 0 Extproc('open')
              0004.00,D                                 *   Value
              0005.00,D                               10i 0 Value
              0006.00,D                               10u 0 Value Options(*NOPASS)
              0007.00,D                               10u 0 Value Options(*NOPASS)
              0008.00,
              0009.00,D ReadFile        PR            10i 0 Extproc('read')
              0010.00,D                               10i 0 Value
              0011.00,D                                 *   Value
              0012.00,D                               10u 0 Value
              0013.00,
              0014.00,D CloseFile       PR            10i 0 Extproc('close')
              0015.00,D                               10i 0
              Rest of it is similar to your readifs2 program except that it handles all kinds of delimiters (provided its given correctly by the user).

              Basically what I was trying to do is read a file from IFS (Could be csv, text tab delimited or any kind of delimited file) and convert this into a PF by automatically generating the DDS based on the contents within the IFS file. This program reads the IFS file (based on parameters passed), generates the ideal DDS (each field length if based on the maximum length of data within the specific column), creates the physical file and copies the data into the physical file using the CPYFRMIMPF command.

              Of course the limitation here is that the DDS will only have character fields since I still see no way of determining what type of field it should be.
              Last edited by vikramx; July 8, 2011, 12:12 AM.

              Comment

              Working...
              X