Solved

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

Posted on 2003-10-27
3
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

627 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