C Functions and varibles

This is a discussion on C Functions and varibles within the C Programming forums, part of the General Programming Boards category; im working on a little text to html conversion program. i have a function to create a title(1 param), function ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    Exclamation C Functions and varibles

    im working on a little text to html conversion program. i have a function to create a title(1 param), function to chacge inputted name into a output-able name(t param, old filename and extension) and the function that does the work( 3 params, input file, output file and the title). when ever i call the main function using separte functions, all the varibles are the same. why!?!?!?!?
    i have tried this a couple of different ways, and the funny part is it has worked in a function before(a function to chage the inputted filename to a output-able file).

    heres some unfinished code of it
    ==============================================
    # include <stdio.h>
    # include <string.h>
    # include <ctype.h>

    int main(int argc, char *argv[]) {
    process(argv[1], convertfileext(argv[1], ".html") );
    }

    int convertfileext(char name[], char ext[]) {
    int x = strlen(name);
    int converted = 0;

    while(name[x] != EOF) {
    if(name[x] == '.') {
    name[x] = '\0';
    name = strcat(name, ext);

    converted = 1;
    break;
    }

    --x;
    }

    if(converted == 0)
    name = strcat(name, ext);

    return name;
    }

    int createtitle(char filename[]) {
    int x = 0;

    filename[0] = toupper(filename[0]);
    while(filename[x] != EOF) {
    if(filename[x] == '_')
    filename[x] = ' ';
    else if(filename[x] == ' ')
    filename[x+1] = toupper(filename[x+1]);

    ++x;
    }

    return filename;
    }

    int process(char path[], char newfile[]) {
    printf("\n%s\n", path);
    printf("%s\n", newfile);

    return 0;
    }

    ==============================================
    the process function prints newfile out twice for me. i tried this on windows xp and in red hat linux 7.3, what is happening here!?!?!?

    thanks

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    What if you had passed argv[0] to process() and convertfileext()?

  3. #3
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by The Dog
    What if you had passed argv[0] to process() and convertfileext()?
    argv[0] is the executable that's running itself.

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    thanx. i never use command line args.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    so does any one know why all my varibles end up the same? i have read some books on C before and it been done there, it even done in one of mu functions. what is going on?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,529
    > what is going on?
    You're modifying argv[1], and printing it out - there is only one string present here, so it's going to always print the same answer.

    Also, modifying argv[i] strings is not something I would normally recommend.

    Code:
    # include <stdio.h> 
    # include <string.h> 
    # include <ctype.h> 
    
    // function prototypes are your friends, get to know them
    char *convertfileext(char name[], char ext[]);
    void process(char path[], char newfile[]);
    
    int main(int argc, char *argv[]) {
        char    new[100];
        strcpy( new, argv[1] );
        process(argv[1], convertfileext(new, ".html") ); 
    } 
    
    char *convertfileext(char name[], char ext[]) {
        char    *p;
        p = strrchr( name, '.' );   // find the last .
        if ( p != NULL ) {
            strcpy( p, ext );       // replace current .ext with the new one
        } else {
            strcat( name, ext );    // no .ext, so just add the new one
        }
        return name; 
    } 
    
    void process(char path[], char newfile[]) { 
        printf("\n%s\n", path); 
        printf("%s\n", newfile); 
    }

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    i thought when you pass a arg to a function only a copy goes to the function, why isnt using argv[1] a good idea.

    thanks for the post

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,529
    > i thought when you pass a arg to a function only a copy goes to the function
    It is when the thing you are passing to a function isn't an array. Strings are arrays of char, so all you get is a pointer to the start of the array.

    > why isnt using argv[1] a good idea.
    Because you've already fallen into the trap - you modified it, and got confused.

    Also, you don't know how the memory is allocated. Making a string shorter is OK, but making it longer (with say strcat) is a definite NO, since some of the chars will end up being written to someplace you don't own.

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    thanks alot for the info. its been bugging me for awhile

  10. #10
    Unregistered
    Guest

    Exclamation

    sorry to bug you guys again but any thing in my program just doesnt want to work. i want to take input in allmost like a shell to a function but when a special word is typed in the program exits. heres what i have(yes, all the function work fine but my error message is still showed(function error()).
    ================================================
    int main(int argc, char *argv[]) {
    char file[255];
    char file2[255];

    if(argv[1] != (NULL)) {
    int x = 1;

    while(argv[x] != NULL) {
    strcpy(file, argv[x]);
    strcpy(file2, argv[x]);

    printf("Converting %s ", argv[x]);
    if( (process(argv[x], convertfileext(file, ".html"), createtitle(file2))) == 0)
    printf("...Done.\n");

    ++x;
    }
    } else {
    char input[255];
    char exitnow[5] = "exit";

    error();

    while((strcmp(input, exitnow)) != 0) {
    scanf("%s", input);
    strcpy(file, input);
    strcpy(file2, input);

    printf("Converting %s ", input);
    if( (process(input, convertfileext(file, ".html"), createtitle(file2))) == 0)
    printf("...Done.\n");
    }


    }
    }
    ================================================
    shouldnt the code that could call the function never used if exit it typed?

  11. #11
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    i forgot to mention that function process calls error() if the file isnt found/readable. but still why is error being called?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by mart_man00
    i forgot to mention that function process calls error() if the file isnt found/readable. but still why is error being called?
    Um, because you keep calling it? You really should try using code tags:

    [code]
    ...your code here...
    [/code]

    Like so:

    Code:
    int main(int argc, char *argv[]) {
        char file[255];
        char file2[255];
    
        /**
        *** Replace this:
    
        if(argv[1] != (NULL)) {
    
        ***With this:
        **/
        if( argc > 1  ) {
            int x = 1;
    
            /**
            *** And this:
    
            while(argv[x] != NULL) {
    
            *** With this:
            **/
            for( x = 0; x < argc; x++ ) {
                strcpy(file, argv[x]);
                strcpy(file2, argv[x]);
                printf("Converting %s ", argv[x]);
    
                if( (process(argv[x], convertfileext(file, ".html"), createtitle(file2))) == 0)
                    printf("...Done.\n");
            }
        } else {
            char input[255];
            char exitnow[5] = "exit";
    
            /**
            *** And here is your error() call.
            **/
            error();
    
            while((strcmp(input, exitnow)) != 0) {
                /**
                *** Personally, I'd drop scanf()
                *** and use fgets(), but that's
                *** just me.
                **/
                scanf("%s", input);
                strcpy(file, input);
                strcpy(file2, input);
    
                printf("Converting %s ", input);
                if( (process(input, convertfileext(file, ".html"), createtitle(file2))) == 0)
                    printf("...Done.\n");
            }
        }
        /**
        *** Since this is main(), which is
        *** always a non void function,
        *** the only polite thing to do is
        *** to return something.
        **/
        return 0;
    }
    So much cleaner now...

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

  13. #13
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    thanks

  14. #14
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    i just noticed, it still doesnt work. i just changed the upper code to what you showed me. that one error call should be there so the user is told what to do. why does the while loop not stop when strcmp doesnt return 0?

    by the way why wouldnt you use scanf? i new, just wondering.

    thanks again guys for your posts

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sharing varibles with DLLs
    By ZeroMemory in forum Windows Programming
    Replies: 11
    Last Post: 11-07-2008, 08:59 AM
  2. problems storing values in varibles
    By stodd04 in forum C Programming
    Replies: 7
    Last Post: 02-08-2005, 10:56 AM

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