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 ();
}