/* Program to reverse a .dat sound file. */ #include "DoubleStack.hh" #include // For error messages #include // File I/O #include // For setprecision #include // For STL strings using namespace std; // This line is necessary for easy use of C++ standard libraries. int main( int argc, char *argv[] ) { ifstream fin; ofstream fout; DoubleStack s; // // Check to see if this program was called correctly // if(argc != 3) { cerr << " Incorrect number of arguments" << endl << " Usage: " << endl << "\t" << argv[0] // argv[0] is the name of the program << " " << endl << endl; return 1; } // // Open and read the input file // fin.open(argv[1]); if(!fin.is_open()) { cerr << " Error opening input file " << argv[ 1 ] << ", quitting" << endl; return 1; } // Read in the first line of the .dat file. We want to store // the "sample rate" in a variable, but we can ignore the rest of the line. int sampleRate; string strJunk; char charJunk; fin >> charJunk >> strJunk >> strJunk; fin >> sampleRate; // Read in the file and place values from the second column in the stack. // The first column values are thrown away. We stop reading if there is // an error in the file stream or if we reach the end of the file double timestep, data; while(fin.good()) { fin >> timestep >> data; s.push(data); } // // Close the input file and open the output file // fin.close(); fout.open(argv[2]); if(!fout.is_open()) { cerr << " Error opening output file " << argv[2] << ", quitting" << endl; return 0; } // This prints out the line "; Sample Rate ", printing // the frequency rate stored in the "sampleRate" variable. fout << "; Sample Rate " << sampleRate << endl; // Since the first column consists of numbers which start at 0 and // increase by 1/sampleRate every time slice, we'll just use numSteps // to recalculate these numbers. int numSteps = 0; // We use the setprecision I/O manipulator in order to print our // doubles with a greater number of decimal places fout << setprecision(8); // Print the values in reverse order (by popping them off the stack). // The first column just consists of numbers which start at 0 and // increase by 1/sampleRate per row, so we'll use numSteps/sampleRate // to recalculate the appropriate values. Uniform spacing will // be accomplished by printing a tab. while(!s.isEmpty()) { fout << (double)numSteps / sampleRate << "\t" << s.top() << endl; s.pop(); numSteps++; } // Close the output file fout.close(); return 0; }