Inputting text file data into an array, please help!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2006
Posts: 12
Reputation: mybrainhurts is an unknown quantity at this point 
Solved Threads: 0
mybrainhurts mybrainhurts is offline Offline
Newbie Poster

Inputting text file data into an array, please help!

 
0
  #1
Jan 8th, 2006
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

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:
  1. #include <iostream.h>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6.  
  7.  
  8. bool getInputFilename(char fname[])
  9. {
  10. ifstream fin;
  11.  
  12. cout << "Please enter the filename for input : ";
  13. cin >> fname;
  14.  
  15. fin.open(fname, ios::nocreate);
  16.  
  17. if (!fin.is_open())
  18. return false;
  19.  
  20. fin.close();
  21. return true;
  22. }
  23.  
  24.  
  25. bool getOutputFilename(char fname[])
  26. {
  27. ofstream fout;
  28.  
  29. cout << "Please enter the filename for output : ";
  30. cin >> fname;
  31.  
  32. fout.open(fname);
  33.  
  34. if (fout.fail())
  35. return false;
  36.  
  37. fout.close();
  38. return true;
  39.  
  40. }
  41.  
  42. int firstArray(int i, int array[6], int maxSize, int amountRead)
  43. {
  44. ifstream fin;
  45. ofstream fout;
  46.  
  47. while(fin>>array[amountRead]&& amountRead < maxSize)
  48. {
  49. amountRead++;
  50. }
  51.  
  52. for (i=0; i < 5; i++)
  53. {
  54. fout << array[i] << endl;
  55. }
  56.  
  57. cin.get();
  58.  
  59. return 0;
  60. }
  61.  
  62.  
  63.  
  64. void main()
  65. {
  66. ifstream fin;
  67. ofstream fout;
  68.  
  69.  
  70. char IFname[20], OFname[20];
  71.  
  72.  
  73.  
  74.  
  75. while (!getInputFilename(IFname))
  76. {
  77. cout << "Invalid filename try again!\n\n";
  78. }
  79.  
  80. while (!getOutputFilename(OFname))
  81. {
  82. cout << "Invalid filename try again!\n\n";
  83. }
  84.  
  85. fout.open(OFname);
  86. fin.open(IFname);
  87.  
  88.  
  89.  
  90. }

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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Inputting text file data into an array, please help!

 
0
  #2
Jan 8th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 12
Reputation: mybrainhurts is an unknown quantity at this point 
Solved Threads: 0
mybrainhurts mybrainhurts is offline Offline
Newbie Poster

Re: Inputting text file data into an array, please help!

 
0
  #3
Jan 9th, 2006
So how would i call the function to make it work? Please
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 12
Reputation: mybrainhurts is an unknown quantity at this point 
Solved Threads: 0
mybrainhurts mybrainhurts is offline Offline
Newbie Poster

Re: Inputting text file data into an array, please help!

 
0
  #4
Jan 9th, 2006
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...
  1. #include <iostream.h>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6.  
  7.  
  8. bool getInputFilename(char fname[])
  9. {
  10. ifstream fin;
  11.  
  12. cout << "Please enter the filename for input : ";
  13. cin >> fname;
  14.  
  15. fin.open(fname, ios::nocreate);
  16.  
  17. if (!fin.is_open())
  18. return false;
  19.  
  20. fin.close();
  21. return true;
  22. }
  23.  
  24.  
  25. bool getOutputFilename(char fname[])
  26. {
  27. ofstream fout;
  28.  
  29. cout << "Please enter the filename for output : ";
  30. cin >> fname;
  31.  
  32. fout.open(fname);
  33.  
  34. if (fout.fail())
  35. return false;
  36.  
  37. fout.close();
  38. return true;
  39.  
  40. }
  41.  
  42. void firstArray(char name[], int marks [])
  43. {
  44. ifstream fin;
  45. ofstream fout;
  46.  
  47. while (!fin.eof()) {
  48.  
  49.  
  50. for (int i = 0; i < 12; i++) {
  51. fin >> marks[i];
  52.  
  53. }
  54.  
  55. }
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. void main()
  64. {
  65. ifstream fin;
  66. ofstream fout;
  67.  
  68. char IFname[20], OFname[20];
  69. int marks [12];
  70. char name[40];
  71.  
  72. firstArray(name, marks);
  73.  
  74.  
  75.  
  76. while (!getInputFilename(IFname))
  77. {
  78. cout << "Invalid filename try again!\n\n";
  79. }
  80.  
  81. while (!getOutputFilename(OFname))
  82. {
  83. cout << "Invalid filename try again!\n\n";
  84. }
  85.  
  86. fout.open(OFname);
  87. fin.open(IFname);
  88.  
  89. fout << marks << endl;
  90.  
  91.  
  92. fout.close();
  93. fin.close();
  94.  
  95. }

Output file ends up giving me some random numbers (0012FE00)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Inputting text file data into an array, please help!

 
0
  #5
Jan 9th, 2006
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.
  1. void firstArray(ifstream& in, int marks [], int arraySize)
  2. {
  3. ...

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
  1. fin.open(IFname);
  2. if(!fin.is_open())
  3. {
  4. cout << "cannot open file " << IFname << endl;
  5. return 1;
  6. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 12
Reputation: mybrainhurts is an unknown quantity at this point 
Solved Threads: 0
mybrainhurts mybrainhurts is offline Offline
Newbie Poster

Re: Inputting text file data into an array, please help!

 
0
  #6
Jan 9th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Inputting text file data into an array, please help!

 
0
  #7
Jan 9th, 2006
to this twice (or put in loop that runs until end-of-file

  1. fin >> name;
  2. firstArray(...);
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 12
Reputation: mybrainhurts is an unknown quantity at this point 
Solved Threads: 0
mybrainhurts mybrainhurts is offline Offline
Newbie Poster

Re: Inputting text file data into an array, please help!

 
0
  #8
Jan 9th, 2006
#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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Inputting text file data into an array, please help!

 
0
  #9
Jan 9th, 2006
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.
  1. fin >> name;
  2. i = 0;
  3. while (i < 12 && fin>>marks[i]) {
  4. i++;
  5. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 12
Reputation: mybrainhurts is an unknown quantity at this point 
Solved Threads: 0
mybrainhurts mybrainhurts is offline Offline
Newbie Poster

Re: Inputting text file data into an array, please help!

 
0
  #10
Jan 9th, 2006
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:

  1. #include <iostream.h>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6.  
  7.  
  8. bool getInputFilename(char fname[])
  9. {
  10. ifstream fin;
  11.  
  12. cout << "Please enter the filename for input : ";
  13. cin >> fname;
  14.  
  15. fin.open(fname, ios::nocreate);
  16.  
  17. if (!fin.is_open())
  18. return false;
  19.  
  20. fin.close();
  21. return true;
  22. }
  23.  
  24.  
  25. bool getOutputFilename(char fname[])
  26. {
  27. ofstream fout;
  28.  
  29. cout << "Please enter the filename for output : ";
  30. cin >> fname;
  31.  
  32. fout.open(fname);
  33.  
  34. if (fout.fail())
  35. return false;
  36.  
  37. fout.close();
  38. return true;
  39.  
  40. }
  41.  
  42. void firstArray(ifstream& fin, int marks [], int arraySize, char name[])
  43. {
  44.  
  45.  
  46. while (!fin.eof()) {
  47.  
  48.  
  49. for (int i = 0; i < 12; i++) {
  50. fin >> marks[i];
  51. fin >> name;
  52.  
  53. }
  54.  
  55. }
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63. void main()
  64. {
  65. ifstream fin;
  66. ofstream fout;
  67.  
  68. char IFname[20], OFname[20];
  69. int marks [12], i;
  70. char name [3];
  71.  
  72.  
  73. while (!getInputFilename(IFname))
  74. {
  75. cout << "Invalid filename try again!\n\n";
  76. }
  77.  
  78. while (!getOutputFilename(OFname))
  79. {
  80. cout << "Invalid filename try again!\n\n";
  81. }
  82.  
  83. fout.open(OFname);
  84. fin.open(IFname);
  85.  
  86. firstArray(fin,marks,12, name);
  87.  
  88. fin >> name;
  89. i = 0;
  90.  
  91. while (i < 12 && fin>>marks[i]) {
  92. i++;
  93. }
  94.  
  95. fout.close();
  96. fin.close();
  97.  
  98. }

Really appreciate you taking the time to help.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC