我刚开始学习linux和C,请不要严格评判我。
我正在尝试查找当前工作目录并在此目录中打开文件以查找特定单词。如果我只找到 cwd,它会给出正确的 cwd,但是当我添加 while 循环 cwd 为空时。
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
main(){
char *buff;
char *currentDir =getcwd(buff,PATH_MAX);
printf("Current directory: %s\n",currentDir);
FILE *file;
char *filename = "profile";
file = fopen(filename,"r");
if(file == NULL)
{
fprintf(stderr,"File %s wasn't found\n",filename);
}
while(1)
{
char buffer[80];
char *token;
fgets(buffer,80,file);
if(feof(file))
{break;}
else{
*token = strtok(buffer,"=");
if(strcmp(token,"HOME")==1);
{
printf("HOME token is found");
}
}
free(token);
}
fclose(file);
}
输出:当前目录:(空)分段错误