Hi I want to add last element into a vector of struct containing an array. I have tried to add it under its loop but has issues with constant char. It says "invalid conversion from ‘const char*’ to char" [-fpermissive]
This my file named as myfile.txt.log
id value
1 ABC
2 BDV
3 COO
4 DDC
5 EEE
6 FGE
7 GTW
8 HFE
9 IOO
10 KPP
Desired output:
vector size: 11
vector content: ABC
vector content: BDV
vector content: COO
vector content: DDC
vector content: EEE
vector content: FGE
vector content: GTW
vector content: HFE
vector content: IOO
vector content: KPP
vector content: XXX
Here is my code. Any inputs will be great! I am on C++ 98
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
struct V {
char a[10];
};
int main() {
FILE * pFile;
char mystring [100];
int i;
char str[3];
char strCp[3];
vector<V> input;
pFile = fopen ("/uploads/myfile.txt.log" , "r");
if (pFile == NULL) perror ("Error opening file");
else {
while ( fgets (mystring , 100 , pFile) != NULL )
{
sscanf(mystring, "%d %s", &i, str);
strncpy(strCp, str, 3);
//puts(strCp);
V v;
int size = sizeof(strCp)/sizeof(strCp[0]);
for ( unsigned int a = 0; a < size; a++ )
{
v.a[a] = strCp[a];
v.a[a-1] = "XXX";
}
// Set vector
input.push_back(v);
}
//Vector Output
cout << "vector size: " << input.size() << endl;
vector<V>::iterator it;
for ( it = input.begin(); it != input.end(); it++ )
{
cout << "vector content: " << it->a << endl;
}
fclose (pFile);
}
return 0;
}