943,969 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7776
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 8th, 2006
0

Inputting text file data into an array, please help!

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mybrainhurts is offline Offline
12 posts
since Jan 2006
Jan 8th, 2006
0

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

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jan 9th, 2006
0

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

So how would i call the function to make it work? Please
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mybrainhurts is offline Offline
12 posts
since Jan 2006
Jan 9th, 2006
0

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

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...
C++ Syntax (Toggle Plain Text)
  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)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mybrainhurts is offline Offline
12 posts
since Jan 2006
Jan 9th, 2006
0

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

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.
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. fin.open(IFname);
  2. if(!fin.is_open())
  3. {
  4. cout << "cannot open file " << IFname << endl;
  5. return 1;
  6. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jan 9th, 2006
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mybrainhurts is offline Offline
12 posts
since Jan 2006
Jan 9th, 2006
0

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

to this twice (or put in loop that runs until end-of-file

C++ Syntax (Toggle Plain Text)
  1. fin >> name;
  2. firstArray(...);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jan 9th, 2006
0

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

#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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mybrainhurts is offline Offline
12 posts
since Jan 2006
Jan 9th, 2006
0

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

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.
C++ Syntax (Toggle Plain Text)
  1. fin >> name;
  2. i = 0;
  3. while (i < 12 && fin>>marks[i]) {
  4. i++;
  5. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jan 9th, 2006
0

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

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:

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mybrainhurts is offline Offline
12 posts
since Jan 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Can't get loop to work properly
Next Thread in C++ Forum Timeline: 'double' problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC