Guys,

Am trying to open a file and read the content in that using C from mainframes.

Here is the code,

#include<stdio.h>                    
int main()                           
{                                    
int i;                               
FILE *fp1;                           
fp1=fopen("**********.CPP.SAVE","w");   
if(fp1==NULL)return;                 
for(i=0;i<256;i++)                   
{                                    
fprintf(fp1,"%c",i);                 
}                                    
fclose(fp1);                         
return 0;                            
else { printf("File does not exist");
}                                    
}   

Only the else part is been printed.

Is the fopen syntax right in zOS.
Suggestions needed.

Thanks & Regards,
AK

Recommended Answers

All 3 Replies

fp1=fopen("**********.CPP.SAVE","w");

This is a little confusing, are you trying to use wildcards or placeholders to pick up any file matching that pattern? If so, fopen() doesn't do that. If it happens, that would be an extension provided by your compiler that you need to confirm rather than asking people who are unlikely to be familiar with your platform.

If fopen() on your compiler doesn't do pattern matching, which is a good assumption, you'll need to use some non-standard functions to read the files in the directory, check them against your pattern, and open/process them each in turn.

That doesn't look like compilable code to me. What if statement does the else belong to? Can't be line 7 because that's not a multi-line statement that needs { and }. Lines 14 and 15 are just junk.

you should learn some common white spacing to help make the code readable. but this may help out some problems.

i believe your code was getting confused with where each if statement was ending. using {} can help specify that problem and make things easier to read.

  1. your first if statement did not look right at all as it would end the fuction if there is nothing there, skipping your else statement
  2. i was started as a int, and you have it printing as a %c in your for loop, so i changed it.

this will take a file, say that if it doesnt exist and end the code. if it does the code will print 256 characters from the code.

    #include<stdio.h>
    int main()
    {
        char i;
        FILE *fp1;
        fp1=fopen("**********.CPP.SAVE","w");

        if(fp1 == NULL)
        { 
            printf("File does not exist");
            return 0;
        }

        for(i=0;i<256;i++)
        {
            fprintf(fp1,"%c",i);
        }

        fclose(fp1);
        return 0;
    } 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.