Having trouble passing parameters

This is a discussion on Having trouble passing parameters within the C Programming forums, part of the General Programming Boards category; Hi i am having some trouble passing parameters to a function. I worked out this code in the main but ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    53

    Having trouble passing parameters

    Hi i am having some trouble passing parameters to a function. I worked out this code in the main but now i am trying to tidy up my code to make it more readable.
    Please ignore the magic numbers or unused variables at the moment. Also all files are linked correctly between each other just not shown in Code.

    Code:
    int main(int argc, char* argv[])
    {
        char *menuData;
        char menuBuffer[1000], subMenuBuffer[500];
        char *subMenuData;
        int temp, validation, count = 0;
        GJCType menu;
    
    
        if (argc != IMPORT) {
            printf("usage: %s menu.dat submenu.dat", argv[0]);
        } else {
    
            FILE *menuFile = fopen(argv[1], "r");
            FILE *subFile = fopen(argv[2], "r");
    
            if ((validation = fileImporter(menuFile, menuBuffer[0])) != TRUE) {
                fprintf(stderr, "Buffer inport failed \"menuFile\"");
            }
            if ((validation = fileImporter(subFile, subMenuBuffer[0])) != TRUE) {
                fprintf(stderr, "Buffer import failed \"subfile\"");
            }
    
        }
    
        return EXIT_SUCCESS;
    }
    other file.

    Code:
    int fileImporter(FILE* file, char* buffer)
    {
        int temp;
        int count = 0;
        if (file == FAILED) {
            printf( "Could not open menu.dat\n");
            return FALSE;
        } else {
            while ((temp = fgetc(file)) != EOF || !feof(file)) {
                printf(temp);
                buffer[count] = temp;
                count++;
            }
            buffer[count] = '\0';
            fclose(file);
            return TRUE;
        }
    }
    Code compiles fine but when executed the program crashes.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) Check your file opens in the first file, right after you open them and it's NULL not FAILED... not the second one.
    2) I trust you wrote a .h file prototypeing your function and included it in your first file.
    (Frankly, with such a trivial amount of code, I'd just put FileImporter() above main in the first file and call it a day)

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
            if ((validation = fileImporter(menuFile, menuBuffer[0])) != TRUE) {
                fprintf(stderr, "Buffer inport failed \"menuFile\"");
            }
            if ((validation = fileImporter(subFile, subMenuBuffer[0])) != TRUE) {
                fprintf(stderr, "Buffer import failed \"subfile\"");
            }
    Remove that.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    53
    Quote Originally Posted by quzah View Post
    Code:
            if ((validation = fileImporter(menuFile, menuBuffer[0])) != TRUE) {
                fprintf(stderr, "Buffer inport failed \"menuFile\"");
            }
            if ((validation = fileImporter(subFile, subMenuBuffer[0])) != TRUE) {
                fprintf(stderr, "Buffer import failed \"subfile\"");
            }
    Remove that.


    Quzah.
    ?? this calls the function

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    53
    The code was failing at the buffer[count] = temp line in the ImportFile function

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by TonyG View Post
    ?? this calls the function
    Why is it everyone is so confused by a simple "match the colors" exercise?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Quote Originally Posted by TonyG View Post
    The code was failing at the buffer[count] = temp line in the ImportFile function
    Actually, it's probably failing here:
    Code:
    printf(temp);
    You need to read the docs on printf. You don't get to pass it whatever you feel like. temp is an int, but printf needs a char * as the first parameter. You can pass your int/char for printing as a second parameter, so long as you give the right format string for the first parameter.

    EDIT: And you still need to follow Quzah's advice from post #3. He's telling you to remove the magenta text only. The [0] passes in a single char. You want to pass in a pointer to the first element, so use the name of the array only.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    53
    Probably because we are learning. I don't even know what Match the Colors exercise means?

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    53
    printf(temp);
    Sorry that was only in there for debugging. It was still failing with out it. Ignore that line

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    You still need to follow Quzah's advice from post #3. He's telling you to remove the magenta text only. The [0] passes in a single char. You want to pass in a pointer to the first element, so use the name of the array only, like so:
    Code:
    if ((validation = fileImporter(menuFile, menuBuffer)) != TRUE) {
    Notice how I just use menuBuffer instead of menuBuffer[0]? That's what he meant.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by TonyG View Post
    Probably because we are learning. I don't even know what Match the Colors exercise means?
    Seriously? What is confusing about me highlighting a specific spot of your code in color, and then saying remove that? I give up. You're too dumb for this.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    53
    ...Sorry for thinking to hard on something. Have only been at my computer for 7 hrs straight coding.

    Quote Originally Posted by quzah View Post
    Seriously? What is confusing about me highlighting a specific spot of your code in color, and then saying remove that? I give up. You're too dumb for this.


    Quzah.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TonyG View Post
    Probably because we are learning. I don't even know what Match the Colors exercise means?
    He said to take out the text he marked in that ugly arsed purple he likes so much.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TonyG View Post
    ...Sorry for thinking to hard on something. Have only been at my computer for 7 hrs straight coding.
    Ahhh... working a short day I see.... Must be nice.

  15. #15
    Registered User
    Join Date
    May 2011
    Posts
    53
    Glad im not the only one that felt
    Seriously? What is confusing about me highlighting a specific spot of your code in color, and then saying remove that? I give up. You're too dumb for this.
    was a little out of line. Guess Aunty Flow is in town.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble understanding 'for' loop parameters
    By spottedzebra in forum C Programming
    Replies: 11
    Last Post: 06-23-2010, 08:23 PM
  2. Help..having trouble passing parameters
    By paret in forum C Programming
    Replies: 6
    Last Post: 12-02-2009, 07:24 PM
  3. Passing Parameters
    By fry in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2002, 03:06 AM
  4. Passing parameters
    By pdstatha in forum C++ Programming
    Replies: 1
    Last Post: 06-30-2002, 10:07 AM
  5. Passing parameters to a module
    By Sue Paterniti in forum C Programming
    Replies: 25
    Last Post: 05-09-2002, 06:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21