I am in need of help writing some code to open a file take two values, then add them together and output them to another file. I have very little programming experience and am looking for any help.
// copy from one file to another
#include <iostream>
#include <fstream>
usingnamespace std;
int main() {
int a, b, c;
ifstream inFile("intinput.txt", ios::binary); //opens intinput file
ofstream outFile("results.txt", ios::binary); //creates output file
c = a + b;
while (inFile.good()) // Though perhaps this condition is wrong
{
outFile.put(inFile.get());
}
inFile.close();
outFile.close();
}
// copy from one file to another
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main() {
int a, b, c;
string line;
ifstream inFile;
inFile.open("intinput.txt");
ofstream outFile;
outFile.open("results.txt");
if (inFile.is_open());
{
while (getline(inFile, line));
{
cout << line << "results.txt";
}
inFile >> a >> b;
c = a + b;
outFile << c;
inFile.close();
outFile.close();
}
}
Okay so I looked at what you said and this is what I have come up with
> take two values, then add them together and output them
¿can you do that? just that, and use the standard input and output streams (e.g. std::cin, std::cout)
Later you could simply do stream redirection
$ ./program.bin < input.txt > output.txt
or a textual replace in your program 's/std::cin/input/g'
So I have been working on this and have gotten to the point where I can add the variables and output them to a different file, but I don't know the code to go tot he next line. This is what I have so far.
To get to the next line when writing to the file, just send the file "\n" when you want subsequent data on the next line. To get the next line when reading a file in, use getline again. If you have many lines, you could use it within a loop.
I wouldn't necessarily suggest using '\n' when outputting. It can vary based on operating systems. Windows uses "\r\n" (CRLF) , UNIX uses '\n' (LF) , Mac before version 9 uses '\r' (CR) , and later mac uses '\n' (LF) there is no universal newline.
LF: Multics, Unix and Unix-like systems (GNU/Linux, OS X, FreeBSD, AIX, Xenix, etc.), BeOS, Amiga, RISC OS and others.
CR+LF: Microsoft Windows, DEC TOPS-10, RT-11 and most other early non-Unix and non-IBM OSes, CP/M, MP/M, DOS (MS-DOS, PC DOS, etc.), Atari TOS, OS/2, Symbian OS, Palm OS, Amstrad CPC
LF+CR: Acorn BBC and RISC OS spooled text output.
CR: Commodore 8-bit machines, Acorn BBC, ZX Spectrum, TRS-80, Apple II family, Mac OS up to version 9 and OS-9
RS: QNX pre-POSIX implementation.
I'm not 100% positive if endl is portable but you could write a macro or use the correct one according to the OP's OS.
Just to clarify, so you are trying to read line by line, and add whatever numbers are contained in a line.
So if the input file would be:
10 20 3
2
9 9 9 9
Then the output file would be:
33
2
36
If this is the kind of behavior you want to achieve, all you need to do is read line by line by using std::getline() and build a string stream from which you extract the numbers.
You already got very close. So I'll give you incomplete code, because it's enough.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <sstream>
#include <string>
// ...
std::string line;
while (std::getline(indata, line))
{
std::stringstream numbers(line);
int x; // a, b, c, d, ...
int sum = 0;
while (numbers >> x)
sum += x;
outfile << sum << '\n';
}
@ giblit: if the file is not opened in binary mode, the newline gets translated appropriately.