字符串的问题 [英] problem with strings

查看:52
本文介绍了字符串的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前几天我问过你如何加入像文件这样的字符串


一个数值从1到5改变另一个字符串如.txt ;


有一个像file1.txt,file2.txt这样的结果。


你给了我正确答案。


但是现在我又遇到了另一个问题。


如何将这些值保存到数组?


这是你给我的代码:


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]; //我试过了这个


char fname [] =" file";


char ext [] =" .txt" ;;


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


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);


乙[K ++] = OUT; //这个


}


但是它不起作用。


strcpy (B [K ++],OUT);什么都不工作。


我该怎么办?


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?



推荐答案

Aris写道:
几天前我问你如何加入像文件这样的字符串

你为什么忽略我们告诉你的一切?

一个数字从1改为5,另一个字符串改为.txt

,以得到像file1.txt这样的结果。 ," FILE2.TXT"

你给了我正确答案。

但是现在我又遇到了另一个问题。

如何将这些值保存到数组?

这是你给我的代码:
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:



这不是任何人给你的代码。你仍然和以前一样有完全相同的问题。


char *不是一个字符串类型。


了解按值调用和按引用调用之间的区别。


在您使用strcpy编写内容之前,必须首先使用
访问内存。

放弃尝试使用char *并使用std :: string。


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.




Ron Natalie写道:


[snip]

Ron Natalie wrote:

[snip]
在您使用strcpy写入内容之前,您必须首先为其访问内存。
Before you can write into something with strcpy you must first
access memory for it.



[snip]


在你用strcpy写东西之前,你必须首先给它/ b $ b *分配*内存。




" Marcelo Pinto" <熔点****** @ gmail.com>在消息中写道

news:11 ********************** @ g47g2000cwa.googlegr oups.com ...

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

Ron Natalie写道:

[snip]

Ron Natalie wrote:

[snip]
在你用strcpy写东西之前你必须首先为它访问内存。
Before you can write into something with strcpy you must first
access memory for it.


[snip]

在你用strcpy写东西之前,你必须先为它分配*内存。


[snip]

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




实际上应该是''提供''记忆。这个记忆

可能会分配,或静态或自动。


-Mike



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

-Mike


这篇关于字符串的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆