Get input file from string

For this assignment I need to get the input file from one ifstream and create another ifstream with the input file name being contained in the first ifstream file.

What's wrong is that ifstream anotherin(_datafile2) has to take in variable input file names. The input in the first ifstream the file is going to be changing so the string can't be static. I know it has to be enclosed in quotes so I used a string stream to do that but it still isn't reading the file. If I hard code my file name into ifstream anotherin(_datafile2) my code runs. Can someone help me get this to work. This is beyond annoying and I tried using escape sequences but it's still not going well.

Here's the input text file for the first ifstream:
1
2
3
4
5
filename: data.txt
find-name Dave
find-number 4.5
find-name Freda
find-number 3.14

Next here's the actual data.txt
1
2
3
4
Dave 10.0
Molly 13.1
Matthew 3.2
Kelly 4.5

Here's what I should be getting:
1
2
3
4
Found: Dave 10.0
Found: Kelly 4.5
Not found: Freda
Not found: 3.14

First Header File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef _LAB9_H_ //make sure lab8.h included only once
#define _LAB9_H_ //make sure lab8.h included only once

#include <fstream> //for using ifstream and ofstream
#include <iomanip>
#include <string>  //for using strings
#include <sstream> //originally for planned string stream
#include <iostream>

using std::ifstream;
using std::ofstream;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::ostream;
using std::istringstream;
using std::stringstream; //for string stream but not used anymore

struct basicStruct
{
	double number;
	string inputname;
	string name;
};

#endif //make sure header included only once


Second Header File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "basic.h" //let's make sure to include the Feature header file in this header file.
				   //but later make sure to include it only once if we're including this header file. 
				   //in the implementation CPP as well.
#include <fstream>   //for using ifstream and of stream filestreams
#include <iostream>
#include <ios>
#include <string>    //for using strings
#include <sstream>   //for the string stream
#include <iomanip>   //for i/o manipulation as this gives setw, setprecision, fixed, showpoint

using std::string;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::ostream;
using std::istringstream;
using std::getline;
using std::setw;
using std::ios;

const int SIZE = 10; //setting the size of the int which is to be used as the size of the array.
					  //must be constant hence the use of const int.

void readData(string input, string output);

basicStruct readFeature(string line);

void findname(basicStruct myFeatures[], int count, ifstream &anotherin, ofstream &out);

void findID(basicStruct myFeatures[], int count, ifstream &anotherin, ofstream &out);


CPP File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "Lab9.h"
#include "basic.h"

void readData(string inputfile, string outputfile)
{
	ifstream in(inputfile); //set in to inputfile
	ofstream out(outputfile); //set out to outputfile

	string datafile;
	string _datafile2;
	string filenametext;
	string line;
	string command;
	stringstream datafile2;
	ifstream anotherin(_datafile2);

	struct basicStruct basic[SIZE], search;
	int count = 0;

	getline(anotherin, line);
	while (!in.fail() && count < SIZE)
	{
		search = readFeature(line);
		if (count < SIZE)
		{
			basic[count] = search;
			count++;
		}
		getline(anotherin, line);
	}
	anotherin.close();

	getline(in, filenametext, ' ');
	getline(in, datafile, '\n');
	getline(in, command, ' ');

	datafile2 << "\"" << datafile << "\"";
	_datafile2 = datafile2.str();

	while (!in.fail())
	{
		if (command == "find-name")
		{
			findname(basic, count, in, out);
		}
		else if (command == "find-number")
		{
			findID(basic, count, in, out);
		}
		in >> command;
	}
	in.close();
}

basicStruct readFeature(string line)
{
	basicStruct search;
	istringstream in(line);
	getline(in, search.name, ' ');
	in >> search.number;

	return search;
}

void findname(basicStruct myFeatures[], int count, ifstream &in, ofstream &out)
{
	bool found = false;
	string nameToFind;

	getline(in, nameToFind, '\n');
	for (int i = 0; i < count; i++)
	{
		if (myFeatures[i].name.find(nameToFind) != string::npos)
		{
			found = true;
			out << "Found: " << myFeatures[i].name << " " << myFeatures[i].number << endl;
		}
	}
	if (!found)
	{
		out << "Not found: " << nameToFind << endl;
	}
}

void findID(basicStruct myFeatures[], int count, ifstream &in, ofstream &out)
{
	bool found = false;
	double idToFind;

	in >> idToFind;
	for (int i = 0; i < count; i++)
	{
		if (myFeatures[i].number == idToFind)
		{
			found = true;
			out << "Found: " << myFeatures[i].name << " " << myFeatures[i].number << endl;
		}
	}
	if (!found)
	{
		out << "Not found: " << idToFind << endl;
	}
}

Last edited on
Topic archived. No new replies allowed.