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

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    Can 't convert from a character to octal value in memory


    Can 't convert from a string to an octal in memory using either
    of the two
    sprintf (three_oct_char, "%o", single_char);
    sscanf (single_char, "%o", three_oct_char);
    ========================================
    three_oct_char = 27777776450 ---> incorrect value
    single_char = / octal_long = 57
    three_oct_char = 27777776450 ---> incorrect value
    single_char = p octal_long = 160
    three_oct_char = 27777776450 ---> incorrect value
    single_char = r octal_long = 162
    three_oct_char = 27777776450 ---> incorrect value
    single_char = o octal_long = 157
    three_oct_char = 27777776450 --- > incorrect value
    single_char = c octal_long = 143

    ========================================
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *progam_name;
    char *file_name;
    
    read_the_file ()
     {
        FILE *file_pointer, *file_pointer_2, *fopen();
        int i = 0;
        int result = 99;
        char three_oct_char[] = {'\0', '\0', '\0', '\0'};
        char word_text [100];
        char word_octal[100];
        char single_char;
        unsigned long octal_long;
    
        file_pointer = fopen("/root/text_file.txt", "r");
        if((file_pointer = fopen("/root/text_file.txt","r")) == NULL)
         {
           perror("Couldn't open file. File does not exist.");
           return 1;
         }
    
        file_pointer_2 = fopen("/root/octal_file.txt", "w");
        if((file_pointer_2 = fopen("/root/octal_file.txt","w")) == NULL)
         {
           perror("Couldn't open file. File does not exist.");
           return 1;
         }
    
    
        single_char = getc(file_pointer);  /* read first character */
        while(!feof(file_pointer))
         {
           /* convert from a character to an octal          */
    
           if (single_char != '\n')
             {
               word_text[i] = single_char;
    --->           sprintf (three_oct_char, "%o", single_char);
               printf ("three_oct_char = %o\n", three_oct_char);
               i++;
             }
           else
             {
               word_text[i] = '\0';
               printf ("word_text[] = %s\n", word_text);
               sprintf(word_octal, "%s", word_text);
               printf ("word_octal[] = %o\n", word_octal);
             }
    
           printf ("single_char = %c  octal_long = %o\n", single_char, single_char);
    
           single_char = getc(file_pointer);
         }
     }
    
    main()
    
      {
        read_the_file ();
      }
    Last edited by linh; July 21st, 2003 at 04:16 PM.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    Doesn't appear to be anything wrong with the sprintf() statement. However, you have a bug in the next line, which displays the conversion:
    Code:
    printf ("three_oct_char = %o\n", three_oct_char); 
    should be
    printf ("three_oct_char = %s\n", three_oct_char);
    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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    Smile Thank you for your help


    Thank you Scorpions4ever.

IMN logo majestic logo threadwatch logo seochat tools logo