Thread: functions won't work together

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    25

    functions won't work together

    Hi,

    I'm reading in a file called openInFile and then passing it line by line to two functions as follows....

    while (fgets(line, sizeof(line), openedInFile) != NULL) {
    test_4_globalVariable(line);
    test_4_function(line);
    }

    These two functions both work fine if I comment the other out, but if I try to call both of them, the first one works fine and the other produces strange results.

    Any ideas?


    Bill

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I guess line could be altered by the first calling function, and then it may produce the different reults in the second. As I havent got code for the functions - I dont know

    Are either of those functions altering parameters?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How did you declare line?

    If it's
    char *line;

    Then your code is broken


    Also, if you have

    test_4_globalVariable( char *line);
    test_4_function( char *line);

    Make them
    test_4_globalVariable( const char *line);
    test_4_function( const char *line);

    Because if either modify the line, the other is in for a surprise.

  4. #4
    Unregistered
    Guest
    I take it that 'line' is pointing to an array or something like that?
    it's important that 'line' is pointing to something before using it

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    25
    "line" is decalred as...

    char line[1000];

    i'm trying to read a text file line by line. the two functions that i call don't modify the line, though i am ready the tokens by splitting it...


    char *x = strtok(line, delim);

    and reading it token by token...

    x = strtok(NULL, delim);

    these shouldn't actually modify the line though........should they?!

    Bill

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    25
    i sort of know what the problem is but still need a little bit of help...

    char line[1000] is getting passed to the 1st function where it is being tokenised. When the 2nd function tries to use "line", it is getting the tokenised version. That is why the two functions work independently, but not together (I assume). Therefore, I need to pass the original version of "line" to both functions. How do I do this? Do I have to make "line" a constant (and if so, how)?

    I appreciate any help!

    Bill

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Then read the manual page for strtok much more carefully!!!

    For example:
    BUGS

    Never use this function. This function modifies its first argument. The identity of the delimiting character is lost. This function cannot be used on constant strings.

    Of course the 2nd function is going to have problems if the first has trampled all over the buffer with strtok

    The fix is to make a copy of the line, then strtok the copy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions are Still Not Understood.
    By errigour in forum C Programming
    Replies: 6
    Last Post: 04-09-2009, 02:54 PM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. Trying to do basic math functions. Why doesn't this work?
    By crazychile in forum C Programming
    Replies: 5
    Last Post: 10-25-2008, 05:14 PM
  4. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  5. Replies: 6
    Last Post: 05-06-2003, 03:08 PM