【发布时间】:2016-12-09 12:58:52
【问题描述】:
我很好奇以前是否有人这样做过。
我在从结构中获取字符串时遇到问题。我要做的是从我正在使用的特定结构中获取字符串,然后将该字符串放入 fprintf("%s",whateverstring);
FILE* outfile = fopen("Z:\\NH\\instructions.txt","wb");
if ((dir = opendir ("Z:\\NH\\sqltesting\\")) != NULL) {// open directory and if it exists
while ((ent = readdir (dir)) != NULL) { //while the directory isn't null
printf("%s\n", ent->d_name); //I can do THIS okay
fprintf("%s\n",ent->d_name); //but I can't do this
fclose(outfile);
}
}
closedir (dir);
//else {
//
// perror (""); //print error and panic
// return EXIT_FAILURE;
//}
}
我在这里采取了错误的方法吗?我正在考虑以某种方式使用char[80] =ent.d_name;
但是显然这不起作用。有什么方法可以从结构中获取该字符串并将其放入 fprintf 中?
【问题讨论】:
-
嘿?你读过手册页吗?
-
另外,没有关于结构的信息。
-
fprintf()不采用格式字符串作为第一个参数。 -
fprintf(outfile,"%s", ent->d_name)。你必须给 fprintf 指向文件的指针作为第一个参数