Basic query regarding arrays/data sets
-
I am a musician who is trying to code an app for a project. I basically need to know how I can take a set of data integers saved in a file of some extension <.txt or whatever> that any user like myself can edit and save using notepad or such, and subsequently utilise these in my program. By way of explanation, the app is desired to generate scorefile data for use within a soft synth app and the scorefiles usually take the form of long screeds of largely repeated data values which are coded manually- holding back the popularlity of the software no end!!! If a set of pitch values could be saved in a file then these values could be called by an app to gen the score using them as a template. I appreciate this is a fairly non-technical query, but it is becoming a bit of a stumbling block at my end I would really appreciate a hand. I thank you for your time.
-
I am a musician who is trying to code an app for a project. I basically need to know how I can take a set of data integers saved in a file of some extension <.txt or whatever> that any user like myself can edit and save using notepad or such, and subsequently utilise these in my program. By way of explanation, the app is desired to generate scorefile data for use within a soft synth app and the scorefiles usually take the form of long screeds of largely repeated data values which are coded manually- holding back the popularlity of the software no end!!! If a set of pitch values could be saved in a file then these values could be called by an app to gen the score using them as a template. I appreciate this is a fairly non-technical query, but it is becoming a bit of a stumbling block at my end I would really appreciate a hand. I thank you for your time.
If I understand your question correctly : 1) you have files with a bunch of integers in them 2) you want to read these integers into an array 3) (possibly) manipulate the integers 4) (possibly) write the integers out to file again One possible solution : At the top of your CPP file :
#define MAX_RECORDS 1000 // max number of data integers
int array[MAX_RECORDS]; // the array of data integers
int numRecords; // actual number of integers
// in the fileTo bring up a file open dialog and read in the data :
CFileDialog ifile(TRUE,"txt",NULL,OFN_HIDEREADONLY,
"text files|*.txt|all files|*.*||");int result = ifile.DoModal();
if (result == IDOK)
{
CString info;
info = ifile.GetPathName();ifstream infile;
infile.open(info);numRecords = 0;
while (!infile.eof())
{
if (numRecords == MAX_RECORDS) break;
infile >> array[numRecords];
numRecords++;
}
if (infile.eof()) numRecords--;infile.close();
}
to bring up a file save dialog and write the data integers to file :
CFileDialog ofile(FALSE,"txt",NULL,OFN_OVERWRITEPROMPT,
"text files|*.txt|all files|*.*||");int result = ofile.DoModal();
if (result == IDOK)
{CString info;
info = ofile.GetPathName();ofstream outfile;
outfile.open(info);for (int i=0; i<numRecords; i++)
outfile << array[i] << "\n";outfile.close();
}
-
I am a musician who is trying to code an app for a project. I basically need to know how I can take a set of data integers saved in a file of some extension <.txt or whatever> that any user like myself can edit and save using notepad or such, and subsequently utilise these in my program. By way of explanation, the app is desired to generate scorefile data for use within a soft synth app and the scorefiles usually take the form of long screeds of largely repeated data values which are coded manually- holding back the popularlity of the software no end!!! If a set of pitch values could be saved in a file then these values could be called by an app to gen the score using them as a template. I appreciate this is a fairly non-technical query, but it is becoming a bit of a stumbling block at my end I would really appreciate a hand. I thank you for your time.
forgot 1 thing ... at the top of the CPP you also need to add the line :
#include <fstream.h> // for file i/o