-
Trouble with file io
Hi, I am new to c++ (and programming in general) and have a problem with a small project that I am doing to aid my research. I run multiple Polymerase Chain Reactions to amplify specific sections of DNA from pathogenic bacteria. Anyway, to take some of the pain out of setting these up, I read up a bit and wrote a small c++ programme to calculate the volumes I need for single and multiple PCR reactions. The programme works fine but I would like to add the ability to save the output from my programme to a file. Specifically I would like the programme to invoke the user to name an output file and then for the programme to write to that file and to close the file. I have tried using examples of file io from this site and others but i do not have enough experience or knowledge to implement them properly. I have included the code below. Would anyone be able to point me in the direction of a tutorial that could help me with this, or suggest some additions to my code? The programme was written using emacs on a Mac running mac os 10.2.5 and compiled using g++.
Thanks in advance
will
Code:
// Mastermix Version 0.4
// April 2003
// William Gordon MacKay
// E-mail - wgmk1d@udcf.gla.ac.uk
#include <iostream.h>
using namespace std;
int main ()
{
cout << endl << endl;
cout << "MASTERMIX (version 0.4 [23.04.2003])" << endl << endl;
cout << "WG MacKay (wgmk1d@udcf.gla.ac.uk)" << endl << endl;
cout << "Please enter the volumes for one PCR (in microlitres) below," << endl << endl;
// Defines the PCR reagent variables and prompt the user to asign them values
// These PCR reagent variables are then asigned names and printed to the terminal
int ReturnCode = 0;
float pcr = 1;
cout << "Total vol: ";
cin >> pcr;
if (!cin.fail()) // numerical value entered
{
float pcrB = 1;
cout << "Buffer: ";
cin >> pcrB;
if (!cin.fail()) // numerical value entered
{
float pcrM = 1;
cout << "MgCl2: ";
cin >> pcrM;
if (!cin.fail()) // numerical value entered
{
float pcrD = 1;
cout << "dNTPs: ";
cin >> pcrD;
if (!cin.fail()) // numerical value entered
{
float pcrF = 1;
cout << "Forward: ";
cin >> pcrF;
if (!cin.fail()) // numerical value entered
{
float pcrR = 1;
cout << "Reverse: ";
cin >> pcrR;
if (!cin.fail()) // numerical value entered
{
float pcrU = 1;
cout << "Undefined: ";
cin >> pcrU;
if (!cin.fail()) // numerical value entered
{
float pcrT = 1;
cout << "Taq: ";
cin >> pcrT;
if (!cin.fail()) // numerical value entered
{
float pcrA = 1;
cout << "Target: ";
cin >> pcrA;
if (!cin.fail()) // numerical value entered
{
// The variable water is a special case and is calculated as the "Total volume" - all other volumes
float Water = (pcr-(pcrB+pcrM+pcrD+pcrF+pcrR+pcrT+pcrU+pcrA));
cout << "Water: " << Water << endl << endl;
// Defines the "number of reactions" variable and prompt the use to asign it a value.
float Nr = 1;
cout << "Enter number of PCR reactions (+1 to account for pipetting error): ";
cin >> Nr;
cout << endl;
// New variables are defined that are then multiplied by the "number of reactions"
// variable and the corresponding PCR variable.
// This produces the PCR reagent variable values for a "PCR mastermix".
float Tpcr = Nr*pcr;
float TpcrB = Nr*pcrB;
float TpcrM = Nr*pcrM;
float TpcrD = Nr*pcrD;
float TpcrF = Nr*pcrF;
float TpcrR = Nr*pcrR;
float TpcrT = Nr*pcrT;
float TpcrA = Nr*pcrA;
float TWater = Nr*Water;
// The orginal PCR reagent variables and the new variables (multiplied by the "number of reactions" variable.
// This produces a list of PCR reagents and their corresponding volumes (in microlitres)
// for a single PCR reaction and a PCR reaction mastermix
cout << endl;
cout << "Calculating mastermix..." << endl << endl;
cout << "PCR x" << Nr << endl << endl;
cout << "Water: " << TWater << endl;
cout << "10X: " << TpcrB << endl;
cout << "MgCl2: " << TpcrM << endl;
cout << "dNTPS: " << TpcrD << endl;
cout << "Forward: " << TpcrF << endl;
cout << "Reverse: " << TpcrR << endl;
cout << "Taq: " << TpcrT << endl;
cout << "Total Vol: " << Tpcr << endl;
cout << endl << endl;
}
else
{
cerr << "ERROR - invalid input" << endl;
cin.clear(); // Clear bad input
char BadInput[5]; // Clear up to 5 characters
cin >> BadInput;
ReturnCode = 1;
};
}
else
{
cerr << "ERROR - invalid input" << endl;
cin.clear(); // Clear bad input
char BadInput[5]; // Clear up to 5 characters
cin >> BadInput;
ReturnCode = 1;
};
}
else
{
cerr << "ERROR - invalid input" << endl;
cin.clear(); // Clear bad input
char BadInput[5]; // Clear up to 5 characters
cin >> BadInput;
ReturnCode = 1;
};
}
else
{
cerr << "ERROR - invalid input" << endl;
cin.clear(); // Clear bad input
char BadInput[5]; // Clear up to 5 characters
cin >> BadInput;
ReturnCode = 1;
};
}
else
{
cerr << "ERROR - invalid input" << endl;
cin.clear(); // Clear bad input
char BadInput[5]; // Clear up to 5 characters
cin >> BadInput;
ReturnCode = 1;
};
}
else
{
cerr << "ERROR - invalid input" << endl;
cin.clear(); // Clear bad input
char BadInput[5]; // Clear up to 5 characters
cin >> BadInput;
ReturnCode = 1;
};
}
else
{
cerr << "ERROR - invalid input" << endl;
cin.clear(); // Clear bad input
char BadInput[5]; // Clear up to 5 characters
cin >> BadInput;
ReturnCode = 1;
};
}
else
{
cerr << "ERROR - invalid input" << endl;
cin.clear(); // Clear bad input
char BadInput[5]; // Clear up to 5 characters
cin >> BadInput;
ReturnCode = 1;
};
}
else
{
cerr << "ERROR - invalid input" << endl;
cin.clear(); // Clear bad input
char BadInput[5]; // Clear up to 5 characters
cin >> BadInput;
ReturnCode = 1;
};
cout << "Goodluck" << endl << endl;
return 0;
}
-
Code:
#include <fstream>//for fstream objects
#include <iostream>
using namespace std;
int main()
{
char filename[50];
cout<<"Enter a file name for output "
<<"(e.g. C:\\MyFolder\\my_file.text):"<<endl;
cin.getline(filename, 50);
fstream output_file(filename);
//associates the file with an fstream object and opens the file
int calculation=4*50 + 2;
output_file<<calculation;
output_file.close();
return 0;
}
Example of execution:
Enter a file name for output (e.g. C:\MyFolder\my_file.text):
C:\TestData\output.txt
Press any key to continue
output.txt
----------
202
P.S. Do you think this was in any way helpful in explaining that you don't know how to do file I/O:
I run multiple Polymerase Chain Reactions to amplify specific sections of DNA from pathogenic bacteria
-
7stud said,
"P.S. Do you think this was in any way helpful in explaining that you don't know how to do file I/O:
I run multiple Polymerase Chain Reactions to amplify specific sections of DNA from pathogenic bacteria"
I thought I was being polite by puting the code in context. Apologies.
Thanks for the advice
will;)