Merge File without copies

D

Defected

Hi All,

I have problem whit this program, because the output file print copies
but only at the end of file, and the file does not have two copies of
the same element.

Example of elements in the two file:

11 Sodium Na 22.99
20 Calcium Ca 40.08

------------------------------------------------------------------------
Structure

typedef struct{
int atomic_number;
char name[MAX];
char symbol[MAX];
double weight;
}chemical_t;

------------------------------------------------------------------------

while(!feof(element_1)){
fscanf(element_1,"%d%s%s%lf",&chemical_1.atomic_number,
chemical_1.name,
chemical_1.symbol,
&chemical_1.weight);
while(!feof(element_2)){
fscanf(element_2,"%d%s%s%lf",&chemical_2.atomic_number,,
chemical_2.name,
chemical_2.symbol,
&chemical_2.weight);

for(i = 0; i < el_1; i++){
if(chemical_1.atomic_number < chemical_2.atomic_number){
fwrite(&chemical_1, sizeof(chimico_t), 1, element);
fscanf(element_1,"%d%s%s%lf",&chemical_1.atomic_number,
chemical_1.name,
chemical_1.symbol,
&chemical_1.weight);
}

else{
for(j = 0; j < el_2; j++){
fwrite(&chemical_2, sizeof(chimico_t), 1, element);
fscanf(element_2,"%d%s%s%lf",&chemical_2.atomic_number,,
chemical_2.name,
chemical_2.symbol,
&chemical_2.weight);

}//end for
}//end else
}//end for
}//end while
}//end while


Thanks
 
J

Jens Thoms Toerring

Defected said:
I have problem whit this program, because the output file print copies
but only at the end of file, and the file does not have two copies of
the same element.

Sorry, parse error here. Can you restate that in some way that makes
a bit more sense?
Example of elements in the two file:
11 Sodium Na 22.99
20 Calcium Ca 40.08
typedef struct{
int atomic_number;
char name[MAX];
char symbol[MAX];
double weight;
}chemical_t;
------------------------------------------------------------------------

while(!feof(element_1)){

First problem: you can use feof() only reasonably _after_ you have
read from a file - it's there to tell you if you hit EOF during the
read, it can't predict if the next read will lead to hitting EOF.
fscanf(element_1,"%d%s%s%lf",&chemical_1.atomic_number,
chemical_1.name,
chemical_1.symbol,
&chemical_1.weight);
while(!feof(element_2)){
fscanf(element_2,"%d%s%s%lf",&chemical_2.atomic_number,,
chemical_2.name,
chemical_2.symbol,
&chemical_2.weight);
for(i = 0; i < el_1; i++){

What are 'el_1' and 'el_2'? And why don't you worry about reaching
EOF here?
if(chemical_1.atomic_number < chemical_2.atomic_number){
fwrite(&chemical_1, sizeof(chimico_t), 1, element);
fscanf(element_1,"%d%s%s%lf",&chemical_1.atomic_number,
chemical_1.name,
chemical_1.symbol,
&chemical_1.weight);
}
else{
for(j = 0; j < el_2; j++){
fwrite(&chemical_2, sizeof(chimico_t), 1, element);
fscanf(element_2,"%d%s%s%lf",&chemical_2.atomic_number,,
chemical_2.name,
chemical_2.symbol,
&chemical_2.weight);

I actually can't figure out what this is supposed to be doing.
But if you don't want any doubles in the output file and have
all elements sorted you will have to read in all elements from
both files, eliminate repetitions and sort before writing them
out to the output file.

Or is there something about the input files you didn't tell, e.g.
that they are already sorted by atom numbers in ascending order?
In that case things would be quite a bit simpler. You would need
something like the following pseudo-code (e1 and e2 stand for the
elements you read from the first and second file and f1 and f2
for the first and second file):

1: read e1 from f1
IF read failed because of EOF of f1 {
copy_rest_of( f2 )
DONE
}

read e2 from f2
IF read failed because of EOF of f2 {
copy_rest_of( f1 )
DONE
}

2: IF e1 == e2 {
write out e1 /* write out e2 would also do */
GOTO 1
} ELSE IF e1 < e2 {
write out e1
read new e1 from f1
IF read failed because of EOF of f1 {
write out e2
copy_rest_of( f2 )
DONE
}
} ELSE {
write out e2
read new e2 from f2
IF read failed because of EOF of f2 {
write out e1
copy_rest_of( f1 )
DONE
}
}
GOTO 2

where copy_rest_of() is a function or some code that just copies
everything not yet read from an input file straight to the output
file (and 'DONE' stands for "get out of this function"). Of course,
things can be a bit re-ordered (and don't use goto's unless there
isn't a very good reason;-), the pseudo-code is just for pointing
out the basic logic you would need with already sorted input files.

Regards, Jens
 
D

Default User

Defected said:
Hi All,

I have problem whit this program, because the output file print
copies but only at the end of file, and the file does not have two
copies of the same element.

This is not a program, it's a piece of one. Post a complete, minimal
program that demonstrates the problem. Also, thoroughly explain what
you expected the program to do, and what it did instead.



Brian
 
J

Jens Thoms Toerring

Seems more like an error with the semantic analyzer - the grammar is
passable.

You're right;-) Even "whit" seems to be a valid token as my dictionary
tells me...
Regards, Jens
 
S

santosh

Defected said:
Hi All,

I have problem whit this program, because the output file print copies
but only at the end of file, and the file does not have two copies of
the same element.

I suggest breaking down each task to a seperate callable function. Even
if it's overkill, it'll help you and others to understand your code's
flow of execution more easily. Also errors can be easily isolated.

I also suggest putting in a few printf()'s and assert()'s to check the
runtime behaviour. Any wrong value or condition can be identified right
upto the source line number. Use the __LINE__ macro with printf().

Also, in future, post a minimal compilable example that demostrates
your problem. It enables others to run your source through their
compilers, lint or whatever and minimise the time needed to tediously
read through the whole source.

Snippets of source code are almost always, very difficult to diagnose
or comment about.

Also see this FAQ:
<http://c-faq.com/stdio/index.html>

Many of the FAQs on that page may be relevant to your program's
problems, like 12.2, 12.41 etc.
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top