LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 03-21-2003, 03:31 AM   #1
lsropia
Member
 
Registered: Dec 2002
Distribution: RedHat
Posts: 30

Rep: Reputation: 15
ios, iostream, fstream & iomanip etc.


Hello,

I used to write c++ programmes in MS Windows, and rather new to this exciting linux programming. The code snippet like this would run OK in the MS Windows, but what should I do to get it running in Linux. The problem is basically using iomanip.

==================================================
#include <dirent.h>
#include <iostream.h>
#include <iomanip.h>

main()
{
struct dirent **namelist;
int n;
int nn=0;
n = scandir (".", &namelist, 0, 0 /* alphasort */ );
cout << n << endl; // printf ("%d\n", n);
if (n < 0)
cerr << "ScanDir"; // perror ("scandir");
else {
while (n--) {
if ( !strcmp(namelist[n]->d_name, ".")) ;
else if ( !strcmp(namelist[n]->d_name, "..")) ;
else {
cout << namelist[n]->d_name << "\t\t\t" << "smpl" << setw(4) << setfill(0) << nn++ << endl; // printf ("%s\n", namelist[n]->d_name);
//free (namelist[n]);
}
}
//free (namelist);
}
}
================================================

The program is basically what is given in help pages in linux, modified a bit take use of setw() & setfill().

Also, I donot find the functions itoa() listed in linux, any other equivalent function?

Please help.


Alex
 
Old 03-21-2003, 04:06 AM   #2
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Re: ios, iostream, fstream & iomanip etc.

Quote:
Originally posted by lsropia

The problem is basically using iomanip.

==================================================
#include <dirent.h>
#include <iostream.h>
#include <iomanip.h>

...

Alex
iomanip is needed for setprecision in this example. It works just fine on Linux g++. I would question dirent.h

Code:
#include<iostream>
#include<iomanip>

using std::cout;
using std::setprecision;
using std::streamsize;
using std::endl;

int main() {
  double num = 444.333;
  streamsize prec = cout.precision();
  cout << setprecision(3) << num << endl;
  cout << setprecision(prec) << num << endl;
  return 0;
}
 
Old 03-21-2003, 05:16 AM   #3
lsropia
Member
 
Registered: Dec 2002
Distribution: RedHat
Posts: 30

Original Poster
Rep: Reputation: 15
Hello,

The code segment suggested worked alright, however only slight modification is required in that too, so as to use setw(), setfill() functions in the iomanip.

=====================================================
#include<iostream>
#include<iomanip>

using std::cout;
using std::setprecision;
using std::setw;
using std::setfill;
using std::streamsize;
using std::endl;

int main() {
double num = 444.333;
streamsize prec = cout.precision();
cout << setprecision(8) << setw(12) << setfill('0') << num << endl;
cout << setprecision(prec) << num << endl;
return 0;
}
=============================================

Thanks for a very quick analysis and equally reply.

Alex
 
Old 03-21-2003, 02:50 PM   #4
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
I don't think that g++ implements things like:
std::ios::hex
or
std::hex

It's not perfectly standard compliant, but it is better than vc++6.
 
  


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
linux CISCO IOS emulator? tekhead2 Linux - Software 2 04-22-2008 04:49 AM
Mandrake 9.1 & fstream.. The Many Programming 3 02-13-2005 11:02 AM
c++ fstream:: question javakid Programming 2 01-21-2005 02:03 PM
Help using freebsd 5.0 + squid + wccp + cisco ios nazzymac *BSD 1 07-27-2004 04:11 PM
Ios pk21 Linux - Security 3 08-09-2003 12:13 AM


All times are GMT -5. The time now is 04:42 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