J
JasBascom
I have written a program that is suppose to compare to lines
c12345
I04567
the program takes the first digit encountered of both lines, decides which one
is smaller and writes that to a file called sorted data. the problem is that
that file (a text file) won't create. Can you tell me what if anything i'm
doing wrong in the program, and whether i can force the program to create the
file (sorted data)
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
struct crecord {
char customercode[5];
char customername[21];
char customeraddress[61];
char customerbalance;
char creditlimit;
int Totalbalance;
int Totalcreditlimit;
};
struct irrecord {
char customercode[5];
char partnum[6];
char issue_rec[5];
};
struct drecord {
char customercode[5];
};
int loop = 200;
long offset = 1;
union Allrecords{
struct crecord Newcrecord;
struct irrecord Newirrecord;
struct drecord Newdrecord;
};
union Allrecords unionarray;
void sort_function( union Allrecords unionarray, ifstream& validdata, char*
temp2 )
{
union Allrecords *str_ptr1, *str_ptr2, tempstr;
for(int i =0; i< loop; i++)
while( strcmp(str_ptr1.Newcrecord.customercode, '\0') ||
strcmp(str_ptr1.Newdrecord.customercode, '\0') ||
strcmp(str_ptr1.Newirrecord.customercode, '\0'))
{
str_ptr2 = str_ptr1 + 1;//set to next element.
for( i=0; i<loop; i++)
while( strcmp(str_ptr2.Newcrecord.customercode, '\0') ||
strcmp(str_ptr2.Newdrecord.customercode, '\0'))
{
for(int i=0; i<loop; i++)
if( strcmp( str_ptr1.Newirrecord.customercode,
str_ptr2.Newirrecord.customercode + 1))
{
tempstr = *str_ptr1;
*str_ptr1 = *str_ptr2;
*str_ptr2 = tempstr;
}
*str_ptr1++;//incremented, so that the same code isn't sorted again
}
str_ptr2++;
}
}
int main()
{
const char sorted_file[] = "A:\\514650P2SD.txt";
const char outfile[] = "A:\\514650VDP1.bin";
union Allrecords unionarray;
char* characterarray;
long offset = 1, end_of_file = 0;
int index = 0;
ifstream sort_file;
ifstream validdata;
sort_file.open("A:\\514650P2SD.txt", ios::in);
if(!sort_file)
{
cout<<"Cannot create file"<< endl;
return EXIT_FAILURE;
}
validdata.open("A:\\514650VDP1.bin", ios::in || ios::binary);
if(!validdata)
{
cout<<" Cannot find file"<<endl;
return EXIT_FAILURE;
}
validdata.seekg(0,ios::end);
end_of_file = validdata.tellg();
while(offset <= end_of_file)
{
validdata.seekg(-offset, ios::end);
characterarray[index++] = validdata.get();
offset++;
}
while(sort_file.peek() != EOF)//read the whole file.
{
sort_file.getline( characterarray, sizeof(characterarray[index]) );
switch(characterarray[4])
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
sort_function(unionarray, validdata, characterarray);
default:;
}
}
return 0;
}
c12345
I04567
the program takes the first digit encountered of both lines, decides which one
is smaller and writes that to a file called sorted data. the problem is that
that file (a text file) won't create. Can you tell me what if anything i'm
doing wrong in the program, and whether i can force the program to create the
file (sorted data)
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
struct crecord {
char customercode[5];
char customername[21];
char customeraddress[61];
char customerbalance;
char creditlimit;
int Totalbalance;
int Totalcreditlimit;
};
struct irrecord {
char customercode[5];
char partnum[6];
char issue_rec[5];
};
struct drecord {
char customercode[5];
};
int loop = 200;
long offset = 1;
union Allrecords{
struct crecord Newcrecord;
struct irrecord Newirrecord;
struct drecord Newdrecord;
};
union Allrecords unionarray;
void sort_function( union Allrecords unionarray, ifstream& validdata, char*
temp2 )
{
union Allrecords *str_ptr1, *str_ptr2, tempstr;
for(int i =0; i< loop; i++)
while( strcmp(str_ptr1.Newcrecord.customercode, '\0') ||
strcmp(str_ptr1.Newdrecord.customercode, '\0') ||
strcmp(str_ptr1.Newirrecord.customercode, '\0'))
{
str_ptr2 = str_ptr1 + 1;//set to next element.
for( i=0; i<loop; i++)
while( strcmp(str_ptr2.Newcrecord.customercode, '\0') ||
strcmp(str_ptr2.Newdrecord.customercode, '\0'))
{
for(int i=0; i<loop; i++)
if( strcmp( str_ptr1.Newirrecord.customercode,
str_ptr2.Newirrecord.customercode + 1))
{
tempstr = *str_ptr1;
*str_ptr1 = *str_ptr2;
*str_ptr2 = tempstr;
}
*str_ptr1++;//incremented, so that the same code isn't sorted again
}
str_ptr2++;
}
}
int main()
{
const char sorted_file[] = "A:\\514650P2SD.txt";
const char outfile[] = "A:\\514650VDP1.bin";
union Allrecords unionarray;
char* characterarray;
long offset = 1, end_of_file = 0;
int index = 0;
ifstream sort_file;
ifstream validdata;
sort_file.open("A:\\514650P2SD.txt", ios::in);
if(!sort_file)
{
cout<<"Cannot create file"<< endl;
return EXIT_FAILURE;
}
validdata.open("A:\\514650VDP1.bin", ios::in || ios::binary);
if(!validdata)
{
cout<<" Cannot find file"<<endl;
return EXIT_FAILURE;
}
validdata.seekg(0,ios::end);
end_of_file = validdata.tellg();
while(offset <= end_of_file)
{
validdata.seekg(-offset, ios::end);
characterarray[index++] = validdata.get();
offset++;
}
while(sort_file.peek() != EOF)//read the whole file.
{
sort_file.getline( characterarray, sizeof(characterarray[index]) );
switch(characterarray[4])
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
sort_function(unionarray, validdata, characterarray);
default:;
}
}
return 0;
}