Solved

Output an Array to a File (one element per line)

Posted on 2003-10-27
3
218 Views
Last Modified: 2010-04-02
I was wondering how do you send an array to an output file, one element per line?

Thanks
0
Comment
Question by:killer455
  • 2
3 Comments
 
LVL 17

Expert Comment

by:rstaveley
ID: 9631062
If you have an array...

   int x[100];

.... you can print the 50th element using...

   std::cout << x[49] << '\n';

Consider using a for loop to make the index variable.
0
 

Accepted Solution

by:
gdean earned 20 total points
ID: 9631105
#include <fstream>

using namespace std;

#define SIZE 5

int main()
{
      ofstream outArray;
      char array[SIZE];

      array[0]='a';
      array[1]='b';
      array[2]='c';
      array[3]='d';
      array[4]='e';

      outArray.open("array.txt");
      

      for(int i=0;i<SIZE;i++)
            outArray << array[i] << endl;
      outArray.close();

      return 0;
}
0
 

Expert Comment

by:gdean
ID: 9631108
this will print as:
a
b
c
d
e

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question