这是LIST ftp的cmd的代码段:
count = file_list("./", &files);
if((fp_list = fopen("listfiles.txt", "w")) == NULL){
perror("Impossibile aprire il file per la scrittura LIST");
onexit(newsockd, sockd, 0, 2);
}
for(i=0; i < count; i++){
if(strcmp(files[i], "DIR ..") == 0 || strcmp(files[i], "DIR .") == 0) continue;
else{
fprintf(fp_list, "%s\n", files[i]);
}
}
fclose(fp_list);
if((fpl = open("listfiles.txt", O_RDONLY)) < 0){
perror("open file with open");
onexit(newsockd, sockd, 0, 2);
exit(1);
}
if(fstat(fpl, &fileStat) < 0){
perror("Errore fstat");
onexit(newsockd, sockd, fpl, 3);
}
fsize = fileStat.st_size;
if(send(newsockd, …Run Code Online (Sandbox Code Playgroud) 说我有:
unsigned char *varA, *varB, *varC;
varA=malloc(64);
varB=malloc(32);
varC=malloc(32);
Run Code Online (Sandbox Code Playgroud)
如何将varA 的前 32个字节放入varB,将varA的最后 32个字节放入varC?
int main(int argc, char **argv){
char *str = argv[1];
size_t len = strlen(str);
char *dst = calloc(len+1, sizeof(char));
int i;
for(i=len-1; i>=0; i--){
memcpy(dst, str+i, 1);
dst++;
}
*(++dst) = '\0';
printf("%s\n", dst);
free(dst);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
省略错误检查以获得更多可读性
我不明白为什么我得到了一条带有此消息的段错误:"glibc free():无效指针:"
我正在尝试编写一个使用指针反转字符串的小程序但是:
1) last printf什么都不打印;
2)我遇到了段错误
我使用fstat来获取文件大小.我想使用此大小声明一个数组,然后用另一个fstat更改大小并重新使用相同的数组.例:
fstat(file1, &fileStat);
fsize = filestat.st_size;
char filebuffer[size-of-file1];
/* do something */
fstat(file2, &fileStat);
fsize = filestat.st_size;
char filebuffer[size-of-file2];
/* do something */
Run Code Online (Sandbox Code Playgroud)
显然我不能重新声明filebuffer数组,我必须声明一个新的.但是,如果我想重新使用不同大小的相同数组,我该怎么办?
谢谢!!
编辑:
filebuffer = malloc(fsize);
if(filebuffer == NULL){
perror("malloc");
onexit(sockd, 0, fd, 4);
}
Run Code Online (Sandbox Code Playgroud)
和
tmpfilebuf = realloc(filebuffer, fsize);
if(tmpfilebuf){
filebuffer = tmpfilebuf;
}
else{
perror("realloc");
free(filebuffer);
onexit(sockd, 0, fd, 4);
}
Run Code Online (Sandbox Code Playgroud)
但现在我得到了一个段错误:(
我有这些变种:
uint32_t fsize;
char *filebuffer = NULL;
fsize = fileStat.st_size;
Run Code Online (Sandbox Code Playgroud)
如果我想要malloc正确的内存我必须编写的代码是这样的:
malloc(fsize);
Run Code Online (Sandbox Code Playgroud)
或这个:
malloc(fsize * sizeof(char *));
Run Code Online (Sandbox Code Playgroud)
谢谢!
这是我的代码:
tmpip = get_public_ip();
pubip = strtok(tmpip, "\n");
sprintf(buffer, "220 FTPUtils Server [%s]", pubip);
len_string = strlen(buffer)+1;
if(send(newsockd, &len_string, sizeof(len_string), 0) < 0){
perror("Errore invio len buffer");
onexit(newsockd, sockd, 0, 2);
}
if(send(newsockd, buffer, len_string, 0) < 0){
perror("Errore durante l'invio");
onexit(newsockd, sockd, 0, 2);
}
pubip = NULL;
free(tmpip);
memset(buffer, 0, sizeof(buffer));
if(recv(newsockd, &len_string, sizeof(len_string), MSG_WAITALL) < 0){
perror("Errore ricezione len user buffer");
onexit(newsockd, sockd, 0, 2);
}
if(recv(newsockd, buffer, len_string, 0) < 0){
perror("Errore ricezione del nome utente"); …Run Code Online (Sandbox Code Playgroud) 这是我的代码:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
int num_mezzo_1(int num_orig);
int num_mezzo_2(int num_orig);
int main(int argc, char *argv[]){
int num,contatore,tmp=0,tmp_1,tmp_2;
num=atoi(argv[1]);
if(num <= 3){
printf("%d è primo\n", num);
exit(0);
}
else{
num_mezzo_1(num);
num_mezzo_2(num);
tmp=tmp_1+tmp_2;
//using printf to debug
printf("t1 %d, t2 %d\n", tmp_1,tmp_2);
if(tmp>2){
printf("%d NON è primo\n", num);
}
else{
printf("%d è primo\n", num);
}
}
exit(0);
}
int num_mezzo_1(int num_orig){
int tmp_1=0,cont_1;
for(cont_1=1; cont_1<=(num_orig/2); cont_1++){
if((num_orig % cont_1) == 0){
tmp_1++;
}
}
//using printf to …Run Code Online (Sandbox Code Playgroud) 我开发了一个小型(非常小)的ftp客户端和服务器.
这两个程序只有在使用相同的arch时才能编译和运行.
我怎样才能使这两个程序在不同的拱门上工作?
这是一个代码问题?如果是这样我必须做什么?
提前致谢!
c ×8
pointers ×2
architecture ×1
arrays ×1
function ×1
malloc ×1
return-value ×1
sendfile ×1
sockets ×1
string ×1