我在这个函数里找不到错误。

huangapple go评论92阅读模式
英文:

I can't find a mistake in the function

问题

我正在用C语言编写模拟Linux命令"cat"的程序,但在函数内部找不到错误,该函数应该对非空字符串进行编号。

当我使用以下代码时,我得到了分段错误(segmentation fault):

  1. void output_opt1_file(char *name_file) // This function opens a file and numbers only non-empty lines
  2. {
  3. FILE *f = fopen(name_file, "rt");
  4. if (f != NULL) {
  5. char *string;
  6. fgets(string, 100, f);
  7. int i = 1;
  8. while (fgets(string, 100, f) != NULL) {
  9. if (strlen(string) == 0) //if (string[0] == '\n')
  10. {
  11. printf("%s", string);//puts(string);
  12. }
  13. else
  14. {
  15. printf("%d%c", i, ' ');
  16. ++i;
  17. printf("%s", string);//puts(string);
  18. }
  19. }
  20. fclose(f);
  21. }
  22. }

我需要帮助找到我的错误。

英文:

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):

  1. void output_opt1_file(char *name_file) // функция открывает файл и нумерует только не пустые строки
  2. {
  3. FILE *f = fopen(name_file, "rt");
  4. if (f != NULL) {
  5. char *string;
  6. fgets(string, 100, f);
  7. int i = 1;
  8. while (fgets(string, 100, f) != NULL) {
  9. if (strlen(string) == 0) //if (string[0] == '\n')
  10. {
  11. printf("%s", string);//puts(string);
  12. }
  13. else
  14. {
  15. printf("%d%c", i, ' ');
  16. ++i;
  17. printf("%s", string);//puts(string);
  18. }
  19. }
  20. fclose(f);
  21. }
  22. }

I need help for seeking my mistake.

答案1

得分: 1

你的代码至少有两个问题。

第一个问题是指针 string 未初始化。

  1. char *string;

你需要声明一个字符数组,而不是指针,例如:

  1. char string[100];

第二个问题是,这个 if 语句:

  1. if (strlen(string) == 0)

永远不会评估为 true,因为函数 fgets 至少可以从文本文件中读取一个空字符串的新行字符 '\n'

相反,你可以这样写:

  1. if (string[0] == '\n')

而且,在 while 循环之前的这个调用:

  1. fgets(string, 100, f);
  2. int i = 1;
  3. 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.

  1. char *string;

You need to declare a character array instead of the pointer like for example

  1. char string[100];

The second problem is that this if statement

  1. 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

  1. if ( string[0] == '\n')

And this call before the while loop

  1. fgets(string, 100, f);
  2. int i = 1;
  3. 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

这是工作版本:

  1. void output_opt1_file(char *name_file)
  2. {
  3. FILE *f = fopen(name_file, "rt");
  4. if (f != NULL) {
  5. char string[100];
  6. int i = 1;
  7. while (fgets(string, 100, f) != NULL) {
  8. if (string[0] == '\n')
  9. {
  10. printf("%s", string);
  11. }
  12. else
  13. {
  14. printf("%d%c", i, ' ');
  15. ++i;
  16. printf("%s", string);
  17. }
  18. }
  19. fclose(f);
  20. }
  21. }
英文:

It's working version:

  1. void output_opt1_file(char *name_file)
  2. {
  3. FILE *f = fopen(name_file, "rt");
  4. if (f != NULL) {
  5. char string[100];
  6. int i = 1;
  7. while (fgets(string, 100, f) != NULL) {
  8. if (string[0] == '\n')
  9. {
  10. printf("%s", string);
  11. }
  12. else
  13. {
  14. printf("%d%c", i, ' ');
  15. ++i;
  16. printf("%s", string);
  17. }
  18. }
  19. fclose(f);
  20. }
  21. }

huangapple
  • 本文由 发表于 2023年7月11日 05:04:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定