0a附加w / strcpy [英] 0a appending w/strcpy

查看:122
本文介绍了0a附加w / strcpy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链表,每个节点都有几个数字,一个char $ / $
和一个const char数组。我使用strcpy取一个char数组

参数传递给我的insert函数将字符串复制到

新节点。


对于其中一些条目0a(换行?)正在追加。

我已经浏览了常见问题并用Google搜索但是找不到多少

info适用。


代码跟在这篇文章后面(有点我会将它分成多个

文件,用于更大的项目)。 br />

问候,

新手格兰特


< ------------- ------------------


#include< stdio.h>

#include< stdlib.h>

struct motNode {

struct motNode * next;

unsigned int opCode;

char type ;

unsigned int numArgs;

char opName [];

};

typedef struct motNode motList;

motList * mot_head,* mot_cur;

int mot_insert(unsigned int opCode,char type,unsigned int numArgs,
const char * opName){

mot_cur =(motList *)malloc(sizeof(motList));

mot_cur-> opCode = opCode;

mot_cur-> type = type;

mot_cur-> numArgs = numArgs;

strcpy(mot_cur-> opName,opName);

mot_cur-> next = mot_head;

mot_head = mot_cur;

}


mot_dump (){

while(mot_cur){

printf("%s\ n",mot_cur-> opName);

mot_cur = mot_cur-> next;

}

}


generate_mot(){

mot_head = NULL;

mot_insert(0,''r'',0," hlt");

mot_insert(1,''r'',3, add);

mot_insert(2,''r'',3," sub");

mot_insert(3,''r'', 3,mul);

mot_insert(4,''r'',3," div");

mot_insert(5,''r' ',3," mod");

mot_insert(6,''r'',2," move");

m ot_insert(7,''r'',3,"和);

mot_insert(8,''r'',3,"或);

mot_insert(9,''r'',3," xor");

mot_insert(10,''r'',2," com");

mot_insert(11,''r'',3," sll");

mot_insert(12,''r'',3," srl");

mot_insert(13,''r'',3," sra");

mot_insert(14,''r'',3," jr");

mot_insert(15,''r'',1," rdr");

mot_insert(16,''r'',1," prr" );

mot_insert(17,''r'',1," prh");


/ * I format * /

mot_insert(18,''我',2," li");

mot_insert(19,''我',3," addi");

mot_insert(20,''i'',3," subi");

mot_insert(21,''i'',3," muli");

mot_insert(22,''我',3," divi");

mot_insert(23,''我' ,3,modi);

mot_insert(24,''i'',3," lwb");

mot_insert(25,''i '',3," swb");


/ * J格式* /

mot_insert(26,''j'',2," ; lwa");

mot_insert(27,''我',2," swa");

mot_insert(28,''我',1 ,j,;

mot_insert(29,''我',1," jal");

mot_insert(30,''我'' ,1," jeq");

mot_insert(31,'''',1," jne");

mot_insert(32,''i '',1," jlt");

mot_insert(33,''我',1," jle");

mot_insert(34,' 'i'',1," jgt");

mot_insert(35,''我',1," jge");

}


main(int argv,char * argc []){

FILE * infle;

int

eofl,

fcount = 2;


if(argv< 3){

fprintf(stderr,参数太少了);

fprintf(stderr," \ tusage:\ tp2< flag> ;<输入文件:最少一个> \ n");

退出(1);

}

generate_mot();

mot_dump();

do {

printf(" infle ::%s \ n",argc [fcount]);

if((infle = fopen(argc [fcount]," r"))== NULL){

fprintf(stderr,"无法打开数据文件%s \ n",argc [fcount]);

}

else {

//做好工作

fclose(infle);

}

fcount ++;

} while(fcount< argv);


}


-------------------------------- - >

I have a linked list where each node holds a couple numbers a char
and a const char array. I use strcpy to take an char array
argument passed to my insert function to copy the string into
the new node.

To some of these entries 0a(line feed?) is being appended.
I''ve looked through the faq and googled but haven''t found much
info that applies.

The code follows this post(in a bit I''ll split it into multiple
files for use in a larger project).

Regards,
Newbie Grant

<-------------------------------

#include <stdio.h>
#include <stdlib.h>
struct motNode {
struct motNode * next;
unsigned int opCode;
char type;
unsigned int numArgs;
char opName[];
};
typedef struct motNode motList;
motList *mot_head, * mot_cur;
int mot_insert(unsigned int opCode, char type, unsigned int numArgs,
const char * opName){
mot_cur = (motList *)malloc(sizeof(motList));
mot_cur->opCode = opCode;
mot_cur->type = type;
mot_cur->numArgs= numArgs;
strcpy(mot_cur->opName, opName);
mot_cur->next = mot_head;
mot_head = mot_cur;
}

mot_dump(){
while(mot_cur){
printf("%s\n",mot_cur->opName);
mot_cur = mot_cur->next;
}
}

generate_mot(){
mot_head = NULL;
mot_insert(0,''r'',0,"hlt");
mot_insert(1,''r'',3,"add");
mot_insert(2,''r'',3,"sub");
mot_insert(3,''r'',3,"mul");
mot_insert(4,''r'',3,"div");
mot_insert(5,''r'',3,"mod");
mot_insert(6,''r'',2,"move");
mot_insert(7,''r'',3,"and");
mot_insert(8,''r'',3,"or");
mot_insert(9,''r'',3,"xor");
mot_insert(10,''r'',2,"com");
mot_insert(11,''r'',3,"sll");
mot_insert(12,''r'',3,"srl");
mot_insert(13,''r'',3,"sra");
mot_insert(14,''r'',3,"jr");
mot_insert(15,''r'',1,"rdr");
mot_insert(16,''r'',1,"prr");
mot_insert(17,''r'',1,"prh");

/* I format */
mot_insert(18,''i'',2,"li");
mot_insert(19,''i'',3,"addi");
mot_insert(20,''i'',3,"subi");
mot_insert(21,''i'',3,"muli");
mot_insert(22,''i'',3,"divi");
mot_insert(23,''i'',3,"modi");
mot_insert(24,''i'',3,"lwb");
mot_insert(25,''i'',3,"swb");

/* J format */
mot_insert(26,''j'',2,"lwa");
mot_insert(27,''i'',2,"swa");
mot_insert(28,''i'',1,"j");
mot_insert(29,''i'',1,"jal");
mot_insert(30,''i'',1,"jeq");
mot_insert(31,''i'',1,"jne");
mot_insert(32,''i'',1,"jlt");
mot_insert(33,''i'',1,"jle");
mot_insert(34,''i'',1,"jgt");
mot_insert(35,''i'',1,"jge");
}

main(int argv, char * argc[]){
FILE * infle;
int
eofl,
fcount = 2;

if(argv < 3){
fprintf(stderr,"Too few arguments\n");
fprintf(stderr,"\tusage:\tp2 <flag> <input files:minimum of one>\n");
exit(1);
}
generate_mot();
mot_dump();
do{
printf("infle :: %s\n",argc[fcount]);
if((infle = fopen(argc[fcount],"r")) == NULL){
fprintf(stderr,"Unable to open data file %s\n",argc[fcount]);
}
else{
//do work
fclose(infle);
}
fcount++;
} while(fcount < argv);

}

--------------------------------->

推荐答案

" Grant Austin" < GA ***** @ foo.foo.bar.net>在消息中写道

news:pa **************************** @ foo.foo.bar.ne t ...
"Grant Austin" <ga*****@foo.foo.bar.net> wrote in message
news:pa****************************@foo.foo.bar.ne t...
我有一个链表,每个节点都有几个数字,一个char
和一个const char数组。我使用strcpy将一个char数组
参数传递给我的insert函数,将字符串复制到新节点。

其中一些条目0a(换行?)正在追加。
我已经浏览了常见问题并用谷歌搜索了但没有找到太多适用的信息。

代码跟随这篇文章(稍后)我将它分成多个
文件,用于更大的项目。)

问候,
新手格兰特

< --- ----------------------------

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

struct motNode {
struct motNode * next;
unsigned int opCode;
char type;
unsigned int numArgs;
char opName [];
};

typedef struct motNode motList;


上面的两个声明可以组合成一个,你知道吗?

请注意,我不会这样做,类型定义结构很少一个好主意。


但是最重要的一点,可能是你的问题的根源是

你的opName字段的声明。你真的需要给它一个大小。

我很惊讶它甚至编译。

motList * mot_head,* mot_cur;


Globals是另一件几乎从不是个好主意的东西。

int mot_insert(unsigned int opCode,char type,unsigned int numArgs,
const char * opName){
mot_cur =(motList *)malloc(sizeof(motList));


这里有两件事。首先,不要从maloc()转换返回值。当你忘记

包含stdlib.h时,在C中没有必要使用
并且可能隐藏潜在的问题。其次,你需要检查malloc()的返回值。

mot_cur-> opCode = opCode;
mot_cur-> type = type;
mot_cur- > numArgs = numArgs;
strcpy(mot_cur-> opName,opName);


这是你的问题。你复制了一些东西到opName,但你在opName中有多少空间?b $ b? UB(未定义行为)的典型示例。

mot_cur-> next = mot_head;
mot_head = mot_cur;
}


您的函数被声明为返回int,那么它在哪里?更好的是,

让它返回一个指向新创建节点的指针并摆脱全局

变量。

mot_dump(){
while(mot_cur){
printf("%s\ n",mot_cur-> opName);
mot_cur = mot_cur-> next;
}
}
另一个无法返回int的函数,即使它被声明为

(虽然隐式)。此外,它依赖于在调用之前设置的mot_cur
。为什么不在功能中设置它?然后你可以

摆脱另一个全局变量。它需要一个指向头部的指针

作为参数。

generate_mot(){
mot_head = NULL;
mot_insert(0,''' r'',0," hlt");
mot_insert(1,''r'',3," add");
.... mot_insert(34,''i'',1," jgt");
mot_insert(35,''i'',1," jge");
}


又一次,'你的回报在哪里?

main(int argv,char * argc []){
FILE * infle;
int
eofl,
fcount = 2;

if(argv< 3){
fprintf(stderr,Too few arguments \\\
");
fprintf(stderr," \ tusage:\ tp2< flag><输入文件:最少一个> \ n");
exit(1 );


1不是从main()返回或在exit()中使用的可移植值。

使用stdlib中定义的EXIT_SUCCESS或EXIT_FAILURE。 h,你已经包含了


}
generate_mot();
mot_dump();
做{
printf(" infle ::%s \ n",argc [fcount]);
if((infle = fopen(argc [fcount]," r"))== NULL){
fprintf(stderr,无法打开数据文件%s \ n,argc [fcount]);
}
其他{
//做工作
fclose( infle);
}
fcount ++;
} while(fcount< argv);


返回EXIT_SUCCESS;

}

------------------ --------------->
I have a linked list where each node holds a couple numbers a char
and a const char array. I use strcpy to take an char array
argument passed to my insert function to copy the string into
the new node.

To some of these entries 0a(line feed?) is being appended.
I''ve looked through the faq and googled but haven''t found much
info that applies.

The code follows this post(in a bit I''ll split it into multiple
files for use in a larger project).

Regards,
Newbie Grant

<-------------------------------

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

struct motNode {
struct motNode * next;
unsigned int opCode;
char type;
unsigned int numArgs;
char opName[];
};

typedef struct motNode motList;
The two declarations above could be combined into one, did you know it?
Mind you, I wouldn''t do it, typedefing structures is rarely a good idea.

But the most important point and probably a source of your problem is
the declaration of your opName field. You really need to give it a size.
I am surprised it even compiles.
motList *mot_head, * mot_cur;
Globals are another thing that is almost never a good idea.
int mot_insert(unsigned int opCode, char type, unsigned int numArgs,
const char * opName){
mot_cur = (motList *)malloc(sizeof(motList));
Two things here. First, do not cast the return value from maloc(). It is
not necessary in C and may hide potential problems, when you forget to
include stdlib.h. Second, you need to check the malloc()''s return value.
mot_cur->opCode = opCode;
mot_cur->type = type;
mot_cur->numArgs= numArgs;
strcpy(mot_cur->opName, opName);
Here is your problem. You copy something to opName, but how much room
you have in opName? A typical example of UB (Undefined Behaviour).
mot_cur->next = mot_head;
mot_head = mot_cur;
}
Your function is declared as returning int, so where is it? Better yet,
let it return a pointer to your new created node and get rid of a global
variable.
mot_dump(){
while(mot_cur){
printf("%s\n",mot_cur->opName);
mot_cur = mot_cur->next;
}
} Another function that fails to return int, even though it is declared
as such (although implicitly). Also, it relies on mot_cur being set up
before calling. Why don''t you set it up in the function? Then you could
get rid of another global variable. It would need a pointer to a head
as a parameter.
generate_mot(){
mot_head = NULL;
mot_insert(0,''r'',0,"hlt");
mot_insert(1,''r'',3,"add"); .... mot_insert(34,''i'',1,"jgt");
mot_insert(35,''i'',1,"jge");
}
Yet again, where''s your return?
main(int argv, char * argc[]){
FILE * infle;
int
eofl,
fcount = 2;

if(argv < 3){
fprintf(stderr,"Too few arguments\n");
fprintf(stderr,"\tusage:\tp2 <flag> <input files:minimum of one>\n");
exit(1);
1 is not a portable value to be returned from main() or used in exit().
Use EXIT_SUCCESS or EXIT_FAILURE, both defined in stdlib.h, which you
already include.
}
generate_mot();
mot_dump();
do{
printf("infle :: %s\n",argc[fcount]);
if((infle = fopen(argc[fcount],"r")) == NULL){
fprintf(stderr,"Unable to open data file %s\n",argc[fcount]);
}
else{
//do work
fclose(infle);
}
fcount++;
} while(fcount < argv);
return EXIT_SUCCESS;
}

--------------------------------->



感谢所有评论,事情变得越来越清晰。 />
我会消化一段时间。 (=


我正在创建全局列表,因为我需要访问它在其他文件中的函数
。我认为可能有相当于

a将开销列表传递给那些函数。现在我意识到

我可以将指针传递给列表的头部。

问候,

-G
Thanks for all of the comments, things are becoming clearer.
I''ll be digesting for a while. (=

I''m creating the list as global because I will need access to it
in functions in other files. I thought that there might be quite
a bit of overhead passing the list to those functions. Now I realize
I can just pass a pointer to the head of the list.

Regards,
-G





Grant Austin写道:


Grant Austin wrote:
我有一个链表,每个节点都有几个数字,一个char
和一个const char数组。我用strcpy取一个char数组
参数传递给我的insert函数来复制将字符串转换为新节点。

其中一些条目0a(换行?)正在追加。
我已经浏览了常见问题并搜索了谷歌找不到适用的信息。
I have a linked list where each node holds a couple numbers a char
and a const char array. I use strcpy to take an char array
argument passed to my insert function to copy the string into
the new node.

To some of these entries 0a(line feed?) is being appended.
I''ve looked through the faq and googled but haven''t found much
info that applies.




.............代码剪断...... 。

有很多错误。

主要错误是你没有allo用于存储字符串的

结构的cate存储空间。这是你的代码修改为

显示你可能想要定义函数mot_insert的一种方式。


#include< stdio.h>

#include< stdlib.h>

#include< string.h>


struct motNode

{

struct motNode * next;

unsigned int opCode;

char type;

unsigned int numArgs;

char * opName;

};

typedef struct motNode mot_List;


int mot_insert(mot_List * * mot_head,unsigned int opCode,char type,

unsigned int numArgs,const char * opName);

void mot_dump(mot_List * mot_cur);

void generate_mot(mot_List ** mot_cur);

void mot_free(mot_List ** mot_cur);


int main(无效)

{

mot_List * mot_head = NULL;


generate_mot(& mot_head);

mot_dump(mot_head);

mot_free(& mot_head);

返回0;

}


in t mot_insert(mot_List ** mot_head,unsigned int opCode,char type,

unsigned int numArgs,const char * opName)

{

mot_List * mot_cur = malloc(sizeof * mot_cur);


如果(mot_cur == NULL)返回0;

if((mot_cur-> opName = malloc) (strlen(opName)+1))== NULL)

{

免费(mot_cur);

返回0;

}

mot_cur-> opCode = opCode;

mot_cur-> type = type;

mot_cur-> numArgs = numArgs;

strcpy(mot_cur-> opName,opName);

mot_cur-> next = * mot_head;

* mot_head = mot_cur;

返回1;

}


void mot_dump(mot_List * mot_cur)

{

while(mot_cur)

{

printf("%s\ n",mot_cur-> opName);

mot_cur = mot_cur-> next;

}

}


void generate_mot(mot_List ** mot_cur )

{

mot_insert(mot_cur,0,''r'',0,& ; hlt;;

mot_insert(mot_cur,1,''r'',3," add");

mot_insert(mot_cur,2,'' r'',3," sub");

mot_insert(mot_cur,3,''r'',3," mul");

mot_insert( mot_cur,4,''r'',3," div");

mot_insert(mot_cur,5,''r'',3," mod");

mot_insert(mot_cur,6,''r'',2," move");

mot_insert(mot_cur,7,''r'',3,"和 );

mot_insert(mot_cur,8,''r'',3,"或);

mot_insert(mot_cur,9,''r'', 3,xor);

mot_insert(mot_cur,10,''r'',2," com");

mot_insert(mot_cur,11, ''r'',3," sll");

mot_insert(mot_cur,12,''r'',3," srl");

mot_insert(mot_cur,13,''r'',3," sra");

mot_insert(mot_cur,14,''r'',3," jr");

mot_insert(mot_cur,15,''r'',1," rdr");

mot_insert(mot_cur,16,''r'',1," prr");

mot_insert(mot_cur,17,''r' ',1," prh");


/ *我格式* /

mot_insert(mot_cur,18,''我',2, " li");

mot_insert(mot_cur,19,''我',3," addi");

mot_insert(mot_cur,20,''我',3," subi");

mot_insert(mot_cur,21,''我',3," muli");

mot_insert( mot_cur,22,''我',3," divi");

mot_insert(mot_cur,23,''我',3," modi");

mot_insert(mot_cur,24,''i',3," lwb");

mot_insert(mot_cur,25,''我',3," swb" );


/ * J格式* /

mot_insert(mot_cur,26,''j'',2," lwa");

mot_insert(mot_cur,27,''i'',2," swa");

mot_insert(mot_cur,28,''i'',1," j");

mot_insert(mot_cur,29,''我',1, jal;;

mot_insert(mot_cur,30,''我',1," jeq");

mot_insert(mot_cur,31,''我',1," jne");

mot_insert(mot_cur,32,''我',1," jlt");

mot_insert( mot_cur,33,''我',1," jle");

mot_insert(mot_cur,34,''我',1," jgt");

mot_insert(mot_cur,35,''我',1," jge");

}


void mot_free(mot_List * * mot_cur)

{

mot_List * tmp;


for(; * mot_cur; * mot_cur = tmp)

{

tmp =(* mot_cur) - > next;

free(* mot_cur);

}

返回;

}

-

Al Bowers

Tampa,Fl USA

mailto: xa ****** @ myrapidsys.com (删除x以发送电子邮件)
http://www.geocities .com / abowers822 /



.............code snipped.......
There are many errors.
The major error is that you did not allocate storage space for the
struct that will store the string. Here is your code modified to
show one way you might want to define function mot_insert.

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

struct motNode
{
struct motNode * next;
unsigned int opCode;
char type;
unsigned int numArgs;
char *opName;
};
typedef struct motNode mot_List;

int mot_insert(mot_List **mot_head,unsigned int opCode, char type,
unsigned int numArgs, const char * opName);
void mot_dump(mot_List *mot_cur);
void generate_mot(mot_List **mot_cur);
void mot_free(mot_List **mot_cur);

int main(void)
{
mot_List *mot_head = NULL;

generate_mot(&mot_head);
mot_dump(mot_head);
mot_free(&mot_head);
return 0;
}

int mot_insert(mot_List **mot_head,unsigned int opCode, char type,
unsigned int numArgs, const char * opName)
{
mot_List *mot_cur = malloc(sizeof *mot_cur);

if(mot_cur == NULL) return 0;
if((mot_cur->opName = malloc(strlen(opName)+1)) == NULL)
{
free(mot_cur);
return 0;
}
mot_cur->opCode = opCode;
mot_cur->type = type;
mot_cur->numArgs= numArgs;
strcpy(mot_cur->opName, opName);
mot_cur->next = *mot_head;
*mot_head = mot_cur;
return 1;
}

void mot_dump(mot_List *mot_cur)
{
while(mot_cur)
{
printf("%s\n",mot_cur->opName);
mot_cur = mot_cur->next;
}
}

void generate_mot(mot_List **mot_cur)
{
mot_insert(mot_cur,0,''r'',0,"hlt");
mot_insert(mot_cur,1,''r'',3,"add");
mot_insert(mot_cur,2,''r'',3,"sub");
mot_insert(mot_cur,3,''r'',3,"mul");
mot_insert(mot_cur,4,''r'',3,"div");
mot_insert(mot_cur,5,''r'',3,"mod");
mot_insert(mot_cur,6,''r'',2,"move");
mot_insert(mot_cur,7,''r'',3,"and");
mot_insert(mot_cur,8,''r'',3,"or");
mot_insert(mot_cur,9,''r'',3,"xor");
mot_insert(mot_cur,10,''r'',2,"com");
mot_insert(mot_cur,11,''r'',3,"sll");
mot_insert(mot_cur,12,''r'',3,"srl");
mot_insert(mot_cur,13,''r'',3,"sra");
mot_insert(mot_cur,14,''r'',3,"jr");
mot_insert(mot_cur,15,''r'',1,"rdr");
mot_insert(mot_cur,16,''r'',1,"prr");
mot_insert(mot_cur,17,''r'',1,"prh");

/* I format */
mot_insert(mot_cur,18,''i'',2,"li");
mot_insert(mot_cur,19,''i'',3,"addi");
mot_insert(mot_cur,20,''i'',3,"subi");
mot_insert(mot_cur,21,''i'',3,"muli");
mot_insert(mot_cur,22,''i'',3,"divi");
mot_insert(mot_cur,23,''i'',3,"modi");
mot_insert(mot_cur,24,''i'',3,"lwb");
mot_insert(mot_cur,25,''i'',3,"swb");

/* J format */
mot_insert(mot_cur,26,''j'',2,"lwa");
mot_insert(mot_cur,27,''i'',2,"swa");
mot_insert(mot_cur,28,''i'',1,"j");
mot_insert(mot_cur,29,''i'',1,"jal");
mot_insert(mot_cur,30,''i'',1,"jeq");
mot_insert(mot_cur,31,''i'',1,"jne");
mot_insert(mot_cur,32,''i'',1,"jlt");
mot_insert(mot_cur,33,''i'',1,"jle");
mot_insert(mot_cur,34,''i'',1,"jgt");
mot_insert(mot_cur,35,''i'',1,"jge");
}

void mot_free(mot_List **mot_cur)
{
mot_List *tmp;

for( ; *mot_cur; *mot_cur = tmp)
{
tmp = (*mot_cur)->next;
free(*mot_cur);
}
return;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


这篇关于0a附加w / strcpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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