I'm working on a University assignment at the moment (a final assignment). I've normally been able to figure things out, but the teacher's thrown us a curve ball by making us use <fstream>. We've only had one lecture on it which covered outputting a file, but nothing really handling taking a file input.

Our assignment requires that we have two files, "a" and "b" for simplicity's sake.

Each file is set up similarily:

"a.txt"
# <- one integer, defining the number of entries
#[1], #[2], #[3] ... #[#] <- a line of entries, every one an integer

"b".txt"
# <- same number of entries as "a.txt"
#[1], #[2], #[3] ... #[#] <- a line of entries, every one an integer

What we need to do is set up so that these files' names are defined by the user. The program takes the names, opens up the files, and extracts the number of entries, then uses that to extract the entries themselves from the line below. If your first line in "a.txt" says 6, but the second line has 7 entries, the 7th is taken but not used in calculations later in the program (it reads all the way to the end of the file, in short). We're to assume we never see more then 1000 entries.

Now, I'm not sure how to go about doing this.

This is my current set-up. I grabbed the "inStream" from another topic here on Daniweb, but it was used for a slightly different purpose and it isn't behaving the way it should for me.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	int cost[1000];
	int prof[1000];
	int costent;
	int profent;
	string costfile;
	string proffile;
	int budget;

	cout << "Please input cost file name: ";
	cin >> costfile;
	cout << endl << "Please input profit file name: ";
	cin >> proffile;
	cout << endl << endl << "Please enter a budget (CTRL+D to quit): "
	cin >> budget;

	ifstream inStream;
	inStream.open(costfile);
	if (inStream.is_open())
	{
		while (! inStream.eof() )
		{
			inStream >> costent;

			for (int i = 0; i < costent; i++)
			{
				inStream >> cost[i];
			}
		}
	}
	inStream.close();

	ifstream inStream;
	inStream.open(proffile)
	if (inStream.is_open())
	{
		while (! inStream.eof() )
		{
			inStream >> profent;

			for (int i = 0; i < profent; i++)
			{
				inStream >> prof[i];
			}
		}
	}
	inStream.close();

	for(int i = 0; i < costent; i++)
	{
		cout << cost[i];
	}
	cout << endl;
	for(i = 0; i < profent; i++)
	{
		cout << prof[i];
	}

	return 0;
}

What it should do in this code is grab the first entry, use that to define the number of other entries extracted from the files, and then print out each of these entries placed into the array. It should do this for both files as defined by the user (we assume that they're in the same folder as the program itself). However, I'm getting nothing whatsoever (it's opening the file, but not pulling anything into the array), so I'm sure I'm out to lunch here.

I'm a n00b, so forgive me if there is more problems then there are characters, but can anyone point me in the right direction? D:

I can help you if you elaborate on what exactly you want to do. Your question is not very clear. This "then uses that to extract the entries themselves from the line below. If your first line in "a.txt" says 6, but the second line has 7 entries, the 7th is taken but not used in calculations later in the program" is very ambiguous please explain more clearly.

just heads up the easiest way to read file and put the integers into a array would be this.

ifstream MyFile("somefile.txt");
int array[10];//considering there are 10 elements in the file.
int x = 0;
int i = 0;
while(MyFile >> x) //puts data from MyFile into x
          {
              array[i] = x; // puts value of x into array
               i++;
          }  
for(int j=0;j<i;j++)
          {
              cout<<array[j]<<endl; 
          }

Ahh, there we go. Thanks a bunch Joker, I've got it figured out!

No sweat. Good luck :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.