strcpy

c/c++之strcat函数

守給你的承諾、 提交于 2019-12-20 07:03:05
我们首先来看这样一段代码: # include <iostream> # include <string.h> using namespace std ; int main ( ) { char pointer [ 24 ] = "Hello,world" ; cout << "The length of pointer:" << strlen ( pointer ) << endl ; strcat ( pointer , "China." ) ; cout << pointer << endl ; strcpy ( pointer , "Hello,world" ) ; strcat ( pointer + 3 , "China." ) ; cout << pointer << endl ; strcpy ( pointer , "Hello,world" ) ; strcat ( pointer + 8 , "China." ) ; cout << pointer << endl ; strcpy ( pointer , "Hello,world" ) ; strcat ( pointer + 11 , "China." ) ; cout << pointer << endl ; strcpy ( pointer , "Hello,world" ) ; strcat (

Socket网络编程--小小网盘程序(5)

核能气质少年 提交于 2019-12-19 08:59:46
  各位好呀!这一小节应该就是这个小小网盘程序的最后一小节了,这一节将实现最后的三个功能,即列出用户在服务器中的文件列表,还有删除用户在服务器中的文件,最后的可以共享文件给好友。   列出用户在服务器中的文件列表   增加一个结构体 1 struct FileList 2 { 3 int cnt; 4 char list[16][128]; 5 };   为了方便我就假设服务器最多可以存16个单个用户的文件。如果想要支持更多的文件,这里可以增加一个int pages;用于分页作用,我们在服务器中获取文件时,可以根据分页进行发送。这样既方便又能支持多文件。   client.cpp这个客户端文件增加一个函数 1 int file_list(struct Addr addr,struct User user) 2 { 3 struct sockaddr_in servAddr; 4 struct hostent *host; 5 struct Control control; 6 struct FileList filelist; 7 int sockfd; 8 9 host=gethostbyname(addr.host); 10 servAddr.sin_family=AF_INET; 11 servAddr.sin_addr=*((struct in_addr *)host->h

Difference between array and pointer [duplicate]

拥有回忆 提交于 2019-12-19 08:45:31
问题 This question already has answers here : Why does writing to a string literal in this C program segfault? (6 answers) What's wrong with my strcpy? [closed] (4 answers) Closed 6 years ago . Yesterday I had a little trouble with a homemade "strcpy" function. It works now though but I'm a little confused! char* a = "Hello, World!"; //Works char b[] = "Hello, World!"; //Works also strcpy(a, "Hello!"); //Segmentation fault strcpy(b, "Haha!!"); //Works.. Where is the difference? Why does the char

Difference between array and pointer [duplicate]

梦想的初衷 提交于 2019-12-19 08:44:31
问题 This question already has answers here : Why does writing to a string literal in this C program segfault? (6 answers) What's wrong with my strcpy? [closed] (4 answers) Closed 6 years ago . Yesterday I had a little trouble with a homemade "strcpy" function. It works now though but I'm a little confused! char* a = "Hello, World!"; //Works char b[] = "Hello, World!"; //Works also strcpy(a, "Hello!"); //Segmentation fault strcpy(b, "Haha!!"); //Works.. Where is the difference? Why does the char

C strcpy() - evil?

£可爱£侵袭症+ 提交于 2019-12-18 10:13:25
问题 Some people seem to think that C's strcpy() function is bad or evil. While I admit that it's usually better to use strncpy() in order to avoid buffer overflows, the following (an implementation of the strdup() function for those not lucky enough to have it) safely uses strcpy() and should never overflow: char *strdup(const char *s1) { char *s2 = malloc(strlen(s1)+1); if(s2 == NULL) { return NULL; } strcpy(s2, s1); return s2; } *s2 is guaranteed to have enough space to store *s1 , and using

strcpy_s not working with gcc

冷暖自知 提交于 2019-12-17 20:35:17
问题 I have a C++11 project, and I added some strcpy_s method calls. This works on windows, but when compiling on gcc, there is an error stating that strcpy_s symbol is not found. I did add the line #define __STDC_WANT_LIB_EXT1__ 1 to the code, to no avail. 回答1: GCC (or rather, glibc) does not support strcpy_s() and friends. For some ideas on where you can find a library which does support them, see here: Are there any free implementations of strcpy_s and/or TR24731-1? 回答2: strcpy_s and friends

Access violation when using strcpy?

拜拜、爱过 提交于 2019-12-17 19:54:11
问题 I've tried reinventing the strcpy C function, but when I try to run it I get this error: Unhandled exception at 0x00411506 in brainf%ck.exe: 0xC0000005: Access violation writing location 0x00415760. The error occurs in the *dest = *src; line. Here's the code: char* strcpy(char* dest, const char* src) { char* dest2 = dest; while (*src) { *dest = *src; src++; dest++; } *dest = '\0'; return dest2; } EDIT: Wow, that was fast. Here's the calling code (strcpy is defined in mystring.c): #include

Segmentation fault around strcpy?

狂风中的少年 提交于 2019-12-17 19:02:03
问题 I know that you will rap me over the knuckles but. Why does it make Segmentation fault char* cmd; strcpy(cmd, argv[0]); when this doesn't char *cmd; cmd = "plop"; I didn't practice since a while, and can't remember why. ps: actually, i know that something like that, before the strcpy, would be better char *cmd = (char*) malloc(strlen(argv[0])); but i'm just wondering why this segmentation fault. Thanks ! 回答1: When you do: char * cmd; You're allocating a pointer on the stack . This pointer is

实验五

一曲冷凌霜 提交于 2019-12-16 13:12:28
/* 假定输入的字符串中只包含字母和*号。 编写函数,实现: 除了字符串前导和尾部的*号之外,将串中其他*号全部删除。 在编写函数时,不得使用C语言提供的字符串函数。 例如,若字符串中的内容为****A*BC*DEF*G******* 删除后,字符串中的内容则应当是****ABCDEFG****** */ #include <stdio.h> #include <string.h> void fun(char *a) { int i=0; char *t = a, *f = a; char *q = a; while(*t)//把t定位到倒数第二个字符 t++; t--; while(*t=='*')//分析第三段 t--; while(*f=='*')//分析第一段 f++; while (q<f) { a[i] = *q; q++; i++; } while (q<t) { if(*q!='*') { a[i] = *q; i++; } q++; } while (*q) { a[i] = *q; i++; q++; } a[i]='\0'; } int main () { char s[81]; printf("Entre a string:\n"); gets(s); fun(s); printf("The sting after deleted:\n"); puts(s)

【TCHAR、_T与_TEXT】【strcpy、wcscpy与_tcscpy】(转)

瘦欲@ 提交于 2019-12-15 04:57:47
https://blog.csdn.net/lu_chaoqun/article/details/19208105 (内容出自《把脉VC++》,挺好的一本书,下面内容是我整理摘录的两小节,顺便吐槽微软还真是整天把C搞的多复杂,让人找来找去) 4.5.3 TCHAR、_T与_TEXT 计算机最初使用的不是UNICODE,最初的系统和程序都采用的是ANSI或者MBCS,那么,问题来了:我们开发程序的时候,是采用char还是wchar_t?观察如下代码: char msg[] = "学习C++"; 当我们准备采用wchar_t时,则需要改成: wchar_t msg[] = L"学习C++"; 太麻烦了!幸运的是,Visual C++的开发者预知到了这样的麻烦,他们从而为此造出了一批宏,这些宏看起来乱七八糟的,但是确实解决了我们的问题,且看一段代码: TCHAR msg[] = _T("学习C++"); 这段代码中,msg到底是char数组还是wchar_t数组呢?Visual C++的解释是,如果需要它是char数组,那么它就是char数组;否则,它就是wchar_t数组。这样的魅力来源于宏TCHAR的定义: typedef unsigned char CHAR; typedef unsigned wchar_t WCHAR; #ifdef UNICODE typedef wchar