Functions in c and c++ with Arrays as Parameters and Arguments 4

Опубликовано: 16 Октябрь 2024
на канале: Arif Mahmood
40
0

Shows how to define #functions in c and c plus plus programs. Shows how to convert a program implementation without functions to an implementation using functions like #bool, #void, string and char.. Shows how to #pass #array as #function #parameters and #arguments.

C code:


#include<stdio.h>
#include<stdbool.h> // for bool functions

bool pullout_contents(char *in_file, char elements[][50])
//char pullout_contents(char *in_file, char elements[][50])
//void pullout_contents(char *in_file, char elements[][50])
{
FILE *input_file = fopen(in_file, "r");

char line[50];
for (int line_no = 1; fgets(line, sizeof(line), input_file) != NULL; ++line_no)
{
strcpy(elements[line_no], line);
}

fclose(input_file);
}

bool show_contents(const char elements[][50])
//char show_contents(const char elements[][50])
//void show_contents(const char elements[][50])
{
for (int line_no = 1; line_no <3; ++line_no)
{
printf("LINE %d: %s", line_no, elements[line_no]);
}
}

bool pushin_contents(char out_file[], const char elements[][50])
//char pushin_contents(char out_file[], const char elements[][50])
//void pushin_contents(char out_file[], const char elements[][50])
{
FILE* output_file= fopen(out_file, "w");//w for write a for append

for (int line_no = 1; line_no <3; ++line_no)
{
fprintf(output_file, "LINE %d: %s", line_no, elements[line_no]);
}

fclose(output_file);
}

int main()
{
char in_file[] = "test.txt";
//FILE *input_file = fopen(in_file, "r");

char out_file[] = "test.txt.out";
//FILE *output_file = fopen(out_file, "w");//w for write a for append

char elements[25][50];

//char line[25];
//for (int line_no = 1; fgets(line, sizeof(line), input_file) != NULL; ++line_no)
//{
// strcpy(elements[line_no], line);
//}

pullout_contents(in_file, elements);

//for (int line_no = 1; line_no <3; ++line_no)
//{
// //printf("LINE %d: %s", line_no, elements[line_no]);
// fprintf(output_file, "LINE %d: %s", line_no, elements[line_no]);
//}

show_contents(elements);

pushin_contents(out_file, elements);

//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

bool pullout_contents(std::string in_file, std::string elements[])
//std::string pullout_contents(std::string in_file, std::string elements[])
//void pullout_contents(std::string in_file, std::string elements[])
{
std::ifstream input_file(in_file.c_str());

std::string line;
for (int line_no = 1; std::getline(input_file, line); ++line_no)
{
elements[line_no]=line;
}

input_file.close();
//return *elements;
return true;
}

bool show_contents(const std::string elements[])
//std::string show_contents(const std::string elements[])
//void show_contents(const std::string elements[])
{
for (int line_no = 1; line_no < 3; line_no++)
{
std::cout << "LINE " << line_no << ":" << elements[line_no] << std::endl;
}

//return *elements;
return true;
}

bool pushin_contents(std::string out_file, const std::string elements[])
//std::string pushin_contents(std::string out_file, const std::string elements[])
//void pushin_contents(std::string out_file, const std::string elements[])
{
std::ofstream output_file(out_file.c_str());

for (int line_no = 1; line_no < 3; line_no++)
{
output_file << "LINE " << line_no << ":" << elements[line_no] << std::endl;
}

output_file.close();
//return *elements;
return true;
}

int main()
{
std::string in_file = "test.txt";
//std::ifstream input_file(in_file.c_str());

std::string out_file = "test.txt.out";
//std::ofstream output_file(out_file.c_str());

std::string elements[100];

//std::string line;
//for (int line_no = 1; std::getline(input_file, line); ++line_no)
//{
// elements[line_no] = line;
//}

pullout_contents(in_file, elements);

//for (int line_no = 1; line_no < 3; line_no++)
//{
// //std::cout << "LINE " << line_no << ":" << elements[line_no] << std::endl;
// output_file << "LINE " << line_no << ":" << elements[line_no] << std::endl;
//}

show_contents(elements);

pushin_contents(out_file, elements);

//if (input_file)
//{
// input_file.close();
//output_file.close();
//}

std::cin.get();
return 0;
}


#c #cplusplus #tutorial

C++ tutorial functions and function examples
introduction to function in c++