Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
697 views
in Technique[技术] by (71.8m points)

c - program works but when I debug it says "<error reading characters in string.>" in line when reading file line by line

I wrote this code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    FILE *actionfpnt;
    char* line[70];
    actionfpnt = fopen(argv[1], "r");
    if (actionfpnt == NULL)
    {
        printf("Error: opening %s failed
", "actions");
    }
    else
    {
        while (fgets(line, 70, actionfpnt) != NULL)
        {
            printf("%s
", line);
        }
        fclose(actionfpnt);
        return 0;
    }
}

while argv[1] is an address to a txt file, and in this text file:

Initialize
Fatal_malfunction $$$ cpu Zi7 $$$ 130
Fatal_malfunction $$$ cpu Zi5 $$$ 15
Returned_from_customer $$$ cpu Zi7 $$$ 10
Rename $$$ bloblo12zZzZ $$$ Zbloblo
Returned_from_customer $$$ Zblabla $$$ 10
Finalize

the confusing part is that the code actually working but when I try to debug it, I see in the variable "line" : "<error reading characters in string.>". the output is:

Initialize

Fatal_malfunction $$$ cpu Zi7 $$$ 130

Fatal_malfunction $$$ cpu Zi5 $$$ 15

Returned_from_customer $$$ cpu Zi7 $$$ 10

Rename $$$ bloblo12zZzZ $$$ Zbloblo

Returned_from_customer $$$ Zblabla $$$ 10

Finalize

when I try to execute anything else with line, I cant because "line" doesnt really contain anything. I would really appreciate a way to solve this issue, and an explenation to why the code worked and got the right output even with this issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You want char line[70]; not char* line[70];!

char* line[70]; is an array of 70 pointers to char.


As fgets() includes reading the line's end you want to printf without an additional terminating :

  printf("%s", line);

And though as no formatting is required using fputs() would be sufficient:

  fputs(line, stdout);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...