Hi guys and girls, im currently struggling trying to work out all this array and file input stuff!

I have been going round in circles for the past few hours, reading books and googling everything i could, so now im hoping some of your intelligence will help me :)

Basically i have a text file, containing names each with 12 numbers underneath them:

Bob Smith

12 17 84 93 83 48 93 47 95 28 84 26

as an example :P

I need to take the first 6 numbers and put them into an array, and put the last 6 into a different array.

I'm struggling badly, here's what i have done:

#include <iostream.h>
#include <string>
#include <fstream>
using namespace std;



bool getInputFilename(char fname[])
{
	ifstream fin;

	cout << "Please enter the filename for input : ";
	cin >> fname;
	
	fin.open(fname, ios::nocreate);
	
	if (!fin.is_open())
		return false;
	
	fin.close();
	return true;
}


bool getOutputFilename(char fname[])
{
	ofstream fout;

	cout << "Please enter the filename for output : ";
	cin >> fname;

	fout.open(fname);

	if (fout.fail())
		return false;

	fout.close();
	return true;

}

int firstArray(int i, int array[6], int maxSize, int amountRead)
{
	ifstream fin;
	ofstream fout;

	while(fin>>array[amountRead]&& amountRead < maxSize)
	{
		amountRead++;
	}

	for (i=0; i < 5; i++)
	{
		fout << array[i] << endl;
	}

	cin.get();

	return 0;
}



void main()
{
	ifstream fin;
	ofstream fout;


	char IFname[20], OFname[20];
	



	while (!getInputFilename(IFname))
	{
		cout << "Invalid filename try again!\n\n";
	}

	while (!getOutputFilename(OFname))
	{
		cout << "Invalid filename try again!\n\n";
	}

	fout.open(OFname);
	fin.open(IFname);



}

The attempt at an array does nothing and was the result of trial and error from all the reading i have been doing, but now im on the verge of throwing my monitor out of the window.

Any help very much appreciated, apologies for any spelling mistakes!

it does nothing because the function is never called anywhere. And why not use std::string for the file names instead of C style character arrays? If you are writing c++ then use c++ as much as possible. And you should use getline() for entering filenames so that the path and file names can contain spaces.

Well i had a play about and changed a few things to no avail. As for the C style character arrays because it was how i was taught...

#include <iostream.h>
#include <string>
#include <fstream>
using namespace std;



bool getInputFilename(char fname[])
{
	ifstream fin;

	cout << "Please enter the filename for input : ";
	cin >> fname;
	
	fin.open(fname, ios::nocreate);
	
	if (!fin.is_open())
		return false;
	
	fin.close();
	return true;
}


bool getOutputFilename(char fname[])
{
	ofstream fout;

	cout << "Please enter the filename for output : ";
	cin >> fname;

	fout.open(fname);

	if (fout.fail())
		return false;

	fout.close();
	return true;

}

void firstArray(char name[], int marks [])
{
	ifstream fin;
	ofstream fout;

	while (!fin.eof()) {


  	for (int i = 0; i < 12; i++) {
  		fin >> marks[i];
  					
	}
	
	}
}






void main()
{
	ifstream fin;
	ofstream fout;

	char IFname[20], OFname[20];
	int marks [12];
	char name[40];

	firstArray(name, marks);
	


	while (!getInputFilename(IFname))
	{
		cout << "Invalid filename try again!\n\n";
	}

	while (!getOutputFilename(OFname))
	{
		cout << "Invalid filename try again!\n\n";
	}

	fout.open(OFname);
	fin.open(IFname);

	fout << marks << endl;


	fout.close();
	fin.close();

}

Output file ends up giving me some random numbers (0012FE00)

In function firstArray() you declared an input stream and an output stream. The output stream is never used, so you might as well delete it. The input stream is never opened, so the loop will always fail.

main() is already opening the input and output streams, so why not just pass the name of the input stream as another parameter to firstArray() function and delete both those stream objects you declared inside that funtion. You will also have to change the code shown in blue below.

void firstArray(ifstream& in, int marks [], int arraySize)
{
...

Then in main(), move that function call to firstArray down after the files are opened, and pass the parameters as shown above.

void main()
{
	ifstream fin;
	ofstream fout;

	char IFname[20], OFname[20];
	int marks [12];
	char name[40];

	


	while (!getInputFilename(IFname))
	{
		cout << "Invalid filename try again!\n\n";
	}

	while (!getOutputFilename(OFname))
	{
		cout << "Invalid filename try again!\n\n";
	}

	fout.open(OFname);
	fin.open(IFname);

	firstArray(fin,marks,12);

//	fout << marks << endl;

     for(int i = 0; i < 12; i++)
          fout << marks[i] << endl;
    

	fout.close();
	fin.close();

}

After opening the files you need to make a sanity check to see if the files were opened ok. Something like this

fin.open(IFname);
   if(!fin.is_open())
  {
     cout << "cannot open file " << IFname << endl;
     return 1;
  }

Thanks very much mate, i made the changes and was able to get data from a text file into an array, and output it into another text file.


The test file i used i just contained 10 numbers, however the text file input data i want to use contains:

name //can be 3 words seperated by spaces
10 numbers // all ints

so eg:

Bla Bla Bla

13 18 38 49 58 69 94 85 48 95

Bleh Bleh Bleh

83 47 38 49 03 27 45 48 04 58


How would i change my code so it can output data in the form of:

name - average of first 5 numbers - average of last 5 numbers
name - average of first 5 numbers - average of last 5 numbers

#include <iostream.h>
#include <string>
#include <fstream>
using namespace std;



bool getInputFilename(char fname[])
{
	ifstream fin;

	cout << "Please enter the filename for input : ";
	cin >> fname;
	
	fin.open(fname, ios::nocreate);
	
	if (!fin.is_open())
		return false;
	
	fin.close();
	return true;
}


bool getOutputFilename(char fname[])
{
	ofstream fout;

	cout << "Please enter the filename for output : ";
	cin >> fname;

	fout.open(fname);

	if (fout.fail())
		return false;

	fout.close();
	return true;

}

void firstArray(ifstream& fin, int marks [], int arraySize, char name[])
{


	while (!fin.eof()) {


  	for (int i = 0; i < 12; i++) {
  		fin >> marks[i];
		fin >> name;
  					
	}
	
	}
}






void main()
{
	ifstream fin;
	ofstream fout;

	char IFname[20], OFname[20];
	int marks [12];
	char name [3];


	while (!getInputFilename(IFname))
	{
		cout << "Invalid filename try again!\n\n";
	}

	while (!getOutputFilename(OFname))
	{
		cout << "Invalid filename try again!\n\n";
	}

	fout.open(OFname);
	fin.open(IFname);

	firstArray(fin,marks,12, name);

	 while (!fin.eof()) {

		for(int i = 0; i < 12; i++)
			fout << marks[i] << endl;
		fout << name << endl;
	}

			    

	fout.close();
	fin.close();

}

Didnt quite understand you, attempted changes i made are in red, doesnt work but just trying to figure it out.

move fin >> name OUTSIDE that loop! there is only one instance of it on a line. that whole look is wrong anyway -- should not use eof() like that because it sometimes produces undesireable results.

fin >> name;
            i = 0;
	while (i < 12 && fin>>marks[i]) {
		i++;
	}

Ok now when i run the program it doesnt close and just keeps running for some reason after asking for the output file?

changed what you said to:

#include <iostream.h>
#include <string>
#include <fstream>
using namespace std;



bool getInputFilename(char fname[])
{
	ifstream fin;

	cout << "Please enter the filename for input : ";
	cin >> fname;
	
	fin.open(fname, ios::nocreate);
	
	if (!fin.is_open())
		return false;
	
	fin.close();
	return true;
}


bool getOutputFilename(char fname[])
{
	ofstream fout;

	cout << "Please enter the filename for output : ";
	cin >> fname;

	fout.open(fname);

	if (fout.fail())
		return false;

	fout.close();
	return true;

}

void firstArray(ifstream& fin, int marks [], int arraySize, char name[])
{


	while (!fin.eof()) {


  	for (int i = 0; i < 12; i++) {
  		fin >> marks[i];
		fin >> name;
  					
	}
	
	}
}






void main()
{
	ifstream fin;
	ofstream fout;

	char IFname[20], OFname[20];
	int marks [12], i;
	char name [3];


	while (!getInputFilename(IFname))
	{
		cout << "Invalid filename try again!\n\n";
	}

	while (!getOutputFilename(OFname))
	{
		cout << "Invalid filename try again!\n\n";
	}

	fout.open(OFname);
	fin.open(IFname);

	firstArray(fin,marks,12, name);

	fin >> name;
        i = 0;

	while (i < 12 && fin>>marks[i]) {
		i++;
	}

	fout.close();
	fin.close();

}

Really appreciate you taking the time to help.

that loop is supposed to be in firstArray(), not main(). There is a corrected program

#include <iostream.h>
#include <string>
#include <fstream>
using namespace std;



bool getInputFilename(char fname[])
{
	ifstream fin;

	cout << "Please enter the filename for input : ";
	cin >> fname;
	
	fin.open(fname, ios::nocreate);
	
	if (!fin.is_open())
		return false;
	
	fin.close();
	return true;
}


bool getOutputFilename(char fname[])
{
	ofstream fout;

	cout << "Please enter the filename for output : ";
	cin >> fname;

	fout.open(fname);

	if (fout.fail())
		return false;

	fout.close();
	return true;

}

void firstArray(ifstream& fin, int marks [], char name[])
{
	fin >> name;
	int i = 0;
	while (i < 12 && fin>>marks[i]) {
		i++;
	}
}






void main()
{
	ifstream fin;
	ofstream fout;

	char IFname[20], OFname[20];
	int marks [12], i;
	char name [3];


	while (!getInputFilename(IFname))
	{
		cout << "Invalid filename try again!\n\n";
	}

	while (!getOutputFilename(OFname))
	{
		cout << "Invalid filename try again!\n\n";
	}

	fout.open(OFname);
	fin.open(IFname);

	firstArray(fin,marks, name);


	fout.close();
	fin.close();

}

Starting to take form now and im beginning to understand some of it :P

Right now i have the program running, and can take the name and place it into an output text file, however as the name could be 2 - 3 words long, seperated by spaces, it will sometimes enter in a number if the name is only 2 words long (the number gets entered as surname).

Is there a better way to output the name to the text file?

#include <iostream.h>
#include <string>
#include <fstream>
using namespace std;



bool getInputFilename(char fname[])
{
	ifstream fin;

	cout << "Please enter the filename for input : ";
	cin >> fname;
	
	fin.open(fname, ios::nocreate);
	
	if (!fin.is_open())
		return false;
	
	fin.close();
	return true;
}


bool getOutputFilename(char fname[])
{
	ofstream fout;

	cout << "Please enter the filename for output : ";
	cin >> fname;

	fout.open(fname);

	if (fout.fail())
		return false;

	fout.close();
	return true;

}

void firstArray(ifstream& fin, int marks [], char name[], char middleName[], char surname[])
{
	fin >> name >> middleName >> surname;
	 
	int i = 0;
	while (i < 12 && fin>>marks[i]) {
		i++;
	}
}






void main()
{
	ifstream fin;
	ofstream fout;

	char IFname[20], OFname[20];
	int marks [12];
	char name [20];
	char middleName[20];
	char surname[20];

	while (!getInputFilename(IFname))
	{
		cout << "Invalid filename try again!\n\n";
	}

	while (!getOutputFilename(OFname))
	{
		cout << "Invalid filename try again!\n\n";
	}

	fout.open(OFname);
	fin.open(IFname);

	firstArray(fin,marks, name, middleName, surname);

	fout << name << " " << middleName << " " << surname;


	fout.close();
	fin.close();

}

since name is on a separate line, I would use getline() to read the entire line into a temporary string, than parse that string for spaces. std::string has a find function that you can use to get the individual words

#include <string.h>
//
..
string temp;
// read the entire line in the string
getline(input_file,temp);
// get last name
//
// locate the first space that separater last and first names
int pos = temp.find(' ');
// if found, then copy it to destination character buffer
if(pos > 0)
{
   strcpy(last_name,temp.substr(0,pos).c_str());
   // remove last name from the string
   temp = temp.substr(pos+1);
}
// now do the same with first and middle names

or if you want to use c strings for this

char temp[80];
// read the entire line in the string
input_file.getline(temp,sizeof(temp));
// get last name
//
// locate the first space that separater last and first names
char *ptr = strchr(temp,' ');
// if found, then copy it to destination character buffer
if(ptr != 0)
{
   // null terminate the last name portion of temp string
   *ptr++ = 0;
   // bypass all white space to get to the first name
   while(*ptr && isspace(*ptr))
     ptr++;
   // copy last name to final character buffer
   strcpy(last_name,temp);
   // remove last name from the string.  This will move everythnig
  // in the temp string left to write over the last name.
   strcpy(temp,ptr);
}
// now do the same with first and middle names
This article has been dead for over six months. Start a new discussion instead.