LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-18-2004, 12:55 PM   #1
pjordan
LQ Newbie
 
Registered: Nov 2004
Posts: 2

Rep: Reputation: 0
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
 
Old 11-18-2004, 01:16 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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;
}
 
Old 11-18-2004, 04:03 PM   #3
pjordan
LQ Newbie
 
Registered: Nov 2004
Posts: 2

Original Poster
Rep: Reputation: 0
That is exactly what I was looking for. It was so sinple. Thank you. Thank you.
 
  


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
bash read a given line number from text bendeco13 Programming 7 08-31-2012 04:49 PM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 10:33 PM
Have problem converting a decimal number to octal Linh Programming 4 05-20-2004 04:21 PM
How do I add two octal number ? Linh Programming 3 05-20-2004 04:08 PM
Script to read a simple text file ravykanth Linux - Newbie 9 10-22-2003 05:15 AM


All times are GMT -5. The time now is 04:49 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration