【发布时间】:2017-05-29 11:31:23
【问题描述】:
创建一个将客户记录添加到文本文件的函数。 我已经制作了一个功能,可以修剪客户名称等的前导和最后一个空格,称为修剪空格。
函数 addrecord 用于处理记录在文件中的存储。它有 3 个参数(姓名/地址/电话)。在存储操作函数之前会使用 trimspaces 函数去除空格,然后将 3 个字符串合并为一个。
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h> //mkdir
#include <stdio.h> //printf
#include <errno.h> //error number
#include <unistd.h> //access
#include <string.h> //strcat
#include <ctype.h> //isspace
#include <stdlib.h>//malloc
int checkFile();
int makeFile();
int addRecord(char* name, char* addr, char* phon);
int searchRec(int column, char* value);
char* getRec(int recNo);
int getRecNo();
char* trimspaces(char* string,char*ptr);
int addRecord(char* name, char* addr, char* phon){
printf("\n- starting records addReord function -\n");
int success = 0;
char* namt = trimspaces(name,namt);
char* addt = trimspaces(addr,addt);
char* phot = trimspaces(phon,phot);
//this prints "trimmed words: , , "
printf("\n trimmed words: %s, %s, %s",namt,addt,phot);
/*
char*combined1 = strcat(namt,"|");
char*combined2 = strcat(combined1,addt);
char*combined3 = strcat(combined2,"|");
char*combined4 = strcat(combined3,phot);
printf("\nwords combined: %s",combined4);
*/
printf("\n- leaving records addrecord function -\n");
return success;
}
char* trimspaces(char* string,char*ptr){
printf("\n- entered trimspaces function -");
char *str= string;
int slen = strlen(str); //string length
int ctfor = 0; //counter forward
int ctbak = 0; //counter back
while(isspace(*str)){ str++; ctfor++; }; //count to start of word
while(*str){str++;}; //go to end
do{ str--; ctbak++; }while(isspace(*str)); //count from end to end of word
int cbako = (slen - ctbak) + 1; //counter back reversed
int wlen = cbako - ctfor; //get word length
printf("\nstr_len:%d,counter_fore:%d,counter_bak:%d,cbakreversed:%d,wlen:%d",slen,ctfor,ctbak,cbako,wlen);
while(*str){ str--; }
str++;
while(isspace(*str)){
str++;
}
char newStr[wlen]; //char pointer gives segmentation fault
memcpy(newStr,str,wlen);
printf("\n--%s--",newStr);
ptr = malloc(sizeof(newStr)+1);
ptr = newStr;
printf("\nPTR is : %s",ptr);
return ptr;
printf("\n- leaving trimspaces function -");
}
int main(){
addRecord("kara","19,sams st","993328");
}
这是输出: (我希望 --text-- 之间的文本是去掉前导/结尾空格的字符串,并且要说 timmed words 行 - TRIMMED words: kara,19,sams st,993328)
- starting records addReord function -
- entered trimspaces function -
str_len:4,counter_fore:0,counter_bak:1,cbakreversed:4,wlen:4
--kara--
PTR is : kara
- entered trimspaces function -
str_len:10,counter_fore:0,counter_bak:1,cbakreversed:10,wlen:10
--19,sams st@--
PTR is : 19,sams st@
- entered trimspaces function -
str_len:6,counter_fore:0,counter_bak:1,cbakreversed:6,wlen:6
@--93328s W
@TR is : 993328s W
TRIMMED words: , ,
- leaving records addrecord function -
我在 main 函数的输出中遇到了 2 个问题。首先是打印的字符串 - printf("\n TRIMMED words: %s, %s, %s",namt,addt,phot); 阅读:修剪过的词:,,, 我尝试了很多东西,但返回的变量总是空白。我想知道我是否正确使用了 malloc 和指针。
第二个问题是
--19,sams st@--
PTR is : 19,sams st@
@--93328s W
@TR is : 993328s W
我不知道@ 和 Ws 是从哪里来的。当我用不同的值测试 trimspaces 函数时,它打印出正确的结果。
我会注意到,我在终端上使用了 export PS1='\u@\h: ' 来获得更短的提示。
我应该怎么做才能让变量打印值?
【问题讨论】:
-
在您对
trimspaces的调用中,参数namt、addt和phot未初始化。此代码具有未定义的行为。 -
在
ptr = malloc(...); ptr = ...中,从malloc返回的值丢失了。这是内存泄漏。 -
你创建了一个局部变量
newStr[wlen];。该变量(及其内容)在函数返回后消失。并且您复制的字符串不是以 NULL 结尾的。 -
trimspaces返回newStr(实际上是指向newStr的第一个元素的指针),这是一个局部数组,函数返回后就不存在了。这是未定义的行为。 -
在许多平台上,
char是有符号类型(即它可以有一个负值)。将负值传递给isspace具有未定义的行为(除非它是EOF)。这应该是isspace((unsigned char)*str)。