Reputation: 1
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\n", "actions");
}
else
{
while (fgets(line, 70, actionfpnt) != NULL)
{
printf("%s\n", 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.
Upvotes: 0
Views: 35
Reputation: 70893
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 \n
:
printf("%s", line);
And though as no formatting is required using fputs()
would be sufficient:
fputs(line, stdout);
Upvotes: 2