473,406 Members | 2,369 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,406 software developers and data experts.

problem with strings

A few days ago I asked from you how to join a string like "file"

A number that change values from 1 to 5 and another string like ".txt"

to have a result like "file1.txt","file2.txt" and so on

and you gave me the right answer.

But now I got another problem.

How can I save that values to an array?

This is the code you gave me:

void CreateName(char *dest_fname, const char *fname, const char *ext, int
counter,int *a,int length)

{

sprintf(dest_fname, "%s%d%s", fname, counter, ext);

Write_File(dest_fname,a,length);

}

int main()

{

int *A;

char *B[5]; //I tried this

char fname[] = "file";

char ext[] = ".txt";

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

int file_counter=0,k=0;

for(int i=20000; i<=100000; i+=20000)

{

A=Create_RundomNumbers_Array(i);

CreateName(out, fname, ext, ++file_counter,A,i);

B[k++]=out; //and this

}

But it doesn't work.

strcpy(B[k++],out); doesn't work.either.

what can I do?



Jan 3 '06 #1
10 1490
Aris wrote:
A few days ago I asked from you how to join a string like "file"
So why are you ignoring everything we told you?
A number that change values from 1 to 5 and another string like ".txt"

to have a result like "file1.txt","file2.txt" and so on

and you gave me the right answer.

But now I got another problem.

How can I save that values to an array?

This is the code you gave me:

That is not the code anybody gave you. You still have exactly the same
problem as before.

char* is NOT A STRING TYPE.

Learn the difference between call by value and call by reference.

Before you can write into something with strcpy you must first
access memory for it.
Ditch your attempts to use char* and use std::string.
Jan 3 '06 #2

Ron Natalie wrote:

[snip]
Before you can write into something with strcpy you must first
access memory for it.

[snip]

Before you can write into something with strcpy you must first
*allocate* memory for it.

Jan 4 '06 #3

"Marcelo Pinto" <mp******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

Ron Natalie wrote:

[snip]
Before you can write into something with strcpy you must first
access memory for it.

[snip]

Before you can write into something with strcpy you must first
*allocate* memory for it.


Actually that should be 'provide' memory. This memory
might be allocated, or static, or automatic.

-Mike
Jan 4 '06 #4

Ron Natalie wrote:
Ditch your attempts to use char* and use std::string.


I know this is widely echoed advise in this news group, but I'm not
sure I agree with it.

Personally I think new programmers getting into C++ should actually DO
figure out how char*'s work. Once they've mastered char*'s and start
"real projects", they should obviously use std::string or some
equivalent, but if you don't have a general understanding of new and
delete (or delete[] ), I don't think you should simply skip the basics
and move on to std::string...

Cheers,
Andre

Jan 4 '06 #5
in*****@gmail.com wrote:
Ron Natalie wrote:
Ditch your attempts to use char* and use std::string.


I know this is widely echoed advise in this news group, but I'm not
sure I agree with it.

Personally I think new programmers getting into C++ should actually DO
figure out how char*'s work. Once they've mastered char*'s and start
"real projects", they should obviously use std::string or some
equivalent, but if you don't have a general understanding of new and
delete (or delete[] ), I don't think you should simply skip the basics
and move on to std::string...


std::string is much easier to use correctly than char*.

Surely the basics are std::strings, and the complicated bits are arrays,
resource management, and pointers?

Your comments seem backwards somehow.

Only when somebody is at ease with the basics, should they progress to
the more advanced topics of pointers, arrays and resource management.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 4 '06 #6

Ben Pope wrote:
in*****@gmail.com wrote:
Ron Natalie wrote:
Ditch your attempts to use char* and use std::string.
I know this is widely echoed advise in this news group, but I'm not
sure I agree with it.

Personally I think new programmers getting into C++ should actually DO
figure out how char*'s work. Once they've mastered char*'s and start
"real projects", they should obviously use std::string or some
equivalent, but if you don't have a general understanding of new and
delete (or delete[] ), I don't think you should simply skip the basics
and move on to std::string...


std::string is much easier to use correctly than char*.


Yes.
Surely the basics are std::strings, and the complicated bits are arrays,
resource management, and pointers?
Sort of.. (see below)
Your comments seem backwards somehow.

Only when somebody is at ease with the basics, should they progress to
the more advanced topics of pointers, arrays and resource management.


The "usage" of std::strings is easier than char* of course. But
std::string hides implementation details and simple language
mechanism's (buffer allocation and deletion etc) that the developer
should be familiar with in order for them to apply such mechanism's to
their own classes and algorithms.

The concept of arrays may be more advanced, but a character array is
IMHO the simplest, most concrete way to explain those features and
visualize them. It's much easier to explain an array of chars than an
array of X objects.

One should be aware of what allocates memory where and who deletes it
before diving into library functions that do it for you.

This may seem backwards - heck, it may even actually be backwards - but
I believe it's a better way to learn.

Also, the recommendation of "Heck, just use std::string" usually comes
at a point where somebody is fundamentally misunderstanding what a
char* does. So I definitely think it's better to explain that first,
before just pointing towards a library feature.

Otherwise one could simply say "Don't bother with char*'s, just use
Visual Basic!" ;)

Cheers,
Andre

Jan 5 '06 #7

in*****@gmail.com wrote:
Ron Natalie wrote:
Ditch your attempts to use char* and use std::string.
I know this is widely echoed advise in this news group, but I'm not
sure I agree with it.

Personally I think new programmers getting into C++ should actually DO
figure out how char*'s work.


No, no, no, no, NO !!!! (at least, not until well after they learn
about std::string)
Once they've mastered char*'s and start
"real projects", they should obviously use std::string or some
equivalent
Ever heard of 'primacy' in teaching?
but if you don't have a general understanding of new and
delete (or delete[] ), I don't think you should simply skip the basics
and move on to std::string...


Given that std::string is basic and representing a string with an array
of chars is advanced, this statement does not make sense.

char array representation of strings is a hack invented for the sole
reason that the C language has no better option. C++ is not C.

The idea that one should not be allowed to learn how to _use_ a
standard library facility until one has learnt how to implement it in C
is ridiculous.

std::string is a fundamental that should be taught at the same level as
basic control structure (for, if, while), std::vector and std::cout.

dynamic memory management is an advanced topic that should be taught at
the same level as inheritance, polymorphism, templates and the char
array hack for representing strings.

Or are you suggesting that nobody should be shown

#include <iostream>

int main()
{
std::cout << "Hello, world!\n";
return 0;
}

until they know how to implement a stream buffer?

Gavin Deane

Jan 5 '06 #8
in*****@gmail.com wrote:
Ben Pope wrote:
std::string is much easier to use correctly than char*.
Yes.
Surely the basics are std::strings, and the complicated bits are arrays,
resource management, and pointers?


Sort of.. (see below)
Your comments seem backwards somehow.

Only when somebody is at ease with the basics, should they progress to
the more advanced topics of pointers, arrays and resource management.


The "usage" of std::strings is easier than char* of course. But
std::string hides implementation details and simple language
mechanism's (buffer allocation and deletion etc) that the developer
should be familiar with in order for them to apply such mechanism's to
their own classes and algorithms.


Yes, but they should only need to apply those things to their own
classes and algorithms *after* hello world, and whatever else comes
after that.
The concept of arrays may be more advanced, but a character array is
IMHO the simplest, most concrete way to explain those features and
visualize them. It's much easier to explain an array of chars than an
array of X objects.
std::vector is much easier to use than a dynamically allocated array.
One should be aware of what allocates memory where and who deletes it
before diving into library functions that do it for you.
Why? Keeping that horrible resource management stuff hidden (in
constructors and destructors - RAII) is what we all strive to do in
order to create code that is not leaky and exception safe, surely?

Why should a beginner learn how to misuse dangerous tools from the very
beginning (when they don't need to), only to be set straight later on?
This may seem backwards - heck, it may even actually be backwards - but
I believe it's a better way to learn.
I guess that you think that you should learn C before C++?
Also, the recommendation of "Heck, just use std::string" usually comes
at a point where somebody is fundamentally misunderstanding what a
char* does. So I definitely think it's better to explain that first,
before just pointing towards a library feature.
I disagree. Get inexperienced people to use the features that have been
painstakingly designed and refined over time and slapped in the standard
library, instead of reinventing the wheel.

The features have to be in standard-conforming implementation. They are
there to hide all the messy details of resource allocation, provide
value-semantics and generally make your life easier.

The use of raw pointers is really only necessary in very low-level code,
and at that point you really do need to understand in detail what every
line of code *really* does... such as whether it can throw an exception,
in order to avoid resource leaks.

Pointers and the free store are often unnecessary in simple programs,
and even then, the use of references rather than pointers mitigates the
chance of many errors.

The moment you introduce pointers you have to introduce not just
reference semantics, but exception safety and resource management
techniques such as RAII. IMO this is too much to grasp in one go,
especially for a beginner.
Otherwise one could simply say "Don't bother with char*'s, just use
Visual Basic!" ;)
I'll refer to your previous comment:
The "usage" of std::strings is easier than char* of course. But
std::string hides implementation details and simple language
mechanism's (buffer allocation and deletion etc) that the developer
should be familiar with in order for them to apply such mechanism's to
their own classes and algorithms.


So I guess everybody should be taught machine code initially. C++ is
way too abstract!

Abstractions are what we try to model in our software, in order to not
have to think about the details. The more details that can disappear
behind an interface, the better.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 5 '06 #9
On 2006-01-04, in*****@gmail.com <in*****@gmail.com> wrote:

Ron Natalie wrote:
Ditch your attempts to use char* and use std::string.


I know this is widely echoed advise in this news group, but I'm
not sure I agree with it.

Personally I think new programmers getting into C++ should
actually DO figure out how char*'s work. Once they've mastered
char*'s and start "real projects", they should obviously use
std::string or some equivalent, but if you don't have a general
understanding of new and delete (or delete[] ), I don't think
you should simply skip the basics and move on to std::string...


Learn std::string first, along with the other standard
containers. Then learn about iterators. After that, pointers will
practically be learned; there just another iterator, after all.

This is the approach taken in _Accelerated C++_. Before you're
done you write your own String and Vector classes. It's a
perfectly legitimate approach.

--
Neil Cerutti
Jan 5 '06 #10
On 2006-01-05, in*****@gmail.com <in*****@gmail.com> wrote:
Ben Pope wrote:
std::string is much easier to use correctly than char*.
Yes.
Surely the basics are std::strings, and the complicated bits
are arrays, resource management, and pointers?


Sort of.. (see below)
Only when somebody is at ease with the basics, should they progress to
the more advanced topics of pointers, arrays and resource management.


The "usage" of std::strings is easier than char* of course. But
std::string hides implementation details and simple language
mechanism's (buffer allocation and deletion etc) that the
developer should be familiar with in order for them to apply
such mechanism's to their own classes and algorithms.


That's why learning std::string before char* is so valuable. It
exposes new C++ programmers to a major C++ abstraction mechanism.
Learning it the other way around may be a disservice. The ability
to program in Brainf**k is very important in C++, but you don't
have to learn it first. ;-)
The concept of arrays may be more advanced, but a character
array is IMHO the simplest, most concrete way to explain those
features and visualize them. It's much easier to explain an
array of chars than an array of X objects.


I agree that eventually you need to understand these concepts.

--
Neil Cerutti
Jan 5 '06 #11

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

Similar topics

10
by: jcc | last post by:
Hi guys, I'm a newbie to C#. My Visual Studio 2005 failed to compile the following code with error as 'HelloWorld.A' does not implement interface member...
0
by: Pablo Jose Almeida da Guia | last post by:
Well.. ...I am using some resource files to make the internationalization of a ASP.NET application. I created a project and three resource files. ------- resource files ------- /strings.txt...
2
by: Fredrik Rodin | last post by:
All, I'm having problems with my resource manager in ASP.NET 2.0 after conversion from ASP.NET 1.1. Here is a background: In ASP.NET 1.1 All my user controls and aspx pages inherit from...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
14
by: Dennis Benzinger | last post by:
Hi! The following program in an UTF-8 encoded file: # -*- coding: UTF-8 -*- FIELDS = ("Fächer", ) FROZEN_FIELDS = frozenset(FIELDS) FIELDS_SET = set(FIELDS)
22
by: sam_cit | last post by:
Hi Everyone, I have the following structure in my program struct sample { char *string; int string_len; };
11
by: Bob Rock | last post by:
Hello, I have an array of strings and need to find the matching one with the fastest possible code. I decided to order the array and then write a binary search algo. What I came up with is the...
0
by: nass | last post by:
hello everyone and happy new year. i am not sure how to tackle this problem or where is originates from so i am writing here in hope that if you can not help you can at least point me in a...
35
by: Chris | last post by:
Hi, I tried to create a class which must change the propety 'visible' of a <linktag in the masterpage into 'false' when the user is logged. But i get the error: "Object reference not set to an...
4
by: Alexey Moskvin | last post by:
Hi! I have a set of strings (all letters are capitalized) at utf-8, russian language. I need to lower it, but my_string.lower(). Doesn't work. See sample script: # -*- coding: utf-8 -*- s1 =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.