C++ :: Program To Merge Numbers In Two Files And Write Results To Third File
Apr 9, 2013
I need to write a program that merges the numbers in two files and writes the results to a third file. The program reads input from two different files and writes the output to a third file. Each input file contains a list of integers in ascending order. After the program is executed, the output file contains the numbers from the two input files in one longer list, also in ascending order. Your program should define a function with three arguments: one for each of the two input file streams and one for the output file stream. The input files should be named numbers1.txt and numbers2.txt, and the output file should be named output.txt.
View 1 Replies
ADVERTISEMENT
Sep 22, 2014
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);
[Code] .....
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?
View 13 Replies
View Related
Jul 8, 2013
how do i decompress an input file and write the results into an output file?
also my do while loop is supposed to keep going unless the user selects the option to exit the program, instead it exits after finishing ay of the options.
here's my code so far...
#include <iostream>
#include <iomanip>
#include <fstream>
[Code]....
View 1 Replies
View Related
Sep 16, 2012
I am trying to code a merge sort algoritm, using my knowledge of C++. I have made some code but it is not working. It is giving some random results.
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
void print(int *niz) {
[Code] ....
View 1 Replies
View Related
Apr 3, 2015
I need to write a program that reads four float numbers from the input.txt file, then it prints out the greatest of the four numbers into the output.txt file. I did everything, but the numbers don't print out.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inFile;
ofstream outFile;
float number1, number2, number3, number4;
[Code]...
View 2 Replies
View Related
Sep 7, 2014
-Create three files namely all_numbers, odd_number and even_number.
-Get the number of records and the numbers to store from the user.
-Save all the numbers in "all_numbers" file.
-Read each number and check whether it is odd or even.
-Store the odd numbers in odd_number file and even numbers in even_number file.
-Finally, print the numbers stored in odd_number and even_number files.
The output of the program will look like this.
How many records that you want to store :41
2
3
4
The data are written too the respective files.
The even numbers are 2 4
The odd numbers are 1 3
View 9 Replies
View Related
Nov 23, 2013
I am trying to make a simple program, I would like it to be able to do the following, Write a program that opens two text files (input1.txt and input2.txt) for input .The program should write the followings in the two files. input1.txt:
This is the first line in input1.txt.
This is the second line in input1.txt.
This is the third line in input1.txt.
This is the fourth line in input1.txt.
input2.txt:
This is the first line in input2.txt.
This is the second line in input2.txt.
This is the third line in input2.txt.
What would be the best way to go about doing this? I wrote the program below, but I don't think that it works.
#include <iostream>
#include <fstream>
#include <string>
int main()
{
using namespace std;
string input1;
[Code]...
View 2 Replies
View Related
Jan 5, 2013
I am attempting to merge binary files. However, this is to no avail. The program keeps segfaulting. I want to merge the buffers the files are stored in and then write the new one to disk. Anyway, here is my code.
Main.cpp:
#include "getsize.h"
long lSize;
char * buffer;
size_t result;
FILE * pFile;
FILE * pFile2;
FILE * pFile3;
void read1() {
pFile = fopen ( "uTorrent.exe", "rb");
[Code] ....
View 19 Replies
View Related
Oct 11, 2014
I would like to read in binary files, then write them to another file.
I write a code, what works perfectly, if I would like to just copy the file to another. But I want to make it a little other.
If I open a file in hex-editor I also can see the ASCII values. But I would like get the ONLY the hex values to the other file.
For example:
d5 57 4f ad 30 33 0b 4e 49 a7 05 18 c4 90 66 d8 45 ac 39 3e 7d f1 a8 02 80
14 20 90 6e 20 12 38 0c 65 4a 28 d2 80 72 04 20 a9 4a 82 84 60 6a 0b 25
59 4c 30 c8 69 c0 ec fa 36 ed 3a da b1 9a 82 02 e0 bb 7e 41 87 02 f6 10 34
eb 95 93 63 01 6b 8d e1 d7 43 c3 df 92 5d 8a ed 57 61 4e 36 07 2a d7 56 2b
b5 0e 55 83 b4 76 8c b7 61 77 0e c9 76 0c 81 1b 01 63 0c 8b 73 57 d5 6d 4c
0c c2 0d 52 45 18
How could I make it?
View 4 Replies
View Related
Oct 7, 2013
I have to merge two sorted files. Algotrithm that i'm using is below, but it reads not all the numbers in the files, and stops, even the files contain the same number of elements. What is wrong, I can't understand.
ifstream f1("E:desc1.txt");
ifstream f2("E:desc2.txt");
ofstream f("E:desc.txt");
cout <<endl<< "Nuerele sortate: " <<endl;
int n1, n2;
f1 >> n1;
f2 >> n2;
[code]...
desc1.txt is:
99 97 95 93 91 89
and desc2.txt is:
100 98 96 94 92 90
and I'm getting this:
100 99 98 97 96 95 94 93 92
View 1 Replies
View Related
May 11, 2014
I'm trying to finish this project which is supposed to read two files, and merge them alphabetically. I've written this code, and it compiles. But it isn't actually doing anything??The console opens and closes immediately upon running.
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
#define hisFamily "The Adopted.txt" //His family
#define herFamily "The Originals.txt" //Her family
#define ourFamily "The Big Picture.txt" //Our family
[Code] ....
View 7 Replies
View Related
Jan 18, 2015
I am trying to write a program that adds numbers to an array. The array capacity starts out at 2, then doubles whenever the size reaches capacity. The program works perfectly fine when the array needs to be expanded. So I have narrowed down the issues into this one function (_dynArrSetCapacity)
Creates original array
DynArr* createDynArr(int cap) {
assert(cap > 0);
DynArr *r = (DynArr *)malloc(sizeof( DynArr));
assert(r != 0);
initDynArr(r,cap);
[Code]...
If i do createDynArr(10) for instance in the main, the program works perfectly, as it never gets expanded
View 3 Replies
View Related
Aug 5, 2014
user enter four number and the output will be the greatestt number among these
View 1 Replies
View Related
Sep 24, 2014
Write a program which stores 10 decimal numbers in an array. For these numbers accept input from the users. Once the array is populated do the followings:
Display each elements of the array
Display the sum of all the elements of array
View 4 Replies
View Related
Oct 21, 2014
I have to write a program where the user will input integer numbers. How many numbers they enter is unknown, therefor you should use a repetition structure for the input. When the user is done, they will enter -1 to exit.
Create a dynamic array if the size=2( the initial size must be 2) Repeat until user enters -1.
I have to do this without using vectors.
This is what i have, I cannot figure out what to put in main. I was thinking of a do-while?
Code:
#include <iostream>
using namespace std;
void resize(int *[], int);
int main() {
int *listDyn;
int size=2;
[code].....
View 5 Replies
View Related
Dec 11, 2013
im supposed to create a program that reads in a list of integers from the terminal and writes the negative numbers to one file and the positive numbers to another file.
i got most of it doen but for some reason its not writting the negative numbers. on what im doing wrong?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
int pos_num = 0;
int neg_num = 0;
int positive_numbers = pos_num % 5;
[Code]...
View 2 Replies
View Related
Apr 10, 2013
Whenever I try to call merge sort on large numbers say n=10000000. It gives an error. It works fine for small numbers, even though I have declared my Lists on the heap.
The below code is just the divde part.
List<long>* Merge(List<long> *ParentList) {
if((ParentList)->Length==1) {
return ParentList;
[Code] .....
View 1 Replies
View Related
Aug 29, 2014
One of my class assignments is to create a program that receive a .txt file containing a students name and their grades as follows:
John K. 99, 87, 57, 89, 90, 95
Amanda B. Jones 100, 88, 76, 99, 86, 92
etc..
The number of students is unknown until run time. You have to take those grades and average them weighing the first (4) at 10% a piece and the last (2) at 30% each.
Then return an output file with the students name and their letter grade A,B,C,D,F based on their computed score. In addition, on screen it needs to display the average scores for each Q1, Q2, etc. as well as the minimum and maximum for each test on the screen. I am having a hard time in assigning the scores to a variable so that they can then be computed as an average and then used to determine a letter grade. I have begun to write the code and am a bit stuck..here's what I have so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
[Code]....
View 2 Replies
View Related
May 14, 2013
The Program must allow users to enter any two positive numbers and then display the results.
#include <iostream>
using namespace std;
class ArithmeticOperators{
private:
int x;
int y;
public:
void set_values();
[Code] .....
View 2 Replies
View Related
Aug 31, 2014
We have to write a program that reads an input file containing data in the form:
Martha J, 80, 90, 90, 100, 89, 99, 85
Anna Smith, 65, 72, 77, 68, 62, 70, 65
Bill Gates, 60, 54, 89, 62, 65, 60, 50
...etc...
and then use that information to determine their letter grade and write that information to a .txt file. I have been able to successfully pass that information to a function to determine their letter grade and have been able to successfully compute their letter grade. However, I can get this information to successfully post within the *computeGrade () function. Yet, I don't know how to pass this information back to the main() on a student by student basis so that I can open a new .txt file and write just their name and letter grade to it.
Another issue I'm having is when it comes to computing the averages of the class. Each score is either a quiz (there are 4), a midterm (there are 2) or a final (there is 1). I'm a little stuck on how to pass say all of the quiz 1 grades as one entity to the function averagesminmax(). Is there a way to compile each of the grades for a specific quiz or midterm as one array and pass that to the function to then do the computation. Also we need the min and max so again from compiling the grades of a particular quiz or exam together as one.
Here's the code that I have so far.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
const char *computeGrade();
int averagesminmax();
int main() {
char fName[20];
char lName[20];
[code]....
Do I need to re-read the input file and assign them to different variables?
Here's a sample of the output so far:
Enter input .txt file name
Graded
Thui Bhu, 91, A
Ariana B. Smith, 96, A
Emily Gonzales, 83, B
Jennifer L, 89, B
Maria Jones, 67, D
Bill Gates, 55, F
Escobar Morris, 78, C
Anne Latner, 88, B
Program ended with exit code: 0
Also in my output file, the only thing that it prints will be the first name of whatever the last student in the input file list is.
View 2 Replies
View Related
Feb 11, 2013
Here I was trying to write a program to copy one file to another.
Code:
#include <stdio.h>#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp , *ft;
[Code]....
So it should copy test1 to test2. Now I created test1 in gedit. I just put one character "c" in it and closed it. And I ran the program. When I check sizes of test1 and test2 , using
ls -l
command, I see that test2 has 4 bytes and test1 has 2 bytes. So something strange is happening here. I just put a single character in test1, so shouldn't it show 1 byte ? And why test2 is showing 4 bytes ? I have Win XP on my laptop. And I use Ubuntu 12 inside VirtualBox. So this is Linux environment...
View 10 Replies
View Related
Feb 26, 2013
Write a program that reads data from a file (use the attached data file). These data are a student name and 3 test scores. The program should calculate the average of the 3 test scores, and display the name, 3 test scores, and the average to the monitor.
Useful tips:
a) Include the following header files: iostream, fstream, iomanip, and string
b) The name of the data file is “datafile.txt”, you need to save the file in the same folder of the source file.
c) use the manipulators (setw, setprecision, setfill, showpoint, fixed) to format the average with 1 digits after decimal point as following.
d) Use character ‘ ’ for tab.
View 3 Replies
View Related
Jan 28, 2013
So I'm trying to create a program that allows one to read/write on to output.txt. I though I had everything set up right, but its only writing one word to the text file. Heres the code.
#include "stdafx.h"
#include <iostream> //Needed for User Input
#include <fstream> //needed for ofstream
#include <string> // needed for strings
#include <windows.h> //needed for Sleep
#include <cstdlib> //Needed for return EXIT_SUCCESS;
using namespace std;
int main() {
ofstream outputfile; //allows to read and write to files
[Code] .....
If I type Puppies Are Cute and go to output.txt, the only thing written in the text file is Puppies.
View 9 Replies
View Related
Apr 18, 2014
I'm trying to write a program where it asks the user for a file name.
If that file exists, the it will open the file and then read it and print the information in the file.
However, if the user enters a file name that does not exist, the user will have 2 more tries (for a total of 3 tries) to get the correct name of the file.
I am close to getting it. What I have now just won't go through the loop to validate if the input matches an existing file. It goes straight to the printing of what is in the file. If the file doesn't exist, it prints: "Found your file..Opening..processing...Total keys found = 0."
So, my question, what am I doing wrong that it won't go through the loop properly?
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
using namespace std;
int main() {
bool fileFound = false;
[code]....
View 2 Replies
View Related
Apr 24, 2014
I have a lab with the following instructions:
Manually create an input text file called Lab22BInput.txt with the following data:
Dove
Doe
Alston
Zebra
Egg
Write a program that will read the names from the input file. Print out each of the names read to the screen. The program should go through the list of names and determine which comes 1st in the alphabet. The program should determine how many letters are in this name. Print out the name that comes first in the alphabet and the length of the name.
I have created the input file, and the data correctly outputs to the screen when I run the program. However, I do not know what kind of logic to use to sort them alphabetically and determine the amounts of characters.
So far I have the following:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string str1, str2, str3, str4, str5;
[Code]...
View 2 Replies
View Related
Mar 7, 2013
I am trying to write a program for class that reads from an input file the month, year & total collected. I am using notepad for the input file.
This is what the notepad file looks like
-----------------------------------
March 2013 63924.62
Why does it give me random numbers in the output rather than what the notepad has?
Also, the outfile is completely blank.
#include <iostream>
#include <iomanip>
#include <fstream>
[Code].....
View 2 Replies
View Related