У меня есть работающая программа c, которая определяет список и отображает строки, хранящиеся в списке.Я пытаюсь изменить код, чтобы вывести элементы char из списка для отображения в окне консоли, которое не работает.Ниже приведен код, который показывает не только закомментированные строки кода списка со строкой, но также и модифицированный код, чтобы получить элементы char.
struct List
{
char *data;
struct List *next;
};
typedef struct List list;
int main()
{
char line[25];
FILE *input_file;
list *tokens = NULL;
list *current = NULL;
input_file = fopen("test.evl", "r");
if (input_file == NULL)
{
printf("I cant read:%d\n", errno);
printf("I cant read:%s\n", strerror(errno));
perror("I cant read");
fprintf(stderr, "I cant read");
}
//while (fgets(line, sizeof(line), input_file) != NULL)
//{
// list *node = NULL;
// current = malloc(sizeof(list));
// current->data = strdup(line);
// current->next = NULL;
// if (tokens == NULL)
// {
// tokens = current;
// }
// else
// {
// node = tokens;
// while (node->next != NULL)
// node = node->next;
// node->next = current;
// }
//}
while (fgets(line, sizeof(line), input_file)!=NULL)
{
list *node = NULL;
current = malloc(sizeof(list));
for (size_t i = 0; i < sizeof(line); i++)
{
current->data = (line[i]);
current->next = NULL;
if (tokens == NULL)
{
tokens = current;
}
else
{
node = tokens;
while (node->next != NULL)
node = node->next;
node->next = current;
}
}
}
for (current = tokens; current; current = current->next)
{
//printf("%s", current->data);//for string output works
printf("%c\n", current->data);//for char output not working
}
if (input_file)
{
fclose(input_file);
}
getch();
return 0;
}
вывод для цикла while, который дает строки, которые я вижу, но цикла while, которыйЯ использую для реализации char не показывает вывод.Что может быть проблемой в моем подходе.Если мой подход верен, что может быть решением для получения вывода этого списка в виде типа символа?
Если во входном файле есть эта строка:
// модуль комментария top;
Тогда для строкового регистра вывод будет
// верхняя часть модуля комментария;
Но как только мы изменим его на символьный регистр, получится
/ /
a
комментарий
module
top;
Чтобы сделать короче говоря, мне нужно реализовать приведенный ниже код со списком
char line[25];
for (int line_no = 1;fgets(line, sizeof(line), input_file) != NULL; ++line_no)
{
// We should break line into tokens here
//for (int i = 0; i < 25; i++)
for (size_t i = 0; i < sizeof(line);++i)
{
printf("%c\n",line[i]);//need to implement ths logic with a list
}
//printf("%s", line);//this works with my list
}