Thread: Passing parameters to a module

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    Passing parameters to a module

    I have a couple of programs which open files, and need to create a module which has the fopen code - and then pass the file and mode parameters to the module.

    I have included code in my calling pgm as
    Code:
    fp = opentextfile(argv[1],"rb";
    and in the module have included

    Code:
    int opentextfile(name_type filename, mode_type mode) {
       fopen(filename, mode);
    1. I would like to return something if the file does not open? How can I return the NULL? Is this the correct way to do this?
    How do I access the returned integer from the calling pgm to then exit the pgm is the file has not been opened?

    Many thanks
    Sue

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    If you have a pointer, you can return NULL. For example,
    Code:
    char *ReturnString(){
         char* String = "Test"
         String = NULL
         return String;
    }
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    1. I would like to return something if the file does not open? How can I return the NULL? Is this the correct way to do this?
    How do I access the returned integer from the calling pgm to then exit the pgm is the file has not been opened?
    If return 0 = the program returned 0.
    If return 1 = the program returned 1.

    You can create a quick batch file to test if your porgram returns status' properly.
    If the program your trying the capture the output of returns status' properly, then your in buisness. If it doesn't, your stuck.

    Batch file:
    Code:
    MyPgmToTest.exe
    if errorlevel 1 echo Program errored
    if errorlevel 0 echo Program executed properly
    MyPgmToTest.exe is the program you will test the return status of. When the program returns 0 it generally means nothing was returned and everything went smoothly. If it returns 1 it generally means something went wrong. You could convert this into C code. It is similar to file existance checking and the errorlevel checking of a system call. If the program you are executing with system returns status' properly then you can capture the return status.

    Examples:

    Return status of file exsitance checking:
    Code:
    FILE * Tmp;
    if ( ( Tmp = fopen("foo.txt", "r") ) == 0 )
    {
        printf("file doesn't exist");
    }
    else
    {
        printf("File does exist");
    }
    Return status of program executions:
    Code:
    if(system("foo.exe") == 1)
    {
        printf("an error occured");
    }
    else
    {
        printf("Everything went fine");
    }
    P.S. this was done fairly quickly so don't think to highly of it, it should present some options though..
    Last edited by Shadow; 05-07-2002 at 10:35 PM.
    The world is waiting. I must leave you now.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Passing parameters to a module

    Originally posted by Sue Paterniti

    1. I would like to return something if the file does not open? How can I return the NULL? Is this the correct way to do this?
    How do I access the returned integer from the calling pgm to then exit the pgm is the file has not been opened?
    Have a look at this code.
    Code:
    #include <stdio.h>
    
    FILE *OpenFile(char *, char *);
    
    int main(void)
    {
    	FILE *fp;
    	
    	if ((fp = OpenFile("myfile.txt", "r")) == NULL)
    	{
    		perror("myfile.txt");
    		return (1);
    	}
    	
    	printf("file opened successfully");
    	
    	fclose(fp);
    	
    	return (0);
    }
    
    FILE *OpenFile(char *fn, char *mode)
    {
    	FILE *fp;
    	
    	fp = fopen(fn, mode);
    
    	/* assumes error checking is done externally to the is function */	
    	return (fp);
    }
    In OpenFile(), the file pointer is returned. If the fopen() failed, it value of fp will be NULL, and the calling function catches the error. It then uses perror() to print a meaningful error message.
    perror() uses the int variable errno, which you can also access yourself if you want.

    In my code, there's no real point in having the fopen() in a seperate function, you can just call fopen() from within main(). It just serves an example.

    Also, if your ever need to return null:
    >return (NULL);

    Lastly, the exit code from main() shouldn't be >255 if you want to trap it in the calling shell (DOS or UNIX). I am pretty sure those environments only use 1 byte to determine the exit code of a program, hence the 255 limit.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    File opens from a module

    Thanks Hammer,

    I am having trouble with this pgm....the requirement is to have a pgm access a module -

    Can you please tell me -
    I have labelled my variables in the module (mod.c) as static - if I open the file in mod.c - will it be available next time I return to mod.c to say run the stat - or write the output data?

    or - do I have to open the file in the module - return the FILE - and then pass the FILE back to the module when I call the stat function in the module?

    I guess the question is - once I open a file - does it stay available?


    I have included the following:

    in the calling pgm:

    Code:
    fileerror = filebinaryopen(argv[1], "wb");
       if ((fileerror == (1)) {
       exit(1); }
    in the module

    [code]
    int filebinaryopen(name_type file1, mode_type outmode) {
    if ((fp1 = fopen(file1, outmode)) == NULL )
    {e = 1; }
    return(e);
    }

    but when I run it I am getting a segmentation fault...

    thanks for your help

    Sue

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    more: open file from a module

    Hammer,

    I have tried to incorportate some of your suggestions, but although the module compiles - I am getting "incompatible types in assignment" compile errors from the calling pgm.

    In the module header I have as follows:

    Code:
    exterb FILE filebinaryopen(name_type filename, mode_type outmode);
    in the module c I have as follows:
    Code:
    FILE filebinaryopen(name_type filename, mode_type outmode) {
       fp = fopen(filename, outmode);
       return(*fp); }
    in the calling pgm I have as follows:

    Code:
    if ((fp = filebinaryopen(argv[2], "wb")) == NULL 
    {
       perror("argv[2]");
       exit(1);
    }
    can you suggest what i am doing wrong??

    thanks
    sue

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I can see where you going with this now . Took it a bit of time going into me head, but I'm there now!

    Right, your function declarations are incorrect.
    Code:
    FILE filebinaryopen(name_type filename, mode_type outmode) {
       fp = fopen(filename, outmode);   return(*fp); }
    Here you say you should be returning FILE, but infact you are returning FILE*, a pointer. To fix this change this to:
    Code:
    FILE *filebinaryopen(name_type filename, mode_type outmode) {
       fp = fopen(filename, outmode);   return(*fp); }
    Depending on what you are going to do with the file pointer, you could return go about the return int method, but I'd strongly suggest not. The better way is to return the FILE*, that way the caller can pass the same file pointer to another function.

    For example, if you only returned an int denoting success/failure, how would you go about using the FILE* you had just opened? You couldn't, is the simple answer.

    This may be irrellevant now, but in your code that seg faults, what is the e variable about? It doesn't seem to be declared.

    Also, extern is spelt with a b , but I'm sure you knew that anyways.
    >exterb FILE filebinaryopen(name_type filename, mode_type outmode);

    Lastly, if you have a function called ...binaryopen, why do you need to pass the mode parameters? Surely you know it's "wb", or maybe "rb". Just a thought.....
    Last edited by Hammer; 05-09-2002 at 03:26 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    Thanks Hammer,

    I will try as you suggest....

    in regards the passing of the file mode - yes, of course - I don't need to pass that.....

    Thanks for your assistance

    I will have another go....
    Sue

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    Hammer,

    Hmm, no luck - I am still getting compile errors "incompatible types on return"

    The code I have is as follows:

    in the module header:

    Code:
    extern FILE * filebinaryopen(name_type file1);
    in the module body (mod.c)

    Code:
    FILE * filebinaryopen(name_type file1) {
       fp = fopen(file1, "wb");
       return(*fp1):
    }
    ? should there be a space between the * and the module name (ie filebinaryopen). I have tried both, but no difference.
    any suggestions

    Thanks
    Sue

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Doh! Didn't spot your other minor mistake the first time round!
    Code:
    FILE * filebinaryopen(name_type file1) {   fp = fopen(file1, "wb");
       return(fp1):}
    Note the return(fp1); bit. You must return the pointer.

    Looking again though... where is fp defined?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    Some joy!!!!!

    I removed the * from the return(*fp) - and it compiled and got a bit further in the pgm....

    Can you please advise how I can pass the file pointer returned from the open to another function....

    ie I want to create a function to collect the stat information for each file record contained in the file just opened - so far I have

    Code:
    static name_type in_file;
    
    ???? collectstat(FILE *fp) {
       while (fscanf(fp, "%s", in_file) != EOF) {
                    if (access(in_file,0) == 0) {
                    stat(in_file, &file_info); 
                    return (file_info);  }
        }
    }
    the definition of file_info is
    static struct stat file_info; What would be the return type??

    I am getting lots of parse errors and conflicting types -

    From the calling pgm I have
    collectstat(fp);

    Once I am able to return the file_info struct - what would be the syntax to pass it back into a "writedata" function in the module?

    Thanks for your help.
    Sue

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    The fp is defined in the calling pgm
    ie FILE *fp;

    and also in the mod.c module code
    static FILE *fp
    Sue

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I presume that in the file pointed to by fp is a list of filenames to go do stat()'s on? If so, you will need an array to hold the results of the stat() calls (if you only have one struct, it'll only hold only files worth of data).

    Here's a suggestion (follow it if you want, there are many ways to do what you ask):

    - In main (or the parent function), create an array of struct stat's.
    - Open the config file.
    - Pass fp, the array pointer, and the array size to the collectstat() function.
    - collectstat() then reads the config, doing it's stat calls. It uses a local struct stat to store it's initial results.
    - As each stat() is successfully performed, copy the local stat to the next free element in the array.
    - If a stat() call fails, issue a warning or whatever. I don't know if what the local stat struct will hold in this event, but it probably won't be valid, so you best ignore it. Maybe research that one a bit more.
    - Keep a count of how many successful stat()'s you do.
    - collectstat() then returns the count of stat()'s successfully called.
    - main() can then process the array as it wants, from element 0 to element N (as returned from collectstat() ).

    To write out the data, create another function that takes a pointer to the array of structs (and the number of elements used).

    It's probably all a little more complicated than you originally thought, but hey, it's good learning.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    wow!!!

    I'm not sure if I can process all that.....

    yes, the input file is a text file that contains the names of files to be stat'd (one per record).

    I was hoping to be able to read a record, perform the stat, and write a record to the binary output file.....

    So far, we have managed to successfully open both the input and output files.....the next step would be to call the function collectstat from the module, and then call the writedata function.

    If I have static'd the file_info - could I expect it to still be there on return to the module? That would eliminate the necessity of passing the file_info back and forward?

    Do you think this would work - it sounds simpler than arrays - I am afraid - you see!!

    Thanks
    Sue

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK... sorry, I got carried away with myself!

    The idea of a static variable is that the next time you enter the function, it will still hold the same data as when you last left that function. You can also return a pointer to that variable so that you can use it externally to the function it's defined in.

    So, try this:
    Code:
    struct stat *collectstat(FILE *fp)
    {
    	static struct stat file_info;
    	char in_file[1024]; // or whatever length
    	// this doesn't print error messages if anything fails.	
    	if (fgets(in_file, 1024, fp))
    	{
    		if (access(in_file,R_OK) == 0)
    		{
    			if (stat(in_file, &file_info) != -1)
    				return (&file_info);
    		}
    	}
    	return (NULL);
    }
    You'll need to call this multiple times to get each line. The caller can use the struct stat pointer (first testing to ensure it's not null). Let me know how you go.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  2. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  3. Passing parameters to modal dialogs
    By Halloko in forum Windows Programming
    Replies: 2
    Last Post: 10-11-2005, 07:15 AM
  4. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 03:13 AM
  5. Function basics
    By sjleonard in forum C++ Programming
    Replies: 15
    Last Post: 11-21-2001, 12:02 PM