13 Replies - 1074 Views - Last Post: 23 September 2014 - 07:59 AM Rate Topic: -----

#1 HiTechRedneck3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Writing a C++ program that can merge files from the command line in Li

Posted 22 September 2014 - 07:58 PM

I have written a C++ program that is supposed to open two text files (prog2a.dat and prog2b.dat) and write the contents of a specified range of lines to an output file (outfile.dat). I wrote a program that based on the example we were given (to take lines 5-15 from the first file and lines 4-12 of the second file and merge them into the output file) works perfectly. However, after asking my professor for some clarification on another part of the assignment, I found out I have not done this correctly. I have the code written so that it will always output the range of lines I mentioned earlier, but the program is actually supposed to allow the user to merge the files from the command line using whatever range they want by typing in the following command:

 prog2 in1 5-15 in2 4-12 outfile 


But I'm not sure how to tweak my current program to allow this to be done.

Here is the code that I have written, keep in mind that this works properly for the way it is written, but not how it is supposed to work for the purposes of the command line (hopefully that makes sense):

  #include <iostream>
    #include <fstream>
    #include <cstdlib>
    using namespace std;
    
    int main() {
    	// Create output file
    	std::ofstream outFile("outfile.dat", ios::out);
    	
    	// Open input file 1, if can't be opened, exit
    	ifstream in1;
    	in1.open("prog2a.dat");
    	std::string line;
    	int count = 1;
    		if(!in1) {
    			cerr << "Open Failure" << endl;
    			exit(1);
    		} //end if
    		else {
    			while(std::getline(in1, line)) {
    				if(count >= 5 && count <= 15) {
    					outFile << line << "\n"; /*writes the contents of
    					lines 5-15 to outfile.dat*/
    				}
    			++count;
    			} //end while
    		} //end else 
    	in1.close(); //close in1 (prog2a.dat)
    	outFile << "\n"; //add a blank line after the output from prog2a.dat
    	count = 1; //reset the line count to 1 before opening next file.
    
    	//Open input file 2, if can't be opened, exit
    	ifstream in2;
    	in2.open("prog2b.dat");
    		if(!in2) {
    			cerr << "Open Failure" << endl;
    			exit(1);
    		} //end if
    		else {
    			while(std::getline(in2, line)) {
    				if(count >= 4 && count <= 12) {
    					outFile << line << "\n"; /*writes the contents of the
    					lines 4-12 to outfile*/
    				}
    			++count;
    			} //end while
    		} //end else
    	in2.close(); //close in2 (prog2b.dat)
    } //end main 


Is there any simple way to make this work as I described using the command line? Also, I am supposed to break this up into three files, a header file, the program file, and a test file (the test file contains main() and should close the 3 open files and display any error messages), but I'm getting really confused as to what should go in the header file. I know the header file should contain class definitions and constructors, but don't really know how to make that work for this specific program? I'm extremely new to this so any pointers would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Writing a C++ program that can merge files from the command line in Li

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 5332
  • View blog
  • Posts: 16,601
  • Joined: 25-December 09

Re: Writing a C++ program that can merge files from the command line in Li

Posted 22 September 2014 - 08:21 PM

Quote

But I'm not sure how to tweak my current program to allow this to be done.

I would put your current program to the side and start a new program. The first thing you will probably need to figure out is how to pass parameters from the command line. Then once you figure out this issue it should be easier to incorporate this into your assignment. And you should also be considering functions to separate the different parts of your program.



Jim
Was This Post Helpful? 0
  • +
  • -

#3 HiTechRedneck3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Re: Writing a C++ program that can merge files from the command line in Li

Posted 22 September 2014 - 08:27 PM

I was afraid you'd say that haha. This program is due tomorrow along with another one that has me stumped as well, so was really hoping there was a quick and easy fix to keep me from having to rewrite the whole thing. I'm currently working on figuring out functions and how they could be incorporated into this. I think once I get that figured out dividing the program into three files should be fairly simple (I hope).
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 5332
  • View blog
  • Posts: 16,601
  • Joined: 25-December 09

Re: Writing a C++ program that can merge files from the command line in Li

Posted 22 September 2014 - 08:38 PM

Just remember functions don't have to be in their own files. You can have multiple functions in one file.

Quote

as really hoping there was a quick and easy fix to keep me from having to rewrite the whole thing.

You may not need to re-write the whole thing. But by starting a new program to help you learn to work with the command line arguments will probably be faster than trying to learn to work with command line arguments working with your current program. I find it's easier to learn something if I concentrate on that one issue, not muck with this new topic in a more complicated setting. Follow the KISS (Keep It Simple S_____) principle.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 HiTechRedneck3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Re: Writing a C++ program that can merge files from the command line in Li

Posted 22 September 2014 - 08:46 PM

View Postjimblumberg, on 22 September 2014 - 08:38 PM, said:

Just remember functions don't have to be in their own files. You can have multiple functions in one file.


Right, I just mean that once I figure out what functions I need it should be easier to see what should go in the header file, program file, and test file than it is now when I don't have any functions.

Quote

I find it's easier to learn something if I concentrate on that one issue, not muck with this new topic in a more complicated setting.


Good point.

Also, thanks for that link, I'm finding it extremely helpful.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon

Reputation: 5332
  • View blog
  • Posts: 16,601
  • Joined: 25-December 09

Re: Writing a C++ program that can merge files from the command line in Li

Posted 22 September 2014 - 08:49 PM

Quote

Right, I just mean that once I figure out what functions I need it should be easier to see what should go in the header file, program file, and test file than it is now when I don't have any functions.


For a program like this you could do everything in one file, to make things much easier. Once you get over this hump you can then start working on figuring out how to use multiple files.
#include <iostream>

using namespace std;

// Function prototypes.
int someFunction(int someParameter);

int main(int argc, char **argv)
{
   int someValue = someFunction(argc);

   cout << someValue << endl;

   return 0;
}

int someFunction(int someParameter)
{
   return someParameter;
}



Jim
Was This Post Helpful? 0
  • +
  • -

#7 HiTechRedneck3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Re: Writing a C++ program that can merge files from the command line in Li

Posted 22 September 2014 - 08:59 PM

[quote name='jimblumberg' date='22 September 2014 - 08:49 PM' timestamp='1411444171' post='2054813']

Quote

For a program like this you could do everything in one file, to make things much easier. Once you get over this hump you can then start working on figuring out how to use multiple files.


I have to use multiple files as part of the assignment, although I agree, with this type of program it seems pretty unnecessary.
Was This Post Helpful? 0
  • +
  • -

#8 HiTechRedneck3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Re: Writing a C++ program that can merge files from the command line in Li

Posted 22 September 2014 - 09:18 PM

I'm still having trouble figuring out how to make this work, more specifically, since **argv would be the range of lines (if I'm understanding this properly), how to tell the program when to start and stop writing the contents of those lines to the outfile. In the code I have included on this post I do it with an if statement

 if(count >= 5 && count <= 15) {
outFile << line << "\n";
}
++ count; 


Not sure how this would done using the parameters passed in from the command line? I'm assuming I'd still need the count variable to keep track of what line the program is reading in the file.
Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1852
  • View blog
  • Posts: 6,658
  • Joined: 19-February 09

Re: Writing a C++ program that can merge files from the command line in Li

Posted 22 September 2014 - 10:07 PM

Hi, all the command line arguments will be C type strings.

So a range eg 5-15 will be in one string, and will need converting to two numbers. A stringstream is one way of converting.

The range values will vary so will be held in variables.
Was This Post Helpful? 0
  • +
  • -

#10 HiTechRedneck3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Re: Writing a C++ program that can merge files from the command line in Li

Posted 22 September 2014 - 10:14 PM

Do you happen to have a link that I could look at for how the stringstream would work in this instance? I've never even heard of it before so have no idea how it'd be used here.
Was This Post Helpful? 0
  • +
  • -

#11 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1852
  • View blog
  • Posts: 6,658
  • Joined: 19-February 09

Re: Writing a C++ program that can merge files from the command line in Li

Posted 22 September 2014 - 10:31 PM

A stringstream is similar to other streams eg cin, cout and fstreams.

stringstream

You should be able to input data into the stringstream like so :

std::stringstream ss(mystr);



Also there is a C type function for converting from a strings :

sscanf
Was This Post Helpful? 0
  • +
  • -

#12 HiTechRedneck3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Re: Writing a C++ program that can merge files from the command line in Li

Posted 22 September 2014 - 11:00 PM

So since I'm getting the input from the command line would it be something like this?

 
std::stringstream ss(**argv) 

Was This Post Helpful? 0
  • +
  • -

#13 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1852
  • View blog
  • Posts: 6,658
  • Joined: 19-February 09

Re: Writing a C++ program that can merge files from the command line in Li

Posted 23 September 2014 - 07:40 AM

The command line data argv is a array of C strings, so that wouldn't work. My thinking was along the lines of passing one command line argument at a time to the stringstream.

std::stringstream ss(argv[2]);

int low, high;

ss >> low >> '-' >> high;


Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is offline

  • member icon

Reputation: 5332
  • View blog
  • Posts: 16,601
  • Joined: 25-December 09

Re: Writing a C++ program that can merge files from the command line in Li

Posted 23 September 2014 - 07:59 AM

Remember your command line arguments should look like the following:

Quote

prog2 in1 5-15 in2 4-12 outfile

Where argv[0] (prog2) is your program. argv[1] (in1) is the name of the input file 1, "numbers1.txt" for example, next argv[2] are the beginning and ending line numbers in that file. argv[3] in2 is the second file, etc. outfile is the output file name. So to parse the arguments you start with argv[1], the name of the input file number 1. Next are the line numbers, which you can use a stringstream to parse.
int start_stop[2][2]; // A variable to hold the beginning and ending lines.
char delimiter;
string lines(argv[2]); // First copy the C-string to a string.

istringstream sin(lines); // Create a stringstream from the string.

sin >> start_stop[0][0]; // Get the starting line in file 1.
sin >> delimiter;  // Retrieve the delimiter (the '-' char).
sin >> start_stop[0][1]; // Get the ending line in file 1.



And repeat the above for the second file (argv[4])
lines = argv[2]; // First copy the C-string to a string.

sin.str(lines); // reset the stringstream for the next file.

sin >> start_stop[1][0] >> delimiter >> start_stop[1][1]; 



Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1