Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
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
Old 09-02-2005, 11:15 PM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Code:
#include <iostream>
#include <fstream>

//--
using std::cin;
using std::ifstream;
using std::ofstream;
using std::cout;
using std::endl;
//--

int main()
{
  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 outfile("test.txt", std::ios::out);
  for(short i=0; i<4; i++)
  {
    outfile <<  test_data[i] << "  ";
    outfile << endl;
  }
  outfile.close();


  //**** INPUT TEST_DATA TO SAMPLE ****

  float sample[4] = {0};  //Always good to intialize arrays.
  short i(0);
  ifstream infile("test.txt", std::ios::in);
  while(infile >> sample[i])
  {
    ++i;
  }
  infile.close();
  
  //Test
  for(short i=0; i < 4; ++i)
  {
    cout<<sample[i]<<endl;
  }

  return 0;
}
There's a lot of info on streams on this forum. Just use the "search" button. There are some pretty nice things to be found.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Text file input to an array letsC Standard C, C++ 9 03-06-2005 09:30 AM
Implementing C++ file structure in C# IAmWeasel MS Technologies ( ASP, VB, C#, .NET ) 1 07-08-2004 07:38 AM
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 08:50 AM


All times are GMT -8. The time now is 08:20 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting