D
Darren
hi,
i'm trying to write some code to generate seperate files with the name of a
call number, and containing the call data, from one big file containing all
data.
but when i run it, i get the individual files created, but just containing
"NOTF: 206791552" and not the data. the right call number is inside the
corresponding file, just no data. i am at a loss - please help!
each call terminiates with a '~'.
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int length;
char * buffer;
char call_num[12];
char fname[13];
int i = 0;
int j = 0;
int k = 0;
int l = 0;
ifstream inputfile ("data.txt");
if (! inputfile.is_open())
{ cout << "Error opening file: data.txt does not exist."; exit (1); }
// get length of file:
inputfile.seekg (0, ios::end);
length = inputfile.tellg();
inputfile.seekg (0, ios::beg);
// allocate memory:
buffer = new char [length];
// read data as a block:
inputfile.read (buffer,length);
inputfile.close();
for (i=0; i < length; i++)
{
if ( (*(buffer+i)=='N') && (*(buffer+(i+1))=='O') &&
(*(buffer+(i+2))=='T') && (*(buffer+(i+3))=='F') )
{
for (k=0; k < 9; k++)
{
call_num[k] = (* (buffer+ (i+(6+k)) ) );
l++;
}
call_num[9] = '.';
call_num[10] = 't';
call_num[11] = 'x';
call_num[12] = 't';
sprintf( fname, call_num);
ofstream outFile (fname);
while (*(buffer+(i+1)) != '~')
{
outFile << buffer;
i++;
}
cout << "Creating file: ";
cout.write (call_num,13);
cout << endl;
j++;
}
}
cout << endl << "Total number of files created = " << j << endl;
return 0;
}
i'm trying to write some code to generate seperate files with the name of a
call number, and containing the call data, from one big file containing all
data.
but when i run it, i get the individual files created, but just containing
"NOTF: 206791552" and not the data. the right call number is inside the
corresponding file, just no data. i am at a loss - please help!
each call terminiates with a '~'.
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int length;
char * buffer;
char call_num[12];
char fname[13];
int i = 0;
int j = 0;
int k = 0;
int l = 0;
ifstream inputfile ("data.txt");
if (! inputfile.is_open())
{ cout << "Error opening file: data.txt does not exist."; exit (1); }
// get length of file:
inputfile.seekg (0, ios::end);
length = inputfile.tellg();
inputfile.seekg (0, ios::beg);
// allocate memory:
buffer = new char [length];
// read data as a block:
inputfile.read (buffer,length);
inputfile.close();
for (i=0; i < length; i++)
{
if ( (*(buffer+i)=='N') && (*(buffer+(i+1))=='O') &&
(*(buffer+(i+2))=='T') && (*(buffer+(i+3))=='F') )
{
for (k=0; k < 9; k++)
{
call_num[k] = (* (buffer+ (i+(6+k)) ) );
l++;
}
call_num[9] = '.';
call_num[10] = 't';
call_num[11] = 'x';
call_num[12] = 't';
sprintf( fname, call_num);
ofstream outFile (fname);
while (*(buffer+(i+1)) != '~')
{
outFile << buffer;
i++;
}
cout << "Creating file: ";
cout.write (call_num,13);
cout << endl;
j++;
}
}
cout << endl << "Total number of files created = " << j << endl;
return 0;
}