What did I tell you about code tags?
Printable View
What did I tell you about code tags?
Bad idea. http://faq.cprogramming.com/cgi-bin/...&id=1043284351Code:while (!data.eof())
Well, here's what your code does right now.Quote:
in this code i import and export "line" from the files which is not what i'm looking for, i want to import and export individual integers from the files
- Read lines until the end of the input file.
- Write the last line read to the output file.
In effect, it writes the last line of the input file to the output file.
What you need to do is to read integers, not lines. Since you know the format of the input file, you could probably
- read the integer a from the input file;
- read the integer b from the input file;
- calculate the sum of these two numbers, storing the result in c;
- write a, b, and c to the output file.
Does that give you any ideas?
in.txtCode:// Assuming only 26 numbers on 1 line
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(void)
{
ifstream input("in.txt");
ofstream output("out.txt");
string line;
stringstream ss;
double num;
double sum;
char idx;
while ( getline(input, line) )
{
ss << line;
sum = 0;
idx = 'a';
while (ss >> num)
{
output << idx++ << '=' << num << ends;
sum += num;
}
output << idx << '=' << sum << endl;
ss.clear();
}
input.close();
output.close();
return 0;
}
out.txtCode:3 5 7 6 9
4 5 6 3 2 1 3 43 66 34 12 76 34
1 2 3
Code:a=3 b=5 c=7 d=6 e=9 f=30
a=4 b=5 c=6 d=3 e=2 f=1 g=3 h=43 i=66 j=34 k=12 l=76 m=34 n=289
a=1 b=2 c=3 d=6
Yes, well, that's one way to do it. However, your variable names are pretty terse -- it's generally best to use longer names when you're posting code for others to learn from. And really, that's not the easiest code for someone to learn from; it's too complicated. You have to figure that someone who can't read two numbers, add them, and print them isn't going to learn much from something like that.
In addition, if you're going to all of the trouble to use a vector, don't hard-code the size of the vector into the program. That's one nice thing about vectors -- they can expand to hold new data as you see fit.
[edit] On the other hand, you don't need to remember previous numbers at all. See below. [/edit]
Really, it's quite simple. As long as you know there are two numbers:
If you want to sum some arbitrary number of numbers, it's easier to keep a running total. With only one line, something like this would work.Code:int a, b, c;
input >> a >> b;
c = a + b;
output << a << ' ' << b << ' ' << c << '\n';
With multiple lines, it might be necessary to read each line separately and parse that string with stringstreams as knave has shown.Code:int var, sum = 0;
while(input >> var) {
output << var << ' ';
sum += var;
}
output << sum << '\n';
Thank you Knave for your code, its exactly do what I need...:cool:
you're right dwks i didn't understand a lot from the code and there are a lot of new codes that I've to understand before i used it...Quote:
You have to figure that someone who can't read two numbers, add them, and print them isn't going to learn much from something like that.
cheers
Sorry if I sounded insulting, but knave's code really was pretty complicated before [s]he edited it. :)Quote:
you're right dwks i didn't understand a lot from the code and there are a lot of new codes that I've to understand before i used it...
@knave: One last thing: you do know that the std::ends manipulator function prints a NULL, right?
I would think a simple space would be better . . .Code:output << idx++ << '=' << num << ends;
Oh, and how did you syntax highlight that code? ;)