Feb 2, 2011 at 7:59pm UTC
I have a project where I have to set a void function to write a list of five integers with each one separated by a semi-colon, 5;1;3;3;7 for example. But I also have to write another void function that will read ONLY the integers written in the file and place them into an array using a while loop somehow. This is what I got so far:
//Dillon Frakes
//CS-230 D01
//Project 1
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//function declaration
void writeToFile();
void readFromFile(int arr[], int length);
int main(int argc, char *argv[])
{
writeToFile();
int array[] = {5, 1, 3, 3, 7};
const int SIZE = 5;
readFromFile(array, SIZE);
system("PAUSE");
return 0;
}
//function definition or implementation
void writeToFile()
{
//WRITE TO FILE
//declare an ofstream variable
ofstream outData;
//open the file for writing, ios::app for adding to a file without overwriting
outData.open("data.txt");
//write to the file
outData << "5;1;3;3;7";
//close file
outData.close();
}
void readFromFile(int arr[], int length)
{
//READ FILE
//declare an ifstream variable
ifstream inData;
//open test.txt for reading
inData.open("data.txt");
//declare a variable to store the data read from the file
int read;
//prime the read from test.txt
inData >> read;
while(inData)
{
inData.ignore(200,';');
inData >> read;
cout << read << " ";
}
//close the file
inData.close();
}
Not sure where to go from here.
Feb 2, 2011 at 11:10pm UTC
Please edit your post and put the source inside code tags. It will make your post a lot more legible and folks here will be more likely to look at it.