这是我的困境.我有一个文件,并希望读取所有字符,直到程序达到'#',并忽略'#'后该行上的所有内容.例如
0 4001232 0 #comment,丢弃
这令人沮丧,因为感觉有一个非常简单的解决方案.谢谢!
我需要动态获取字符串,但因为我需要获得多个字符串,所以我需要使用函数.到目前为止,我写了这个(我把//****放在我认为可能错误的地方)
char* getstring(char *str);
int main() {
char *str;
strcpy(str,getstring(str));//*****
printf("\nString: %s", str);
return 0;
}
char* getstring(char str[]){//*****
//this part is copy paste from my teacher lol
char c;
int i = 0, j = 1;
str = (char*) malloc (sizeof(char));
printf("Input String:\n ");
while (c != '\n') {//as long as c is not "enter" copy to str
c = getc(stdin);
str = (char*)realloc(str, j * sizeof(char));
str[i] = c;
i++;
j++;
}
str[i] = '\0';//null at the end …Run Code Online (Sandbox Code Playgroud)