【发布时间】:2015-04-29 05:56:40
【问题描述】:
任务是编写一个 c 程序来打印从输入文件读取的行(可能非常非常大),但没有 '\n'。请看下面代码中的注释,是典型的方式还是好的方式??
int main() {
const char *input_wavlist_file = "/home/legend0011/Downloads/test-sample-list.txt";
const int BUFFER_SIZE = 100;
FILE *fr = fopen(input_wavlist_file, "r");
if (fr == NULL) {
printf("Error opening input wav list file!\n");
exit(1);
}
char str[BUFFER_SIZE];
while((fgets(str, BUFFER_SIZE, fr)) != NULL) {
char *pch = strchr(str, '\n');
char *filepath = str;
if (pch != NULL) *pch = '\0'; // is this a typical way????????
printf("reading==>%s",filepath);
}
fclose(fr);
}
【问题讨论】:
-
你没有写入文件.. 你到底想做什么?看起来您正在尝试从文件中读取。所以你的意思是在不读取
\n的情况下读取文件 -
@Gopi 我们都误解了.. OP 想从文件打印到标准输出.. :-)
-
只需在 shell 中打印。或者您可以将其写入 txt 文件。两个都很好!!
-
我只是想问一下用 c 风格写 string substring = source.substr(0, len) 的正确方法是什么
-
@SouravGhosh 是的,问题标题误导了我们