문자열 문제점..
글쓴이: papa3721 / 작성시간: 목, 2007/10/04 - 8:29오전
이놈 하느라 잠도 못자고.. 고수님들 도와 주세요.. ㅠ.ㅠ
첫번째 나온 결과 값는 ID 만드는 함수에서 뽑하낸 것이고.
3번째 문자열 합친후 Q가 생기는데 왜 이러는지.. 도저히 알길이 없네요. ㅠ.ㅠ
제가 작성한 방법에 문제가 있나요?
많은 조언 부탁 드립니다.
==== 결과 ====
[20071014232341]
[mailbox/papa1/mail_list]
[mailbox/papa1/20071014232341Q]
[20071014232341 admin 2007-10-3 23:23:41 Welcome
]
=== 소스 코드 ==
#define BUFSIZE 1024 #define LINESIZE 100 #define LISTENQ 20 #define MODE 0777 #define MEMBERS "USERS" #define T_MEMBERS ".USERS" #define MAXSTRBUF 100 #define D_MAILBOX "mailbox" #define F_MAIL_LIST "mail_list" char serverName[50]; void trim(char s[]) { int i,len; len = strlen(s); for(i=0; i<len; i++){ if( (s[i] == ' ') || (s[i] == '\t') || (s[i] == '\r') || (s[i] == '\n')) break; } s[i]='\0'; } char * getNowTime() { time_t now; struct tm * theTime; char *t = (char *)malloc(sizeof(char) * MAXSTRBUF); time(&now); theTime=gmtime(&now); sprintf(t,"%d-%d-%d %d:%d:%d", theTime->tm_year+1900, theTime->tm_mon+1, theTime->tm_mday, theTime->tm_hour, theTime->tm_min, theTime->tm_sec); t=(char *)realloc(t,strlen(t)); return t; } char *getNewMailId() { time_t now; struct tm * theTime; char *id= (char *)malloc(sizeof(char)*MAXSTRBUF); memset(id,0,sizeof(char)*MAXSTRBUF); time(&now); theTime=gmtime(&now); theTime->tm_mon++; int mon = (theTime->tm_mon < 10) ? theTime->tm_mon+10 : theTime->tm_mon; int mday = (theTime->tm_mday < 10) ? theTime->tm_mday+11 : theTime->tm_mday; int hour = (theTime->tm_hour < 10) ? theTime->tm_hour+10 : theTime->tm_hour; int min = (theTime->tm_min < 10) ? theTime->tm_min+10 : theTime->tm_min; int sec = (theTime->tm_sec < 10) ? theTime->tm_sec+10 : theTime->tm_sec; sprintf(id,"%4d%2d%2d%2d%2d%2d", theTime->tm_year+1900, mon, mday, hour, min, sec); id = (char *)realloc(id, strlen(id)); printf("[%s]\n\n",id); return id; } // r=-1 not valid // r=0 valid int IsValidId(char *user, char *id) { FILE *fp; char id_buf[MAXSTRBUF]; int len=strlen(id); int r=0; char *mail_list_path_file = (char *)malloc(sizeof(char)*MAXSTRBUF); sprintf(mail_list_path_file, "%s/%s/%s", D_MAILBOX, user,F_MAIL_LIST); if( (fp = fopen(mail_list_path_file,"r")) == NULL) error_at_line(1, errno, __FILE__,__LINE__,"Error: file open %s",mail_list_path_file); while( (fgets(id_buf, MAXSTRBUF, fp)) != NULL){ if( (strncmp(id_buf, id, len)) == 0) r=-1; break; } fclose(fp); return (r); } void sendMail(char *from, char *to, char *subject, char *body) { FILE * fp_mail_list; // a file pointer for the mail_list file FILE * fp_mail_body; // a file pointer for the mail body file char *time; // time pointer char *id; // id pointer int n; time = getNowTime(); // get current time id = getNewMailId(); // get a new mail id while(1){ if((n=IsValidId(to, id)) < 0 ) id = getNewMailId(); // get a new mail id else break; } char *mail_list_path_file = (char *)malloc(sizeof(char)*MAXSTRBUF); char *mail_list_body_file = (char *)malloc(sizeof(char)*MAXSTRBUF); char *mail_list_content = (char *)malloc(sizeof(char)*MAXSTRBUF); memset(mail_list_path_file, 0,sizeof(char)*MAXSTRBUF); memset(mail_list_body_file, 0,sizeof(char)*MAXSTRBUF); memset(mail_list_content, 0,sizeof(char)*MAXSTRBUF); // to set the mail list path file sprintf(mail_list_path_file, "%s/%s/%s", D_MAILBOX, to, F_MAIL_LIST); sprintf(mail_list_body_file, "%s/%s/%s", D_MAILBOX, to, id); sprintf(mail_list_content, "%s\t%s\t%s\t%s\n", id, from, time, subject); mail_list_path_file = (char *)realloc(mail_list_path_file, strlen(mail_list_path_file)); mail_list_body_file = (char *)realloc(mail_list_body_file, strlen(mail_list_body_file)); mail_list_content = (char *)realloc(mail_list_content, strlen(mail_list_content)); printf("[%s]\n[%s]\n[%s]\n",mail_list_path_file,mail_list_body_file,mail_list_content); if( (fp_mail_list=fopen(mail_list_path_file, "a+")) == NULL) error_at_line(1, errno, __FILE__,__LINE__,"Error: file open %s",mail_list_path_file); fputs(mail_list_content, fp_mail_list); fclose(fp_mail_list); if( (fp_mail_body=fopen(mail_list_body_file, "w")) == NULL) error_at_line(1, errno, __FILE__,__LINE__,"Error: file open %s",mail_list_path_file); fputs(body, fp_mail_body); fclose(fp_mail_body); free(time); free(id); free(mail_list_path_file); free(mail_list_body_file); free(mail_list_content); } int main() { //int r; char *subject="Welcome"; char *body="This is a simple email system\n" "This beta test version is currently running on the linux sytem\n" "If you have any questions Please, feel free ask me.\n\n" "Thank you very much for choosing this system\n" " by admin\n"; sendMail("admin", "papa1", subject, body); return 0; }
Forums:
일반적인 NULL terminated
일반적인 NULL terminated 문자열 버퍼의 크기는 문자열의 최대 길이보다 1바이트이상 더 커야 합니다.
왜냐하면, 문자열의 마지막을 알리는 NULL이 꼭 포함되어야 하기 때문입니다.
malloc, realloc 하는 부분을 다시 체크 해 보세요.
소스에...
탭에 대한 공백수는 8이죠...
4로하면 다른쪽으로 소스를 옮겼을 때, 프린트할때 위와같이 깨짐이 생깁니다.
댓글 달기