Convert String List Data Structure in c and c++ to Struct List Data Structure 11

Опубликовано: 07 Апрель 2026
на канале: Arif Mahmood
101
0

Shows how to #convert #string #list #data #structure to a #struct #list #data #structure in c and c plus plus #programs.


c code:

#include<stdio.h>

typedef struct File_element
{
char line[25];
}file_element;

typedef struct List
{
//char *data;
file_element *data;
struct List *next;
}list;


int main()
{
char *in_file = "test.txt";
char *out_file = "test.txt.out";

FILE *input_file = fopen(in_file, "r");//r for read
FILE *output_file = fopen(out_file, "w");//w for write

list *elements = NULL;
list *iter =NULL;
list *node = NULL;

char line[25];
for (; fgets(line, sizeof(line), input_file) != NULL;)
{
iter = malloc(sizeof(list));
//iter->data = malloc(strlen(line));//lenght of string which varies
iter->data = malloc(sizeof(file_element));//size must be this
//in case struct has more than one members

file_element element ;
*element.line = malloc(sizeof(file_element));//
strcpy(element.line, line);//

//strcpy(iter->data, line);//
*iter->data = element;//works
iter->next = NULL;

if (elements == NULL)
{
node = elements = iter;
}
else
{
node = node->next = iter;
}
}

for (iter= elements ; iter != NULL; iter = iter->next)
{
//printf("%s", iter->data);
//fprintf(output_file, "%s", iter->data);
printf("%s", iter->data->line);
fprintf(output_file," %s", iter->data->line);
}


free(iter);

if (input_file)
{
fclose(input_file);
fclose(output_file);
}
getch();
return 0;
}



c++ code:

#include <iostream> //for cout
#include <fstream> // for ofstream/ifstream
#include <string> //for << and getline
#include <list> //for lists

struct file_element
{
std::string line;
};

//struct List
//{
// //std::string data;
// file_element data;
// List *next;
//};


int main()
{
std::string in_file = "test.txt";
std::string out_file = "test.txt.out";

std::ifstream input_file(in_file.c_str());
std::ofstream output_file(out_file.c_str());

//List *elements = NULL;
//List *iter = NULL;
//List *node = NULL;

//std::list<std::string> elements;
std::list<file_element> elements;

std::string line;
for (; std::getline(input_file, line);)
{
//iter = new List;
file_element element;
element.line = line;

// //iter->data = line;
// iter->data = element;
// iter->next = NULL;

// if (elements == NULL)
// {
// node = elements = iter;
// }
// else
// {
// node = node->next = iter;
// }
//elements.push_back(line);
elements.push_back(element);
}

//for (iter = elements; iter != NULL; iter = iter->next)
//{
// //std::cout << iter->data << std::endl;
// //output_file << iter->data << std::endl;
// std::cout << iter->data.line << std::endl;
// output_file << iter->data.line << std::endl;
//}

std::list<file_element>::iterator iter = elements.begin();
//std::list<std::string>::iterator iter = elements.begin();
for (; iter != elements.end(); ++iter)
{
//std::cout << *iter << std::endl;
//output_file << *iter << std::endl;
std::cout << (*iter).line << std::endl;
output_file << (*iter).line << std::endl;
}

//delete iter;

if (input_file)
{
input_file.close();
output_file.close();
}
std::cin.get();
return 0;
}

#c #cplusplus #tutorial

C++ Tutorial structures and abstract datatypes C