-2

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;
}

   
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Bebe
  • 77
  • 1
  • 14
  • You want a vector size of 12, but your list is only 11 items long. Can you elaborate? Or did you mean a vector of 11 items? – selbie Sep 23 '21 at 05:58
  • 1
    Remember that C strings are *null-terminated*. A string of three characters needs space for *four* character to include the null-terminator. Since you don't have that space, your program will have *undefined behavior* once you get it to build. There are also many other problems in your code, including but not limited to all the C functionality you use. Even if you're on an old compiler which supports only the C++98 standard, you still have C++ file streams and `std::string` for strings. – Some programmer dude Sep 23 '21 at 05:58
  • 1
    As for your problem, I assume you get the error at `v.a[a-1] = "XXX";`? What is that supposed to do? Perhaps you need to take a step back, invest in [some good books](https://stackoverflow.com/a/388282/440558) and start over with learning the basics of C++. – Some programmer dude Sep 23 '21 at 06:01
  • Yeah I get error there. Just wish to simple a push_back but it’s in a struct. I just want to one more element as the last one. – Bebe Sep 23 '21 at 06:12
  • @selbie I corrected to size 11 – Bebe Sep 23 '21 at 06:13
  • How many letters are there in e.g. `ABC`? How much space do you have for it in the array `str`? Where will the null-terminator be written? – Some programmer dude Sep 23 '21 at 06:22

1 Answers1

3

Consider this using more C++ semantics than C:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

struct V {
    char a[10];
};

int main()
{
    std::vector<V> input;
    std::string s;
    int count = 0;
    while (std::cin >> s)
    {
        count++;
        if ((count % 2) == 0)
        {
            V v = {};
            strncpy(v.a, s.c_str(), 4);
            input.push_back(v);
        }
    }

    V endV = {};
    strncpy(endV.a, "XXX", 4);
    input.push_back(endV);

    fstream logfile;
    logfile.open("logfile.txt", std::ios_base::out);
    if (logfile.is_open())
    {
        for (size_t i = 0; i < input.size(); i++)
        {
            logfile << input[i].a << "\n";
        }
        logfile.close();
    }

    return 0;

}
selbie
  • 100,020
  • 15
  • 103
  • 173