473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Short program

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[i].Newcrecord.customercode, '\0') ||
strcmp(str_ptr1[i].Newdrecord.customercode, '\0') ||
strcmp(str_ptr1[i].Newirrecord.customercode, '\0'))
{
str_ptr2 = str_ptr1 + 1;//set to next element.

for( i=0; i<loop; i++)
while( strcmp(str_ptr2[i].Newcrecord.customercode, '\0') ||
strcmp(str_ptr2[i].Newdrecord.customercode, '\0'))
{
for(int i=0; i<loop; i++)
if( strcmp( str_ptr1[i].Newirrecord.customercode,
str_ptr2[i].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;

}


Jul 22 '05 #1
3 1424

"JasBascom" <ja*******@aol.com> wrote in message
news:20***************************@mb-m01.aol.com...
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
Jul 22 '05 #2
On 15 Feb 2004 20:52:54 GMT in comp.lang.c++, ja*******@aol.com
(JasBascom) was alleged to have written:
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;
}

Jul 22 '05 #3
On Sun, 15 Feb 2004 21:20:18 GMT in comp.lang.c++, David Harmon
<so****@netcom.com> was alleged to have written:
ifstream sort_file(sorted_file);


Geez, should be ofstream of course.

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

99
by: Glen Herrmannsfeldt | last post by:
I was compiling a program written by someone else about six years ago, and widely distributed at the time. It also includes makefiles for many different systems, so I know it has been compiled...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
1
by: Paul Edwards | last post by:
I have the following C program: #include <stdio.h> int main(void) { unsigned short x = 32768, y=16384; if (x > y) {
7
by: Partho | last post by:
I have a float variable which I need to add to a short variable. How do I do this? Do I have to typecast or is there a way around? I tried typecasting the float to a short, but that gives me a 0 or...
4
by: slougheed | last post by:
I encountered a problem after we had converted our declarations of 'unsigned short int' to uint16_t. In one instance, whoever did the conversion failed to delete the 'short' keyword so we had a...
14
by: CapCity | last post by:
If I have a strig that represents a path, how can I programmatically get the "short" DOS name for it? For example, if I have "C:\Program Files\" I want "C:\Progra~1\". Thanks
2
by: CptDondo | last post by:
I'm trying to write what should be a simple program, and it keeps hanging if I use volatile.... The program, stripped of its error checking, is this: unsigned short * start; unsigned short *...
1
by: lumpybanana247 | last post by:
converting a short file name to a long file name (ex. c:\\program files\\common files\\blah blah blah\\) to (ex. c:\\progra~1\\common~1\\blahbl~1\\) to get a short file name... you get the...
17
by: spasmous | last post by:
I need a way to search through a block of memory for a char array "DA" using a pointer to a short. Ideally I would like to write something like: short *data = ... some data...; int j = 0;...
6
by: Gernot Frisch | last post by:
Hi, the program below workes w/o problems on a GP2X and on the PC, but my PocketPC (using GCC 3.3.3) crashes. Very dissapointing, since I expect some speed boost from it. Thnak you for your...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.