LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2003, 12:57 PM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
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 ();
  }

Last edited by Linh; 07-21-2003 at 03:08 PM.
 
Old 07-21-2003, 02:56 PM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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!!!!!! (=
 
Old 07-21-2003, 03:11 PM   #3
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
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);

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

Last edited by Linh; 07-21-2003 at 03:13 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python: converting a 3 character octal string into and ASCII char llama_meme Programming 1 07-06-2010 02:00 PM
C, read 8 bit octal numbers, convert to 24 bit binary bamalabs Programming 3 01-20-2004 09:59 AM
Octal format kawaii Linux - Newbie 2 11-02-2003 08:27 AM
convert a character in a box to keycode number Linh Programming 3 10-14-2003 04:14 PM
Using sed to convert a string to a character? whansard Linux - General 2 01-10-2003 05:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration