View Single Post
Old 09-02-2005, 02:01 PM   #1 (permalink)
wizardman
Registered User
 
wizardman's Avatar
 
Join Date: Sep 2005
Posts: 1
wizardman is on a distinguished road
Send a message via AIM to wizardman
Question Input File Data Into Array in C++?

Hello,
Below is some code I've been working with. The first part saves my test_data array out to a text file. That works fine. The question is how do I retrieve that data back into another array so I can use the that data somewhere else? The includes you'll see first are what I have at the top of my code at this point. The #include "fstream.h" is not commented out for my outputsto work. I'm not sure what I need to make this work. Can someone help me out? If you need any more info please let me know. Thanks in advance.

Code:
#include "fstream.h"
//#include "iostream.h"

//********** OUTPUT DATALOG RESULTS *******************

short i;
float test_data[4];
test_data[0] = 1.23;
test_data[1] = 2.34;
test_data[2] = 3.45;
test_data[3] = 4.56;

//**** OUTPUT TEST_DATA ****
ofstream outfile1("C:\\test.txt",ios::out);
for(i=0;i<4;i++)
{
        outfile1 <<test_data[i];
        outfile1<<endl;
}
outfile1.close();


// NEXT I WANT TO TAKE THE DATA SAVED INTO THE TEST.TXT FILE
// AND INSERT IT INTO THE SAMPLE ARRAY
// THIS IS WHERE I NEED SOME HELP IF POSSIBLE,
// BESIDES THE VARIABLE ARRAY "SAMPLE[4]"
// THE CODE BELOW THAT FLOAT IS WHAT I DON'T THINK WILL WORK.

float sample[4];  // SO WHAT DO I DO NOW?

//**** INPUT TEST_DATA TO SAMPLE ****
ifstream infile1("C:\\test.txt",ios::in);
for(i=0;i<4;i++)
{
        infile1 <<test_data[i];
        infile1<<endl;
}
infile1.close();

Last edited by wizardman; 09-02-2005 at 02:08 PM. Reason: wanted to add [code] tags. didn't know how to do them a first.
wizardman is offline   Reply With Quote