LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Read in an Octal number from a text file using C++ (https://www.linuxquestions.org/questions/programming-9/read-in-an-octal-number-from-a-text-file-using-c-256476/)

pjordan 11-18-2004 11:55 AM

Read in an Octal number from a text file using C++
 
I'm new to LinuxQuestions.org so please forgive me if I use an inproper protocol.

My question is as follows: I am reading in a text file that contains a series of numbers like the following:

1
14
7
23
etc....

My program is working with Octal numbers for all counting purposes so in main I am using the following statement:

cout << setbase(8);

This allows the rest of my program to work great. However, in the list of numbers shown above, the 14 will be read in as 16. In other words, C++ will convert the integer to an octal. The problem is, 14 is already an octal. How do I keep it as and octal?

Option: Read it from the file as a string and convert the string to octal. How?
Option: Read it from the file directly in an octal integer. How? int means integer (base 10). Is there an octal data-type in C++?

In short, I want my text file 14 to be read in as a 14 base 8 and not get converted.

Any help will be greatly appreciated.

pjordan

ToniT 11-18-2004 12:16 PM

cout modifies the outputting, you want it to behave differenty for the input.
eg. if you are reading from stdin, modify your cin.

Sample code:
Code:

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
  cin >> oct;
  int a;
  cin >> a;
  printf("got %d\n",a);
  cout << "Which is also: " << a << endl;
  cout << "As octal.." << oct << a << endl;
}


pjordan 11-18-2004 03:03 PM

That is exactly what I was looking for. It was so sinple. Thank you. Thank you.


All times are GMT -5. The time now is 08:05 AM.