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

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

Question by:killer455
  • 2
LVL 17

Expert Comment

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.

Accepted Solution

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

using namespace std;

#define SIZE 5

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


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

      return 0;

Expert Comment

ID: 9631108
this will print as:


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need some help with listview limit text 20 98
What does std::atomic give me? 7 114
Focus not getting shifted out of  editbox 2 57
I could not build boost code, 10 79
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now