3

i wrote a simple program(C++) that take 20 numbers and sort them into ascending order.Now i want to save the actions in the program in a file like "num.txt" with . Can you explain me what changes should i do?

#include <iostream> #include <iomanip> #include <conio.h> using namespace std; int main() { int x[21], s; int j,i; for (int i = 1; i < 21; i++) { cout << setw(11) << i << ": "; cin >> x[i]; } for (int i = 1; i < 21; i++) { for (int j = i+1; j < 21; j++) { if (x[j] < x[i]) { s = x[j]; x[j] = x[i]; x[i] = s; } } } cout << endl; for (int i = 1; i < 21; i++) { cout << i << ": "; cout << x[i] << "\t"; if (i % 5 == 0) { cout << endl; } } getch(); return 0; } 

i know it's simple but i just started since few days ago and i'm novice.

5 Answers 5

5

To output the numbers to a file you can use std::ofstream for the output stream and replace cout with the variable name you use for the stream.

std::ofstream outfile; outfile.open("num.txt"); for (int i = 1; i < 21; i++) { outfile << i << ": "; outfile << x[i] << "\t"; if (i % 5 == 0) { outfile << std::endl; } } outfile.close(); 

You can also take it a step further and add input validation and use components from the Standard Library to handle most of what you are trying to accomplish. For instance instead of storing the numbers in an array I suggest using std::vector instead. You can also use std::sort to sort the data instead of implementing it yourself.

#include <vector> // vector #include <fstream> // fstream #include <algorithm> // sort #include <iostream> int main() { std::vector<int> numbers; while(numbers.size() != 20) { int value; if(!(std::cin >> value)) { std::cout << "you must enter a number" << std::endl; } else { numbers.push_back(value); } } // Do the sort. Pretty easy huh! std::sort(numbers.begin(), numbers.end()); std::ofstream outfile; outfile.open("num.txt"); if(outfile.is_open() == false) { std::cout << "Unable to open num.txt" << std::endl; } else { for(size_t i = 0; i < numbers.size(); i++) { outfile << i << ": "; outfile << numbers[i] << "\t"; if (i % 5 == 0) { outfile << std::endl; } } outfile.close(); } } 
Sign up to request clarification or add additional context in comments.

3 Comments

Nice, although you might want to keep the output simpler. If numbers in a file is all that is required, then for (int nbr: numbers) { outfile << nbr << std::endl; } is enough (c++11 for-loop)
I considered the range based for loops but felt going more towards components in the Standard Library was going to enough to process ;) Plus the OP seems to want to print 5 numbers per line so some additional processing needed to be done. I did consider adding an overload for operator<< taking a vector on the RHS just for giggles though.
@CaptainObvlious: oops, it was about the 5 per line, but since that was in the original code, is valid (the range based for just saved me some code typing).
4

Use this in your code

#include <fstream> ofstream outputFile; outputFile.open("outputfile.txt"); outputFile << value << endl; outputFile.close(); 

Comments

3

One solution is to use ofstream (in "fstream.h", very similar to std::cout):

ofstream out("num.txt"); if (out.is_open() == false) { cout << "Error! Couldn't open file!" << endl; } else { out << "\n"; for (int i = 1; i < 21; i++) { out << i << ": "; out << x[i] << "\t"; if (i % 5 == 0) { out << "\n"; } } out.close(); } 

3 Comments

He's tagged this C++ and used C++-style I/O for printing to the standard out. So it seems a bit silly to suggest to him C-style I/O.
added C++-style I/O using fstream.
I suggest removing the C style IO altogether. It's a solution yes but can lead the OP down a path they ought not travel.
2

If you're running your program from the command line, type the following(when you run it) to forward the output to a file:

myApp > num.txt 

you can also use the < switch to specify a file to get input from too

You can find more information here.

2 Comments

While the link provided is UNIX specific its worth noting that this approach will work just fine on Windows under cmd.exe and PowerShell.
That's a nice approach but doesn't really lend itself well if there is any output other than the list of numbers. If the OP adds any type of prompt or error reporting it will never be seen since it gets redirected to the output file.
2

here is your code with all the changes now you just need to copy paste (i have made a comment on line i added )

#include <iostream> #include <iomanip> #include <conio.h> #include <fstream>//file stream using namespace std; int main() { int x[21], s; int j,i; for (int i = 1; i < 21; i++) { cout << setw(11) << i << ": "; cin >> x[i]; } for (int i = 1; i < 21; i++) { for (int j = i+1; j < 21; j++) { if (x[j] < x[i]) { s = x[j]; x[j] = x[i]; x[i] = s; } } } ofstream out; //output file stream out.open("num.txt"); //opening (and creating output file named out //cout << endl; for (int i = 1; i < 21; i++) { //cout << i << ": "; out << i << ": "; //printing in output file // cout << x[i] << "\t"; out << x[i] << "\t"; //printing in output file if (i % 5 == 0) { //cout << endl; out << endl; //printing in output file } } //getch(); out.close(); //closing output file return 0; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.