LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Can 't convert from a character to octal value in memory (https://www.linuxquestions.org/questions/programming-9/can-t-convert-from-a-character-to-octal-value-in-memory-74598/)

Linh 07-21-2003 12:57 PM

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))
    {

      if (single_char != '\n')
        {
          word_text[i] = single_char;

      /* convert from a character to an octal          */
1 --->          sprintf (three_oct_char, "%o", single_char);
2 --->          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 ();
  }


TheLinuxDuck 07-21-2003 02:56 PM

Linh.. can you please put [code] [/code] tags around your code when you post??? It's really difficult to read your code without them!

All you have to do is say
[code]
#!/usr/bin/perl
&nbsp;
print "blah blah blah\n";
[/code]

and it comes out as:
Code:

#!/usr/bin/perl

print "blah blah blah\n";

Much much easier to read, since the code tage preserve indentation.

Thanks!!!!!! (=

Linh 07-21-2003 03:11 PM

syntax error
 
Thank you TheLinuxDuck for the hint, I will use that from now on.

============================================
The line #2 below should be changed from a %o to %s
2 ---> printf ("three_oct_char = %o\n", three_oct_char);

change from a %o to %s would cause it to print correctly
2 ---> printf ("three_oct_char = %s\n", three_oct_char);

============================================


All times are GMT -5. The time now is 12:20 AM.