#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    function won ' t return character array word


    function won ' t return character array word

    =========================================
    root:~# ./test-small-network-sh-4

    in read_file_nic word = 0001010002020004110009440005050008220007280010480012330010100013090016440013520032200020550019040019 21
    1 in main word = <---- word array is empty

    =========================================
    Code:
    #include <stdio.h>         
    #include <stdlib.h> 
    #include <string.h>
    
    char read_file_nic(word)
      char word[256];
      {
        FILE *interface, *fopen();
    
        /* process_name_2 is now a file name */
        interface = fopen(process_name_2, "r");
        if (fopen(process_name_2, "r") == NULL)
          {
             perror("Couldn't open file");
             /* shut_down_services(); */
             exit(1);
          }
    
        fscanf(interface, "%s", word);
        printf("in read_file_nic word = %s\n", word);
    
        fclose(interface);
        return(word[0]);
      }
    
    /********************************/
    
    main()
     {
       char word [256] = "";
    
       read_file_nic(process_name_2, word);
       printf ("1 in main word = %s\n", word);
    
     }
    Last edited by linh; July 31st, 2003 at 11:37 AM.
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char read_file_nic(char *filename) {
    	FILE *fp = NULL;
    	char word[256];
    
    	if (NULL == (fp = fopen(filename, "r"))) {
    		fprintf(stderr, "Could not open %s for reading.\n", filename);
    		exit (1);
    	}
    
    	fgets(word, 255, fp);
    	printf("In read_file_nic word = %s\n", word);
    
    	return (word[0]);
    }
    
    int main(void) {
    	char filename[] = "test.txt";
    	char letter = '\0';
    
    	letter = read_file_nic(filename);
    	printf("%c was returned from read_file_nic.\n",letter);
    
    	return (0);
    }
    Try something like this.
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247
    Notice that in main(), you're passing 2 params to read_file_nic(), whereas read_file_nic() only takes one param.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12
    1) The char word [256] = ""; was declared in main, and so when I passed it into a parameter in the function read_file_nic(word)
    it should contain something. So when I printed in main nothing prints out. What did I do wrong ?

    2) If i used the code return(word); in the function read_file_nic(word), I get an error message at compiled time
    warning: return makes integer from pointer without a cast

    3) What does the the pictures of the two dogs mean ? I guess humor.


    ==============================
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    char read_file_nic(word)
    char word[256];
    {
    FILE *interface, *fopen();

    /* process_name_2 is now a file name */
    interface = fopen(process_name_2, "r");
    if (fopen(process_name_2, "r") == NULL)
    {
    perror("Couldn't open file");
    /* shut_down_services(); */
    exit(1);
    }

    fscanf(interface, "%s", word);
    printf("in read_file_nic word = %s\n", word);

    fclose(interface);
    return(word);
    }

    /********************************/

    main()
    {
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    char read_file_nic(word)
    char word[256];
    {
    FILE *interface, *fopen();

    /* process_name_2 is now a file name */
    interface = fopen(process_name_2, "r");
    if (fopen(process_name_2, "r") == NULL)
    {
    perror("Couldn't open file");
    /* shut_down_services(); */
    exit(1);
    }

    fscanf(interface, "%s", word);
    printf("in read_file_nic word = %s\n", word);

    fclose(interface);
    return(word[0]);
    }

    /********************************/

    main()
    {
    char word [256] = "";

    read_file_nic(process_name_2, word);
    printf ("1 in main word = %s\n", word);


    read_file_nic(word);
    printf ("1 in main word = %s\n", word);
    ==================================
  8. #5
  9. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    No, the two dogs are just my two dogs. I love my aussies. :D

    There is a lot missing from your code, tis the reason why I made the example that I did. If you notice, you declared the function as a char, not char *, which is why I returned only the first letter of the word (i.e. return(word[0])). If you want to return the entire contents of the word then you should make the function char *, or modify the word argument itself. Also, as Scorp mentioned, you have but one argument in the function declaration, but you are passing two, and process_name_2 is not declared or set anywhere in your code.

    here is some more sample code, in this case I take two arguments to read_file_nic and alter the second one to be the value.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void read_file_nic(char *filename, char *word) {
    	FILE *fp = NULL;
    
    	printf("Original contents of word = '%s'\n",word);
    	
    	if (NULL == (fp = fopen(filename, "r"))) {
    		fprintf(stderr, "Could not open %s for reading.\n", filename);
    		exit (1);
    	}
    
    	fgets(word, 255, fp);
    	fclose(fp);
    	printf("New value of word = '%s'\n", word);
    }
    
    int main(void) {
    	char filename[] = "test.txt";
    	char *word = NULL;
    
    	if (NULL == (word = (char *)malloc(257))) {
    		fprintf(stderr, "Could not allocate memory for word.\n");
    		return (2);
    	}
    	
    	bzero(word, 256 * sizeof(char));
    
    	strcpy(word, "blah");
    
    	read_file_nic(filename, word);
    	printf("word is now %s.\n",word);
    
    	if (NULL != word) {
    		free(word);
    	}
    	return (0);
    }
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247
    All you had to do was add an extra second parameter to read_file_nic and it would have worked (at least for gcc 2.95). Here's your original code with my changes in bold.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char read_file_nic(process_name_2,  word)
         char process_name_2[256];
         char word[256];
    {
      FILE *interface, *fopen();
    
      /* process_name_2 is now a file name */
      interface = fopen(process_name_2, "r");
      if (fopen(process_name_2, "r") == NULL)
        {
          perror("Couldn't open file");
          /* shut_down_services(); */
          exit(1);
        }
    
      fscanf(interface, "%s", word);
      printf("in read_file_nic word = %s\n", word);
    
      fclose(interface);
      return(word[0]);
    }
    
    /********************************/
    
    main()
    {
      char word [256] = "";
    
      read_file_nic(process_name_2, word);
      printf ("1 in main word = %s\n", word);
    
    }
    See, I didn't change too much :). BTW, I suspect you've got read_file_nic in a different file than main() and no prototype for read_file_nic present. Otherwise, the compiler would have caught the error for you :).
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    Thank you


    Thank you Onslaught and Scorpions4ever

IMN logo majestic logo threadwatch logo seochat tools logo