Solved

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

Posted on 2003-10-27
3
217 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SetCurrentDirectory path limit 7 121
c++ getting the first 10 characters of a char* string 11 94
Add values of each row in an array 3 57
Path to  STL Map header file 1 58
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

777 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