Solved

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

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

747 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

12 Experts available now in Live!

Get 1:1 Help Now