Writing an array to a file.

This is a discussion on Writing an array to a file. within the C++ Programming forums, part of the General Programming Boards category; How would you write a 1000 value array to a file? Right now I am using this: Code: ifstream outFile("file.dat", ...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question Writing an array to a file.

    How would you write a 1000 value array to a file?
    Right now I am using this:
    Code:
    ifstream outFile("file.dat", ios::out);
    while(g < 1000){
    outFile<<votes[g]<<endl;
    g=(g+1); }
    outFile.close();
    Does anyone know of a simpler way to do this?

    Thanks, August

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Does anyone know of a simpler way to do this?
    Dunno about simpler, but shorter.
    Code:
       copy( votes, votes+1000, ostream_iterator< int >(outFile,"\n") );

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Does anyone know of a simpler way to do this?
    First, are you having much success using an ifstream object for output?

    A simpler way would be like this:
    Code:
    int g=0;
    while(g < 1000)
    {
    	outFile<<votes[g++]<<endl;
    }
    However, generally while loops are for looping an indeterminate number of times. For loops are for looping a set number of times:
    Code:
    for(int i = 0; i < 1000; i++)
    {
        
    
    }
    A faster way would be to assemble the data into a string, and then write to the file once. In addition, in your program you are writing to the file with no spaces between the data, so if your array looks like this:

    12, 4, 26, 2, 151

    your output file will look like this:

    124262151

    and I'm wondering how you are ever going to make sense of that data.
    Last edited by 7stud; 04-23-2005 at 06:37 PM.

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Thanks, that is what I wanted to know.

    Quote Originally Posted by 7stud
    and I'm wondering how you are ever going to make sense of that data.
    I would to the same thing but reversed when collecting the data.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Cool-August
    Thanks, that is what I wanted to know.


    I would to the same thing but reversed when collecting the data.
    It won't work. Try it.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >In addition, in your program you are writing to the file with no spaces between the data
    Perhaps you missed the endl.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by swoopy
    >In addition, in your program you are writing to the file with no spaces between the data
    Perhaps you missed the endl.
    Yep.

    Sorry for the bad advice Cool-August.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The endl is bad there, though. It causes a flush and thus a disk access after every single number, which is horribly inefficient. Just a space ' ' or a newline '\n' would be better.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Once I made a math game with a high score for the top player.
    When you viewed the file it looked like this,
    12800august
    There where no newlines of spaces and yet when it read the file it could define the difference bettween the highscore and the player.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. writing data to a file from an array of pointers
    By Mingzhi in forum C++ Programming
    Replies: 1
    Last Post: 07-19-2004, 09:07 AM
  4. writing contents of array to file
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-26-2002, 04:06 PM
  5. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21