LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 09-12-2003, 11:58 AM   #1
cash_05
LQ Newbie
 
Registered: Sep 2003
Posts: 12

Rep: Reputation: 0
Question How can i create filename using interger with gnu C++?


Hi,

I'm thinking of writing a c++ program that will automatci generate some files. My desire filename is from file001 to file999. I will like to use a loop to do that. For example,

..................
const string filepre = "file";
string filename;
int i,;
..................
..................
for (i=1; i<1000; i++) {
filename = filepre + i;
ofile.open(filename);
ofile << i;
ofile.close(filename);
}

But, when i try to compile, the compilre always return some type conversion error.

Can anyone help me to resolve me, guiding me the proper way to do this.

Thanks in advance.
 
Old 09-12-2003, 12:01 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
try using sprintf()
 
Old 09-12-2003, 12:37 PM   #3
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
Also, unless this is an exercise in learning C/C++, this can easily be done in a simple shell script:

Code:
for i in `seq -f%03.f 1 999`; do echo i > file$i; done
 
Old 09-12-2003, 12:38 PM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
by acid_kewpie
try using sprintf()

i think he wants to use c++ strings

best i can think of is
Code:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

int main()
{
    ostringstream oss;

    for(int i=0;i<1000;i++) {
        oss.str("");
        oss << "file" << setfill('0') << setw(3) << i;
        //here i print the filename, you should open it.
        cout << oss.str() << endl;
    }

    return 0;
}
~

Last edited by kev82; 09-12-2003 at 12:40 PM.
 
Old 09-12-2003, 12:43 PM   #5
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
The C++ way would be to use strstream.

Code:
#include <strstream>
#include <string>


main()
{
   std::string filepre = "file";
   std::string filename;

   int i;
   for ( i = 1; i < 10; i++ )
   {
      strstream s;
      s << i;
      filename = filepre + s.str();
      cout << filename;
   }
}
 
Old 09-12-2003, 01:10 PM   #6
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
I suppose this should do it


#include<stdio.h>
#include<string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
main()
{
int i=0;
char *string="file",my_file[100];
for(i=0;i<10;i++)
{
sprintf(my_file,"%s%d",string,i);
open(my_file,O_CREAT);
}
 
Old 09-12-2003, 11:22 PM   #7
cash_05
LQ Newbie
 
Registered: Sep 2003
Posts: 12

Original Poster
Rep: Reputation: 0
Thumbs up

Thank you everyone. Really learn somethign good from here.

Actually, i'm trying to write a program/script that can automatic generate many files in sequence. I'm using cbq to control bandwidth for every users in office. I need to define a bandwidht sharper for every ip, those config files will looks like this:

file-0001.ext
----------------
device=eth0,10mbit,1mbit
rate=560Kbit
weight=56Kbit
prio=5
rule=192.168.0.1/32

file-0002.ext
---------------
device=eth0,10mbit,1mbit
rate=560Kbit
weight=56Kbit
prio=5
rule=192.168.0.2/32

....................

file-0254.ext
---------------
device=eth0,10mbit,1mbit
rate=560Kbit
weight=56Kbit
prio=5
rule=192.168.0.254/32


I'm a newbie in linux world, know nothing about sed, only know abit of c++ where i learn from college. I trying to write a program to generate those files instead manually key in.

Can this be done using a shell script instead of program? If so, anyone willing to teach me?

Thanks again.
 
  


Reply



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
change uploaded files from filename.avi to filename.avi.html like www.rapidshare.de latheesan Linux - Newbie 3 06-16-2005 04:33 AM
create GNU make Makefile from visualstudio project andreyk Programming 0 02-02-2005 10:25 AM
Software to create booklets for GNU/Linux? cfw Linux - General 2 10-28-2004 04:00 AM
makes pointer from interger without a cast y0shi Linux - General 4 10-21-2004 08:17 PM
filename- and filename~ files? slinky2004 Linux - Newbie 5 10-17-2004 10:32 PM

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

All times are GMT -5. The time now is 10:31 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
Open Source Consulting | Domain Registration