473,386 Members | 1,736 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.

Merge File without copies

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
5 2723
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andy Davis | last post by:
I have set up a mail merge document in Word 2003 which gets its data from my Access 2000 database. I want to set up a button on a form that: 1. runs the query to provide the dat for the merge...
14
by: Michael R. Copeland | last post by:
I'm writing an application that requires an "intelligent merge" of 2 files. That is, equal data has a "preferred source" that I want to write out. What I have works, I believe, but it seems...
0
by: Shiraz | last post by:
Hi I have a question regarding the functionality of merge modules. Since this relates to my previous queries, I'll just give you a brief background on the topic. I had to make an installer for...
8
by: mesterak | last post by:
I'm trying to write a text log file processor but am having significant performance issues. * On average, there are about 100-200 files to process each file being about 1MB in size. * Typically...
1
by: mjobbe | last post by:
I have an installer that requires three merge modules (ATL, CRT, and MFC), and after adding them in, I get the following warnings when I build the MSI: WARNING: Two or more objects have the same...
7
by: Defected | last post by:
Thanks all for help, I have tried this code but it's don't work. -------------------------------------------------------------------- Structure typedef struct{ int atomic_number; char...
4
by: Tom Jones | last post by:
I have an application that was originally built using Visual Studio 2003 that I upgraded to Visual Studio 2005. When I attempt to build the *.msi file in the deployment project, I am getting a...
5
by: Mark Chambers | last post by:
Hi there, Can anyone explain the following (very) simple scenario. 1) I make an exact copy of my "DataSet" and delete one record from a given table (in the copy) 2) I invoke...
7
by: giladp1 | last post by:
I found Albert Kallal's great "Super easy Word Merge" code in his site at: http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html Thanks Albert so much for sharing this. I am looking...
1
by: Esther Lane | last post by:
Hello! First off, many many thanks to Albert who wrote the Mail Merge code for MS Access I am using. It has been working beautifully for a few years. However, my client just (without notice!)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.