标签: strncpy

C 中将字符从字符串复制到另一个字符串

我有一个字符串 AAbbCC 我需要的是复制前两个并将它们添加到数组中,然后复制中间两个并将它们添加到数组中,最后复制最后两个并将它们添加到数组中。

这就是我所做的:

char color1[2];
char color2[2];
char color3[2];

strncpy(color1, string, 2); // I take the first two characters and put them into color1

// now I truncate the string to remove those AA values:

string = strtok(string, &color1[1]);

// and when using the same code again the result in color2 is bbAA:

strncpy(color2, string, 2); 
Run Code Online (Sandbox Code Playgroud)

它通过了那些 bb,但也通过了前一个的 AA ..即使数组只有两个位置,当我在其上使用 strtol 时,它给了我一些大值,而不是我正在寻找的 187 ..如何摆脱它?或者如何让它以其他方式工作?任何意见,将不胜感激。

c string truncate char strncpy

3
推荐指数
1
解决办法
3万
查看次数

在C中更有效地使用strncpy复制n个字符

我想知道strncpy考虑到max一些字符,是否有一种更干净,更有效的方法来做以下事情.我觉得自己过度了.

int main(void)
{

        char *string = "hello world foo!";
        int max = 5;

        char *str = malloc (max + 1);
        if (str == NULL)
                return 1;
        if (string) {
                int len = strlen (string);
                if (len > max) {
                        strncpy (str, string, max);
                        str[max] = '\0';
                } else {
                        strncpy (str, string, len);
                        str[len] = '\0';
                }
                printf("%s\n", str);
        }
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

c string malloc strncpy

2
推荐指数
1
解决办法
8667
查看次数

使用strcpy时打印垃圾

我有一个函数可以解析一些数据.我的问题是在使用strncpy后,当我尝试打印它时会得到一些垃圾.我尝试使用malloc使char数组具有确切的大小.

码:

void parse_data(char *unparsed_data)
{
char *temp_str;
char *pos;
char *pos2;
char *key;
char *data;
const char newline = '\n';

int timestamp = 0;

temp_str = (char*)malloc(strlen(unparsed_data));

g_print("\nThe original string is: \n%s\n",unparsed_data);


//Ignore the first two lines
pos = strchr(unparsed_data, newline);
strcpy(temp_str, pos+1);
pos = strchr(temp_str, newline);
strcpy(temp_str, pos+1);

//Split the line in two; The key name and the value
pos = strchr(temp_str, ':'); // ':' divides the name from the value
pos2 = strchr(temp_str, '\n'); //end of the …
Run Code Online (Sandbox Code Playgroud)

c malloc parsing text strncpy

2
推荐指数
1
解决办法
4327
查看次数

strncpy中的SEGMENTATION FAULT - 来自字典的加载

我有这个函数"加载",我从字典中读取单词并将它们放在链表的哈希表中.当我尝试读取一行并将其保存在我的new_node-> text中时,编译器返回SEGMENTATION FAULT,我不知道为什么.当我使用strncpy时,错误会出现.

#define HASHTABLE_SIZE 76801


typedef struct node
{
        char text[LENGTH+1];
        //char* text;
        //link to the next word
        struct node* next_word;
}
node;


    node* hashtable[HASHTABLE_SIZE];

    bool load(const char* dictionary)
    {
        FILE* file = fopen(dictionary,"r");
        unsigned long index = 0;
        char str[LENGTH+1];

        if(file == NULL)
        {
            printf("Error opening file!");
            return false;
        }

        while(! feof(file))
        {
            node * new_node = malloc(sizeof(node)+1000);


            while( fscanf(file,"%s",str) > 0)
            {
                printf("The word is %s",str);
                strncpy(new_node->text,str,LENGTH+1);
                //strcpy(new_node->text,str);

                new_node->next_word = NULL;
                index = hash( (unsigned char*)new_node->text);

                if(hashtable[index] …
Run Code Online (Sandbox Code Playgroud)

c load segmentation-fault strcpy strncpy

2
推荐指数
1
解决办法
425
查看次数

使用分配的内存将整数复制到整数

我对下面的代码有问题。

...
int anInteger;
...
//anInteger gets a value
...

int *anotherInteger;
label = (int *)malloc(sizeof(int));
strncpy(anotherInteger, anInteger, 40);
Run Code Online (Sandbox Code Playgroud)

基本上我想要做的是将值从一个整数复制到我已经为其分配内存的另一个整数。这可以在整数之间使用 strncpy 还是我需要另一个函数?

c integer memory-management copy strncpy

2
推荐指数
1
解决办法
1万
查看次数

奇怪的strncpy响应

我只是运行这个代码,我得到的n = 1不是我期望得到的.你能解释一下为什么会这样吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXRIGA 11
int main()
{
    char s[MAXRIGA+2];
    char a[MAXRIGA]="pippo";
    strncpy(s, a, 1); //       n=1
    printf("%s", s);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

回报

pF
Run Code Online (Sandbox Code Playgroud)

相反,如果n = 2或更多,我得到我想要的.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXRIGA 11
int main()
{
    char s[MAXRIGA+2];
    char a[MAXRIGA]="pippo";
    strncpy(s, a, 2); //       n=2
    printf("%s", s);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

回报

pi
Run Code Online (Sandbox Code Playgroud)

c string strcpy strncpy

2
推荐指数
2
解决办法
183
查看次数

在结构上使用 strncpy

假设我有这个student struct定义:

struct student {
    char *name;
};
typedef struct student Student
Run Code Online (Sandbox Code Playgroud)

现在我有以下功能:

void add_student(const char *student_name) {

    // create new student
    Student *new_s;
    new_s = malloc(sizeof(Student));

    strncpy(new_s->name, student_name, sizeof(new_s->name) - 1)
}
Run Code Online (Sandbox Code Playgroud)

我想将 student_name 添加到新学生结构的名称中。但是因为const charchar不同,我必须使用strncpy.

我以这种方式尝试过,但是出现分段错误,怎么了?

c struct constants char strncpy

2
推荐指数
1
解决办法
1603
查看次数

strncpy/memcpy/memmove 是否逐字节或以其他有效方式复制数据?

众所周知,在 x86/x86_64 等多字节字计算机中,逐字复制/移动大量内存(每步 4 或 8 个字节)比逐字节复制/移动更有效。

我很好奇 strncpy/memcpy/memmove 会以哪种方式做事,以及它们如何处理内存字对齐。

char buf_A[8], buf_B[8];

// I often want to code as this
*(double*)buf_A = *(double*)buf_B;

//in stead of this
strcpy(buf_A, buf_B);
// but it worsen the readability of my codes.
Run Code Online (Sandbox Code Playgroud)

memcpy effective-c++ strcpy strncpy memmove

2
推荐指数
1
解决办法
2741
查看次数

strcpy 如何改变字符串的值

我做了这个作业来决定以下代码将做什么(纸上,没有在计算机上测试)。

char s1[]="Short Message Service", *s2, *s3;
s2=strchr(s1,'M');
s3=strchr(s2,'S');
strncpy(s1+1,s2,1);
strcpy(s1+2,s3);
Run Code Online (Sandbox Code Playgroud)

当我想检查我是否做得对时,我在计算机上运行它并得到以下结果:

s1 = SMservice 

s2 = ice

s3 = Service
Run Code Online (Sandbox Code Playgroud)

我以为s2会是,"Message Service"但它变成了"ice". 显然它在strcpy(s1+2,s3)被调用后发生了变化;有人可以解释为什么以及如何影响该功能s2吗?

c string strcpy strncpy

2
推荐指数
1
解决办法
422
查看次数

当 dest 小于 src 但又足够大以容纳 src 所需的子字符串时,为什么 strncpy() 会产生垃圾?

我尝试使用strncpy()限制复制到 dest (此处为str1 )的 n 字节数。\ndest 对于 n 字节来说足够大,但是当 dest 小于源时(此处为argv[1 ])。\n当我使 dest 足够大以容纳源时,这看起来有所不同。

\n

这是代码:

\n
#include <stdio.h>                                                              \n#include <string.h>                                                             \n                                                                                \nint main(int argc, char *argv[])                                                \n{                                                                               \n/* // Works when str1[?] is 20:                                                                    \n  char str1[20];                                                                \n  char str2[20]; */                                                             \n                                                                                \n  // produces garbage, str1 is smaller than argv[1] but big enough for the 4 bytes copied                                                            \n  char str1[10];                                                                \n  char str2[10];                                                                \n                                                                                \n  printf("argc = %i\\n", argc);                                                  \n  if (argc <= …
Run Code Online (Sandbox Code Playgroud)

c string strcpy strncpy

2
推荐指数
1
解决办法
336
查看次数