Copy merged data to vector?

Started by
12 comments, last by alvaro 9 years, 11 months ago

I'm having trouble figuring out how to copy the data from the two txt files into a vector. I'm trying to do this so I can use vectors sort function for alphabetizing. With the way I've tried to write the code, I keep getting

77 request for member `SIZE1' in `insA', which is of non-class type `int[((unsigned int)((int)SIZE1))]'

Thanks


#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string> 
using namespace std;

#define theAdopted "The Adopted.txt"            //His family
#define theOriginals "The Originals.txt"        //Her family
#define theBigPicture "The Big Picture.txt"     //Our family

//Function Prototypes

class KeepRunning {                            //Prototype needed to keep console from closing.
  public:
    ~KeepRunning() {
      cin.get();}};

//Copies text
int copyLine(ifstream&, ifstream&, ofstream&);

int main()                                                                  
{   
    KeepRunning kr;  
     
    ifstream hisFamily("The Adopted.txt");
    ifstream herFamily("The Originals.txt");
    ofstream ourFamily("The Big Picture.txt");
    
    int SIZE1 = 200, SIZE2 = 200;  
    int insA[SIZE1]; 
    int insB[SIZE2];
    int outs[SIZE1 + SIZE2];
    int lineCountA = 0;
    int lineCountB = 0;
    string lineA;
    string lineB;
    
    //Retreive his family's grades.
    hisFamily;
    if(hisFamily.fail())
    {
        cerr << "ERROR: Cannot open " << theAdopted << ". \n";
        return EXIT_FAILURE;
    } 
    //Retreive her family's grades.
    herFamily;
    if(herFamily.fail())
    {
        cerr << "ERROR: Cannot open " << theOriginals << ". \n";
        return EXIT_FAILURE;
    }
    //Call theBigPicture.
    ourFamily;
    if(ourFamily.fail())
    {
        cerr << "ERROR: Cannot open " << theBigPicture << ". \n";
        return EXIT_FAILURE;
    }        
    //Copy data hisFamily to ourFamily.
    getline(hisFamily, lineA);
    getline(herFamily, lineB);
    while(lineA.length() != 0 && lineB.length() != 0)
    {
        lineCountA++;
        lineCountB++;   
        ourFamily << lineA << endl;
        ourFamily << lineB << endl;      
        getline(hisFamily,lineA); 
        getline(herFamily,lineB);
    } 
    
    cout << "Input data mergered to file 'The Big Picture.exe'." << endl;
    //Close files.
    hisFamily.close();
    herFamily.close();
    ourFamily.close();
    return 0;
}  
int copyLine
    (ifstream& hisFamily,
     ifstream& herFamily,
     ofstream& ourFamily)
     {
         const char NWLN = '\n';
         char nextCh;
         int charCount = 0;
         
         //Merge
         hisFamily.get(nextCh);
         while ((nextCh != NWLN) && !hisFamily.eof())
         {
             ourFamily.put(nextCh);
             charCount++;
             hisFamily.ignore(nextCh);
             hisFamily.get (nextCh);
         }
         if(!hisFamily.eof())
         {
             ourFamily.put(NWLN);
             charCount++;
         }
         return charCount;
     }

               
Advertisement
`insA.SIZE1' doesn't mean anything. That's what the compiler is saying, and I agree. Which constructor of std::vector<char> are you trying to use in that line?

The fact that your code is full of commented-out lines and seems half baked doesn't help me understand what you are trying to do, so I can't be any more helpful than the compiler.

Sorry, the commented out lines are my failed attempt at vector. I just didn't want to get rid of them yet...hanging onto the string of faith thinking I'll figure this out :/.

I'm really not sure which constructor I even should use...I've never dealt with vectors before, and we haven't studied them yet.

The code works well, I'm just trying to use the vector class to alphabetize the data on the output .txt

I just edited it so that you can see the code vector-less...aside from the header.

This code lists the data contained in the two input.txt on the single output.txt, but it isn't alphabetized. I thought if I could copy the input data to a vector first, I could use sort, then copy the data to the output.txt.

Is that a bad idea?

Sorry, the commented out lines are my failed attempt at vector. I just didn't want to get rid of them yet...hanging onto the string of faith thinking I'll figure this out :/.
I'm really not sure which constructor I even should use...I've never dealt with vectors before, and we haven't studied them yet.


Let me ask the question in another way: What did you want that line to do?

So you just have two text files and you want to combine their lines into an output file, but you want to use a vector to write the output sorted. Is that right? Do the files have the same number of lines? (implied by the way you wrote the loop...).
I would write it something like this:

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

void report_error(std::string message) {
  std::cerr << "ERROR: " << message << '\n';
  std::exit(EXIT_FAILURE);
}

void read_file_into_vector(std::string const &filename, std::vector<std::string> &lines) {
  std::ifstream ifs(filename.c_str());
  
  if (ifs.fail())
    report_error("Cannot open file \"" + filename + "\" for reading.");
  
  std::string line;
  
  while (std::getline(ifs, line))
    lines.push_back(line);
}

void write_file_from_vector(std::string const &filename, std::vector<std::string> const &lines) {
  std::ofstream ofs(filename.c_str());
  
  if (ofs.fail())
    report_error("Cannot open file \"" + filename + "\" for writing.");
  
  /* In C++11 you can just do this:
  for (auto &line : lines)
    ofs << line << '\n';
  */
  
  for (std::vector<std::string>::const_iterator it = lines.begin(), end = lines.end(); it != end; ++it)
    ofs << *it << '\n';
}

int main() {   
  std::vector<std::string> all_lines;
  
  read_file_into_vector("The Adopted.txt", all_lines);
  read_file_into_vector("The Originals.txt", all_lines);
  
  std::sort(all_lines.begin(), all_lines.end());
  
  write_file_from_vector("The Big Picture.txt", all_lines);
}

Sorry, the commented out lines are my failed attempt at vector. I just didn't want to get rid of them yet...hanging onto the string of faith thinking I'll figure this out :/.
I'm really not sure which constructor I even should use...I've never dealt with vectors before, and we haven't studied them yet.


Let me ask the question in another way: What did you want that line to do?

So you just have two text files and you want to combine their lines into an output file, but you want to use a vector to write the output sorted. Is that right? Do the files have the same number of lines? (implied by the way you wrote the loop...).

Yes, exactly. They do have equal number of lines. Thanks a million for the example. I'm gonna fool around with this. I find it about a million times easier to learn this language when I'm given an example which is relevant to my problems, because then I see for myself how things work rather than reading in a reference. :)

I can see your code is much more efficient. But, I have to call each line one at a time. That's why I was having such a hard time converting to vector because there are certain elements I simply have to keep.

I can see your code is much more efficient. But, I have to call each line one at a time. That's why I was having such a hard time converting to vector because there are certain elements I simply have to keep.

Sorry, that didn't make any sense. Can you please fully describe what you are trying to do?

Haha yeah. I'm sorry I have literally been awake for two days because of finals and am just trying to finish this project. My brain isn't working to well at this point.

I need to merge two files [done],

read each line of each data file (can't use any fancypants shortcuts built into a library to have the program read the entire file)[done],

Sort,

print to output file.

I wanted to just do some sort of while loop which would read the data from a file into an array within the loop, but I'm not allowed ;(.

This topic is closed to new replies.

Advertisement