英文:
I can't find a mistake in the function
问题
我正在用C语言编写模拟Linux命令"cat"的程序,但在函数内部找不到错误,该函数应该对非空字符串进行编号。
当我使用以下代码时,我得到了分段错误(segmentation fault):
void output_opt1_file(char *name_file) // This function opens a file and numbers only non-empty lines
{
FILE *f = fopen(name_file, "rt");
if (f != NULL) {
char *string;
fgets(string, 100, f);
int i = 1;
while (fgets(string, 100, f) != NULL) {
if (strlen(string) == 0) //if (string[0] == '\n')
{
printf("%s", string);//puts(string);
}
else
{
printf("%d%c", i, ' ');
++i;
printf("%s", string);//puts(string);
}
}
fclose(f);
}
}
我需要帮助找到我的错误。
英文:
I am writing analog linux comand cat by C but I can't find a mistake inside function, which must numerate non-empty strings.
I get: segmentation fault, when I used this function (the code below):
void output_opt1_file(char *name_file) // функция открывает файл и нумерует только не пустые строки
{
FILE *f = fopen(name_file, "rt");
if (f != NULL) {
char *string;
fgets(string, 100, f);
int i = 1;
while (fgets(string, 100, f) != NULL) {
if (strlen(string) == 0) //if (string[0] == '\n')
{
printf("%s", string);//puts(string);
}
else
{
printf("%d%c", i, ' ');
++i;
printf("%s", string);//puts(string);
}
}
fclose(f);
}
}
I need help for seeking my mistake.
答案1
得分: 1
你的代码至少有两个问题。
第一个问题是指针 string
未初始化。
char *string;
你需要声明一个字符数组,而不是指针,例如:
char string[100];
第二个问题是,这个 if 语句:
if (strlen(string) == 0)
永远不会评估为 true,因为函数 fgets
至少可以从文本文件中读取一个空字符串的新行字符 '\n'
。
相反,你可以这样写:
if (string[0] == '\n')
而且,在 while 循环之前的这个调用:
fgets(string, 100, f);
int i = 1;
while (fgets(string, 100, f) != NULL) {
会跳过第一条记录。
请注意,通常文件中的记录可以大于或等于 100
个字符。例如,如果文件中的记录正好包含 99
个字符以及新行字符 '\n'
,那么你的函数将把这个记录视为两个记录:记录的新行字符将被视为一个单独的第二个空记录。
英文:
Your code is wrong at least by two reasons.
The first one is that the pointer string
is uninitialized.
char *string;
You need to declare a character array instead of the pointer like for example
char string[100];
The second problem is that this if statement
if (strlen(string) == 0)
will never evaluate to logical true because the function fgets
at least can read a new line character '\n'
for an empty string from a text file.
Instead you could write
if ( string[0] == '\n')
And this call before the while loop
fgets(string, 100, f);
int i = 1;
while (fgets(string, 100, f) != NULL) {
skips the first record.
Pay attention to that in general a record in a file can be larger than or equal to 100
characters. For example if a record in a file contains exactly 99
characters plus the new line character '\n'
then your function will consider this record as two records: the new line character of the record will be considered as a separated second empty record.
答案2
得分: 0
这是工作版本:
void output_opt1_file(char *name_file)
{
FILE *f = fopen(name_file, "rt");
if (f != NULL) {
char string[100];
int i = 1;
while (fgets(string, 100, f) != NULL) {
if (string[0] == '\n')
{
printf("%s", string);
}
else
{
printf("%d%c", i, ' ');
++i;
printf("%s", string);
}
}
fclose(f);
}
}
英文:
It's working version:
void output_opt1_file(char *name_file)
{
FILE *f = fopen(name_file, "rt");
if (f != NULL) {
char string[100];
int i = 1;
while (fgets(string, 100, f) != NULL) {
if (string[0] == '\n')
{
printf("%s", string);
}
else
{
printf("%d%c", i, ' ');
++i;
printf("%s", string);
}
}
fclose(f);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论