Short program

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;

}
 
J

John Harrison

JasBascom said:
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.

Here you say that you want to write 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)


[snip]

ifstream sort_file;

Here you declare the sorted file for input.
ifstream validdata;


sort_file.open("A:\\514650P2SD.txt", ios::in);

And here you open it for reading.

[snip]
while(sort_file.peek() != EOF)//read the whole file.
{
sort_file.getline( characterarray, sizeof(characterarray[index]) );

And here you start reading from it.

See the problem? You say you want to create/write the sorted file, but all
the code ever does is read. You need to decide what you actually want to do
and adjust the code.

Although you call this a short program, it looks a little complex for you to
handle. I would strongly recommend breaking it down and trying to do
something simpler. Its much easier to take a working program and add things
to it, than it is to make a complex but broken program work. Trust me on
this, you'll save yourself hours of frustration.

john
 
D

David Harmon

const char sorted_file[] = "A:\\514650P2SD.txt"; []
ifstream sort_file; []
sort_file.open("A:\\514650P2SD.txt", ios::in);
if(!sort_file)
{
cout<<"Cannot create file"<< endl;
return EXIT_FAILURE;
}

You are sure that the filesystem on A: can support 10-char file names?
Just to check, try something shorter.

Suggested changes:

const char sorted_file[] = "A:\\514650P2SD.txt";
ifstream sort_file(sorted_file);
if(!sort_file)
{
perror(sorted_file);
return EXIT_FAILURE;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

std::sort whole program 2
unions 2
built in types 4
function error 6
read problem 6
Access violation error 10
type conversion 5
Reading a file 1

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top