By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,813 Members | 1,236 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

Merge File without copies

P: n/a
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_nu mber,
chemical_1.name,
chemical_1.symbol,
&chemical_1.weight);
while(!feof(element_2)){
fscanf(element_2,"%d%s%s%lf",&chemical_2.atomic_nu mber,,
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_nu mber,
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_nu mber,,
chemical_2.name,
chemical_2.symbol,
&chemical_2.weight);

}//end for
}//end else
}//end for
}//end while
}//end while
Thanks
Jan 24 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Defected <de**********@gmail.comwrote:
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
------------------------------------------------------------------------
Structure
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_nu mber,
chemical_1.name,
chemical_1.symbol,
&chemical_1.weight);
while(!feof(element_2)){
fscanf(element_2,"%d%s%s%lf",&chemical_2.atomic_nu mber,,
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_nu mber,
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_nu mber,,
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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jan 24 '07 #2

P: n/a
Defected wrote:
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
Jan 25 '07 #3

P: n/a
Jens Thoms Toerring <jt@toerring.dewrote:
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.
Seems more like an error with the semantic analyzer - the grammar is
passable.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jan 25 '07 #4

P: n/a
Christopher Benson-Manica <at***@norge.freeshell.orgwrote:
Jens Thoms Toerring <jt@toerring.dewrote:
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.
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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jan 25 '07 #5

P: n/a
Defected wrote:
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.

Jan 25 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.