Solved

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

Posted on 2003-10-27
3
219 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

752 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