Can anyone help me with this...
I have a programing assignment in C++. The job is to read an external file---let's say "jobs.in" and store in an array. This external file contains integers in this format
The first integers of each line will be stored under the same array. The second integers of each line will be stored in the second array then the last in the last array.
Then, finally add all the integers in the arrays.
I will be using the 3 arrays in another purpose.
This is my code for opening the external file but id not know where to add the saving to array code as this is my first time having this as a problem...
//This code demonstrates thow to read from files and how write files.
//The file test.txt must be found in the same directory as this code
//The output.txt will be saved in the same directory as this code
void main(){
ifstream fileReader; // ifstream is used for reading files
ofstream fileWriter; // ofstream is used for writing files
string line;
fileReader.open("jobs.in", ios::in); // open the file to read, myfile.txt is the file to read
if(fileReader.is_open()){ //will only get inside the loop if the opening of the file is successful.
while (!fileReader.eof() ){ //all files has an eof character or end of file character so we know that the end of the file has been reached
getline (fileReader,line); //reads an entire line in the file and stores in in string line.
cout << line << endl; //displays on the console the line read from the file
}
fileReader.close(); //always close the fileReader after use.
}
else cout << "Unable to open file";
fileWriter.open("output.txt", ios::out); //open the file output.txt, if it does not exist, it will be created
int num = 2345678;
fileWriter<<"This is how we write to files, it is just like using cout.\n";
fileWriter<<num<<endl; //this writes the value of num in the file output.txt
fileWriter.close(); //always close the fileWriter after use.
}
Can you have the code by Thursday or earlier.This is just part 1 of my assignment. Thanks
You are reading the data line by line - after you read a line, and before you read the next line, is where you need to split the line into the three elements and add them to your arrays.
yes, i agree with faldrax, create 3 arrays, copy the first line to a 4th temp array, then do a loop for() and start copying to the first array till you find "space" then copy the following to the 2nd array, keep going till you hit the 3 space, and copy the rest to the 3rd array.
int flag=0; // create a flag to count # of spaces.
for(int i=0,j=0,k=0,m=0 ; i<size_array_temp; i++) // do a loop to check the array.
{
if ( (array_temp[i] != ' ' ) && (flag==0) )//copy to the array1 the 1st #
{
array1[j]=array_temp[i];
j++;
}
else if( (array_temp[i] == ' ') && (flag==0) )// if there is a space: flag+1
{
flag++; // increase i to skip the space.
i++;
}
if ( (array_temp[i] != ' ' )&& (flag==1) )//copy to the array2 the 2nd #
{
array2[k]=array_temp[i];
k++;
}
else if (array_temp[i] == ' ') // if there is a space: flag+1
{flag++; // increase pos to skip the space.
i++;
}
if ( (array_temp[i] != ' ') && (flag==2) ) //copy to the array3 the 3rd #
{
array3[m]=array_temp[i];
m++;
}