Jump to content

  • Log In with Google      Sign In   
  • Create Account

Copy merged data to vector?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 theBegger   Members   -  Reputation: 107

Like
0Likes
Like

Posted 12 May 2014 - 05:26 AM

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

               

Edited by theBegger, 12 May 2014 - 05:58 AM.


Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 13912

Like
2Likes
Like

Posted 12 May 2014 - 05:48 AM

`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.

#3 theBegger   Members   -  Reputation: 107

Like
0Likes
Like

Posted 12 May 2014 - 05:56 AM

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.



#4 theBegger   Members   -  Reputation: 107

Like
0Likes
Like

Posted 12 May 2014 - 06:00 AM

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?



#5 Álvaro   Crossbones+   -  Reputation: 13912

Like
1Likes
Like

Posted 12 May 2014 - 06:51 AM

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...).

#6 Álvaro   Crossbones+   -  Reputation: 13912

Like
2Likes
Like

Posted 12 May 2014 - 07:11 AM

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


#7 theBegger   Members   -  Reputation: 107

Like
0Likes
Like

Posted 12 May 2014 - 07:45 AM

 

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. :)



#8 theBegger   Members   -  Reputation: 107

Like
0Likes
Like

Posted 12 May 2014 - 07:48 AM

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.



#9 Álvaro   Crossbones+   -  Reputation: 13912

Like
2Likes
Like

Posted 12 May 2014 - 08:14 AM

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?



#10 theBegger   Members   -  Reputation: 107

Like
0Likes
Like

Posted 12 May 2014 - 08:27 AM

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 ;(.



#11 theBegger   Members   -  Reputation: 107

Like
0Likes
Like

Posted 12 May 2014 - 08:28 AM

I know I can sort it, but I can't figure out how to send the data from the first two files into an array...that's my big hold up.



#12 Álvaro   Crossbones+   -  Reputation: 13912

Like
1Likes
Like

Posted 12 May 2014 - 08:34 AM

I still don't understand what part of your requirements is not satisfied by my program above.



#13 theBegger   Members   -  Reputation: 107

Like
0Likes
Like

Posted 12 May 2014 - 08:43 AM

I guess I thought that it read all the lines from inputs at once rather than one at a time.



#14 Álvaro   Crossbones+   -  Reputation: 13912

Like
3Likes
Like

Posted 12 May 2014 - 11:09 AM

I guess I thought that it read all the lines from inputs at once rather than one at a time.

 

 

This is the part of my code that reads lines and puts them in the vector:

 

  std::string line;
 
  while (std::getline(ifs, line))
    lines.push_back(line);

 

As you can see, it's a loop that runs once per line.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS