473,394 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

for me its difficult

Hi all!
I want to make a function which will create file names as :
I will load with an integer
from 1 till 5 and receive a responce like this
"file1.txt","file2.txt"....."file5.txt"
But instead I get crazy things like theese:
file1.txt
file1.txt2
file1.txt23
file1.txt233@
can you help me?

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char * Create_File_Name(int counter_files)
{
char *ch1="file";
char *ch2=".txt";
char string[2];
itoa(counter_files,string,10);
char *ch3=strcat(ch1,string);
char *fname=strcat(ch3,".txt");
return fname;
}
int main()
{

char* fname;
for(int i=1; i<5; i++)
{
fname=Create_File_Name(i);
cout<<fname<<endl;
}
int p;
cin>>p;
return 0;
}


Dec 27 '05 #1
6 1335
> "file1.txt","file2.txt"....."file5.txt"

void CreateName(char *dest_fname, const char *fname, const char *ext, int
counter)
{
sprintf(dest_fname, "%s%d%s", fname, counter, ext);
}

int main()
{
char fname[] = "file";
char ext[] = ".txt";

char out[64] = {'\0'};

CreateName(out, fname, ext, 1);
cout << out; // here you get "file1.txt"

return 1;
}
Dec 27 '05 #2
Aris wrote:
Hi all!
I want to make a function which will create file names as :
I will load with an integer
from 1 till 5 and receive a responce like this
"file1.txt","file2.txt"....."file5.txt"
But instead I get crazy things like theese:
file1.txt
file1.txt2
file1.txt23
file1.txt233@
can you help me?

#include <iostream.h>
This is an outdated non-standard header.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char * Create_File_Name(int counter_files)
{
char *ch1="file";
char *ch2=".txt";
Those should be const char* or const char[].
char string[2];
This means you only have space for one single digit, so you have to ensure
that counter_files is not larger than 9.
itoa(counter_files,string,10);
char *ch3=strcat(ch1,string);
This invokes undefined behavior, because you try to attach something to a
string literal. For one, a string literal is constant and second, you write
beyond its end.
char *fname=strcat(ch3,".txt");
return fname;
}
int main()
{

char* fname;
for(int i=1; i<5; i++)
{
fname=Create_File_Name(i);
cout<<fname<<endl;
}
int p;
cin>>p;
return 0;
}


You should use std::string and std::stringstream. Try this:

#include <iostream>
#include <sstream>
#include <string>

std::string Create_File_Name(int counter_files)
{
std::stringstream stream;
stream << "file" << counter_files << ".txt";
return stream.str();
}

int main()
{
std::string fname;
for (int i = 1; i < 5; ++i)
{
fname = Create_File_Name(i);
std::cout << fname << '\n';
}
}

Dec 27 '05 #3
Aris wrote:
char *ch1="file";
char *ch2=".txt";
char string[2];


char is a single character. char* is a pointer
to a single character. The C++ string type is called
string. Perhaps you should research that (stringstream
might also be useful to you).
Dec 27 '05 #4
thank you,
to all of you!
The problem solved
I come out of a bad situation
and I learned some things more
Merry Christmas to all of you!
Dec 27 '05 #5
Aris wrote:
Hi all!
I want to make a function which will create file names as :
I will load with an integer
from 1 till 5 and receive a responce like this
"file1.txt","file2.txt"....."file5.txt"
But instead I get crazy things like theese:
file1.txt
file1.txt2
file1.txt23
file1.txt233@
can you help me?

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char * Create_File_Name(int counter_files)
{
char *ch1="file";
char *ch2=".txt";
char string[2];
itoa(counter_files,string,10);
char *ch3=strcat(ch1,string);
char *fname=strcat(ch3,".txt");
return fname;
}
int main()
{

char* fname;
for(int i=1; i<5; i++)
{
fname=Create_File_Name(i);
cout<<fname<<endl;
}
int p;
cin>>p;
return 0;
}


Don't use character arrays or itoa. Prefer std::string and the
conversion method discussed here:

http://www.parashift.com/c++-faq-lit....html#faq-39.1

Cheers! --M

Dec 27 '05 #6

Aris wrote:
Hi all!
I want to make a function which will create file names as :
I will load with an integer
from 1 till 5 and receive a responce like this
"file1.txt","file2.txt"....."file5.txt"


DO NOT USE C++; write a script

Dec 27 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Spammay Blockay | last post by:
I've been tasked with doing technical interviews at my company, and I have generally ask a range of OO, Java, and "good programming technique" concepts. However, one of my favorite exercises I...
8
by: Marc | last post by:
Hi all, I was thinking about developing a drag and drop application and was curious how difficult it is to do. Basically I want to have a set of objects that I move around the screen and drop...
0
by: Gardner Pomper | last post by:
Hi, I am pretty new to python, so be gentle :) I have a python script that spawns a number of threads (configurable) on a 12 processor AIX RISC-6000 machine. It works fine, so long as I am...
12
by: Mike Dee | last post by:
A very very basic UTF-8 question that's driving me nuts: If I have this in the beginning of my Python script in Linux: #!/usr/bin/env python # -*- coding: UTF-8 -*- should I - or should I...
0
by: manunam | last post by:
hi all, I am creating an ISAPI filter in VC++. this filter has to check the http client requests, and do some checks. First of all, when a client requests a file, the filter checks if the...
0
by: ALI | last post by:
hi i need some best answer's for this question for interview as an oracle (DBA). Q : what is the most difficult situation u faced and how did u solved it ???? i really appriciate if some...
7
by: M Wells | last post by:
Hi All, I have what seems to me to be a difficult query request for a database I've inherited. I have a table that has a varchar(2000) column that is used to store system and user messages...
7
by: Manu | last post by:
Hi! All I was wondering what is the most difficult part of windows programming Is it Network Programming Etc..
2
by: Eniac | last post by:
*argh* ... *pull hairs* I've recently started developing from ASP to ASP.NET The switch was fairly smooth since i had done some VB.net before ... then came...FORMS! :) I find it astounding...
8
by: Senderos | last post by:
Hello, i try to break this code, since many days (and nights), without any success : <script type="text/javascript"> function p(a,i,c) { var x=a.charCodeAt(i)+c;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.