Proper use of ifstream and Funtions

This is the code I've attempted to use:

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
/************************
Purpose of the Program: Read an input file for gene information and sort the information into different arrays to be reffered to to check for specific letters in specific location.
Input: "dna.txt"
Output: "DNAanalysis.txt"
Main Algorithm:
Assumptions: "dna.txt" will not be altered and only holds the gene info of four individuals.
Program Limitations: Hardcoded for the reading of four people and only four people at any given time.
************************/

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

// Functions

void input(ifstream &); // Get input file dna.txt
void read(char[], char[], char[], char[], char[], char[], char[], char[], ifstream);

int main()
{
	ifstream inFile; 
	ofstream outFile; 
	char Person1GeneA[444], Person1GeneB[444], Person2GeneA[444], Person2GeneB[444], Person3GeneA[444], Person3GeneB[444], Person4GeneA[444], Person4GeneB[444]; // Char arrays for each gene's data
	
	input(inFile); // Opens input file dna.txt
	
	read(Person1GeneA, Person1GeneB, Person2GeneA, Person2GeneB, Person3GeneA, Person3GeneB, Person4GeneA, Person4GeneB, inFile);

	return 0;
}

// Functions

void input(ifstream & inFile) // Get input file dna.txt
{
	inFile.open("dna.txt"); // attempt to open the input file
	if (!inFile)	// did it open?
	{
		cout << "Error opening input file dna.txt. Exiting program." << endl;
		exit(0);
	}
}

void read(char[], char[], char[], char[], char[], char[], char[], char[], ifstream inFile)
{

}


I'm a little stumped as to why my read function is telling me that my inFile is a deleted function and how I would go about properly doing this to get my dna.txt into my read function. Thanks in advance!
Last edited on
You should be passing the ifstream (or better yet istream) by reference.

Also what why all the arrays? Why not use std::string?

This is an assignment specifically for understanding of arrays, so I'm under the impression that I must use arrays to solve this for full marks.

Also that worked perfectly! Such a simple fix! Thanks!
Last edited on
It's kind of silly to have a function just to open the file. Instead, add that to the readfile function. And you can simplify the interface by using an array of structs.
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

struct Person {
    char geneA[444];
    char geneB[444];
};

void readfile(Person *people);

int main() {
    Person people[4];
    readfile(people);
    for (int i = 0; i < 4; i++)
        cout << people[i].geneA << '\n' << people[i].geneB << '\n';
    return 0;
}

void readfile(Person *people) {
    ifstream fin("dna.txt");
    if (!fin) {
        cerr << "Error opening input file dna.txt. Exiting program." << endl;
        exit(EXIT_FAILURE);
    }
    for (int i = 0; i < 4; i++) {
        fin.getline(people[i].geneA, 444);
        fin.getline(people[i].geneB, 444);
    }
    // fin automatically closes at the end of the function
}

Topic archived. No new replies allowed.