8 Replies - 656 Views - Last Post: 28 January 2013 - 02:17 PM Rate Topic: -----

#1 darealmzm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 29-October 12

C++ Problem: How to read certain data from text file into array?

Posted 27 January 2013 - 03:01 AM

Hello everyone, it's been a while since I've done anything in C++ so please excuse the errors, modularity, etc. For this homework assignment we basically have to write a program that reads certain data from a file into an array, (My professor says we don't have to use the array if we can "analyze the data stream as it comes in" but I can't figure that out and I haven't been able to find out how to do it online unless you all can help me.), count how many different numbers are in the file, use a mergesort to put the numbers in order, etc.
The problem so far is how to get the second row of data out of the file and put it in an array.

For example if my file look like this:

1. 20
2. 15
3. 77
4. 15
5. 29
6. 77

How would I get the 20, 15, etc. into an array without reading the 1. , 2. , 3. ?
I have more questions but I need to figure this out first before I get into that. Thanks in advance!!!

Here's my code so far:



#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;


int main (int argc, char * const argv[]) {
  
    std::cout << "Hello, World!\n";
	
	double time_to_accesstrack, sort_coeficient;
	string input_filename;
	
	cout << "Welcome to the I/O reordering analyzer" << endl;
	cout << "\n"<< endl;
	cout << "How long in milleseconds does the drive need to access a track?";
	cin >> time_to_accesstrack;
	cout << "What is the CPU's sort coefficient (in milliseconds)?";
	cin >> sort_coeficient;
	cout << "What is the name of the test data file?";
	cin >> input_filename;
	cout << "\n" << endl;
	cout << "Analyzing..."<< endl;
	
	while (true)
    {
		string infilename;
		
		getline( cin, input_filename );
		infile.open( input_filename.c_str() );
		if(!inFile) {
			cout << endl << "Failed to open file " << input_filename;
			return 1;
		}
		
		long n = 0;
		while(!inFile.eof()) {
			inFile >> n;
			cout << std::setw(15) << n;
		}
		cout << endl;
		
   
    	
	
	
	return 0;
}


	
}




Is This A Good Question/Topic? 0
  • +

Replies To: C++ Problem: How to read certain data from text file into array?

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2957
  • View blog
  • Posts: 10,177
  • Joined: 08-August 08

Re: C++ Problem: How to read certain data from text file into array?

Posted 27 January 2013 - 07:25 AM

Read this:
http://www.dreaminco...ostreamistream/
Was This Post Helpful? 1
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,574
  • Joined: 19-February 09

Re: C++ Problem: How to read certain data from text file into array?

Posted 27 January 2013 - 02:16 PM

You could treat the 1. as a string, read it in and discard it.
Was This Post Helpful? 1
  • +
  • -

#4 darealmzm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 29-October 12

Re: C++ Problem: How to read certain data from text file into array?

Posted 27 January 2013 - 04:34 PM

Ok so I was able to figure out how to put the information in the array but when I prompt the user for the name of the file to be used and type it in, it can't find the file. Is my code for opening the file wrong or what, because I typed the name of the file correctly and should have been able to open it. Can someone please help me figure this out?


#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <sstream>
#include <string>
using namespace std;


int main (int argc, char * const argv[]) {
  
    std::cout << "Hello, World!\n";
	
	double time_to_accesstrack, sort_coeficient;
	ifstream infile;
	string input_filename;
	std::vector<std::vector<int> > allData;
	
	cout << "Welcome to the I/O reordering analyzer" << endl;
	cout << "\n"<< endl;
	cout << "How long in milleseconds does the drive need to access a track?";
	cin >> time_to_accesstrack;
	cout << "What is the CPU's sort coefficient (in milliseconds)?";
	cin >> sort_coeficient;
	cout << "What is the name of the test data file?";
	cin >> input_filename;
	cout << "\n" << endl;
	cout << "Analyzing..."<< endl;
	
	while (true)
    {
		
		getline( cin, input_filename );
		infile.open( input_filename.c_str() );
		std::vector<int> lineData;         
		int val;
		
		std::istringstream lineStream(input_filename);
		if(infile) break;
			cout << endl << "Failed to open file " << flush;
			return 1;
		
		
		while (lineStream >> val) {          
			lineData.push_back(val);         
		}
		
		allData.push_back(lineData);   
		}
		
	for (int i=0; i<allData.size(); ++i)
    {
        for (int j=0; j<allData[i].size(); ++j)
        {
            cout << allData[i][j] << " ";
        }
        cout << "\n";
    }
	cout << "row 0 contains " << allData[0].size() << " columns\n";
    cout << "row 0, column 1 is " << allData[0][1] << "\n";	
		
   
    	
	
	
	return 0;
}


	





Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,420
  • Joined: 25-December 09

Re: C++ Problem: How to read certain data from text file into array?

Posted 27 January 2013 - 04:58 PM

To properly open a fstream() object with the default open mode parameters the file must exist.

What are you entering for the file name?

Are you entering an absolute or relative path name for the file? An absolute path starts from the "root" directory. A relative path means relative to the current working directory.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,574
  • Joined: 19-February 09

Re: C++ Problem: How to read certain data from text file into array?

Posted 27 January 2013 - 05:01 PM

You ask for the filename twice.

26	    cin >> input_filename;
 
33	        getline( cin, input_filename );


Was This Post Helpful? 0
  • +
  • -

#7 darealmzm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 29-October 12

Re: C++ Problem: How to read certain data from text file into array?

Posted 27 January 2013 - 05:57 PM

Thanks for the quick response. I'm using a relative path. An example of what I'm entering for the file name is dataset1.txt

View Post#define, on 28 January 2013 - 12:01 AM, said:

You ask for the filename twice.

26	    cin >> input_filename;
 
33	        getline( cin, input_filename );



Ok should I get rid of the one at line 33 because after I prompt the user for the name of the textfile, I'm trying to open the file the user enters.
Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1335
  • View blog
  • Posts: 4,574
  • Joined: 19-February 09

Re: C++ Problem: How to read certain data from text file into array?

Posted 27 January 2013 - 08:25 PM

Yes, you could get rid of line 33.

Are you just reading from one file?

Then I don't think you should be opening the file in the while loop.

Should the program follow a plan like this?

  create vector for integers
  get file name
  open file

  begin loop
    get data from file
    add int to vector
  end loop

  print vector contents

  close file


Was This Post Helpful? 0
  • +
  • -

#9 darealmzm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 29-October 12

Re: C++ Problem: How to read certain data from text file into array?

Posted 28 January 2013 - 02:17 PM

View Post#define, on 28 January 2013 - 03:25 AM, said:

Yes, you could get rid of line 33.

Are you just reading from one file?

Then I don't think you should be opening the file in the while loop.

Should the program follow a plan like this?

  create vector for integers
  get file name
  open file

  begin loop
    get data from file
    add int to vector
  end loop

  print vector contents

  close file



Thank you so much! I've added a quicksort to my program to sort the array. However we have to use a O(n log n) to calculate the time to do the sort. For example the user inputs 0.3 which is O. The count of how many times the number changes = n. How would I put this in code form, because I don't know anything about algorithms.

View Post#define, on 28 January 2013 - 03:25 AM, said:

Yes, you could get rid of line 33.

Are you just reading from one file?

Then I don't think you should be opening the file in the while loop.

Should the program follow a plan like this?

  create vector for integers
  get file name
  open file

  begin loop
    get data from file
    add int to vector
  end loop

  print vector contents

  close file


Thank you so much! I've added a quicksort to my program to sort the array. However we have to use a O(n log n) to calculate the time to do the sort. For example the user inputs 0.3 which is O. The count of how many times the number changes = n. How would I put this in code form, because I don't know anything about algorithms.

void readData(double& data);
const int LOCATIONS = 64;
int main (int argc, char * const argv[]) {
  
    std::cout << "Hello, World!\n";
	
	double time_to_accesstrack, sort_coeficient, ordered = 0, unordered =0, totalTrackRequest, drivearm;
	std::vector<std::vector<int> > allData;
	
	cout << "Welcome to the request reordering analyzer" << endl;
	cout << "\n"<< endl;
	cout << "How long (in milleseconds) does the drive need to access a track?";
	cin >> time_to_accesstrack;
	cout << "What is the CPU's sort coefficient (in milliseconds)?";
	cin >> sort_coeficient;
	
	
	void readData(double& data){
		
		double data[LOCATIONS] = {0};
		int nNumTrackReq = 0;
		ifstream in;
		string inputFileName;
		int numRecords = 0;
		double trackRequests;
		
		
		cout << "What is the name of the test data file? ";
		cin >> inputFileName;
		
		in.open(inputFileName.c_str());
		while(in.fail( )){
				
			in.clear();
			system("cls");
			cout << "Cannot Open File: " << inputFileName << endl <<"\n\nPlease Re-Enter the input file name: ";
			cin >> inputFileName;
			in.open(inputFileName.c_str());
				
			}
		system("cls");
		cout << "Analyzing file..." << endl << endl;
		while(!in.eof()) {
				
			in >> data[nNumTrackReq];
			trackRequests = data[nNumTrackReq];
			nNumTrackReq++;
			totalSales += sales;
		}
		return data;
		
	}
		
		void quickSort(int left, int right, double& time_to_accesstrack, 
					   double& sort_coeficient, double& ordered, 
					   double& unordered, double& totalTrackRequest, double& drivearm) {
			
			readData(data);
			int i; int j;
			
			for (int nIndex = 0; i<= nIndex; nIndex++){
				if (data[nIndex] != data[nIndex])
					ordered++;
				
			}
			
			int i = left, j = right;
			int tmp;
			int pivot = data[(left + right) / 2];
			
			while (i <= j) {
				while (data[i] < pivot)
					i++;
				while (data[j] > pivot)
					j--;
				if (i <= j) {
					tmp = data[i];
					data[i] = data[j];
					data[j] = tmp;
					i++;
					j--;
				}
			};
			
			
			if (left < j)
				quickSort(data, left, j);
			if (i < right)
				quickSort(data, i, right);
		}




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1