File

D

Defected

Hi All,

I have need to write a void function that will merge the contents of two
text file containing chemical elements sorted by atomic number and will
produce a sorted file of binary records. The funcion's parameters will
be three file pointers.
Each text file line contain for example:

11 Sodium Na 22.99
20 Calcium Ca 40.08

The function can assume that one file does not have two copies of the
same element and that the binary output files should have this same
property.
When one of the input files is exhausted, do not forget to copy the
remaining elements of the other input file to the result file.



I have created the function merge that create a binary file with all the
elements, but I don't understand ,how I can make to produce a binary
file without duplicates elements.

Structure

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



void merge(FILE *element_1, FILE *element_2, FILE *element){

chemical_t chemical;
chemical_t chemical_1;
chemical_t chemical_2;

element = fopen("text_b.dat", "wb");
element_1 = fopen("text1.txt", "rb");

fscanf(element_1,"%d%s%s%lf",&chemical_1.atomic_number,
chemical_1.name,
chemical_1.symbol,
&chemical_1.weight);


while(!feof(element_1)){
fwrite(&chemical, sizeof(chemical_t), 1, element);
fscanf(element_1,"%d%s%s%lf",&chemical_1.atomic_number,
chemical_1.name,
chemical_1.symbol,
&chemical_1.weight);

}


fclose(element_1);


element_2 = fopen("text2.txt", "rb");

fscanf(element_2,"%d%s%s%lf",&chemical_2.atomic_number,
chemical_2.name,
chemical_2.symbol,
&chemical_2.weight);


while(!feof(element_2)){
fwrite(&chemical, sizeof(chemical_t), 1, element);
fscanf(element_2,"%d%s%s%lf",&chemical_2.atomic_number,
chemical_2.name,
chemical_2.symbol,
&chemical_2.weight);


fclose(element_2);
fclose(element);

}


Thanks
 
M

mark_bluemel

Defected said:
Hi All,

I have need to write a void function that will merge the contents of two
text file containing chemical elements sorted by atomic number and will
produce a sorted file of binary records. The funcion's parameters will
be three file pointers.
Each text file line contain for example:

11 Sodium Na 22.99
20 Calcium Ca 40.08

The function can assume that one file does not have two copies of the
same element and that the binary output files should have this same
property.
When one of the input files is exhausted, do not forget to copy the
remaining elements of the other input file to the result file.



I have created the function merge that create a binary file with all the
elements, but I don't understand ,how I can make to produce a binary
file without duplicates elements.

You have created a function which should be called "concatenate"...

To merge two files, you read records from both inputs, decide which
record to write to the output, read another file from the appropriate
input, and so on...
 
M

mark_bluemel

You have created a function which should be called "concatenate"...

To merge two files, you read records from both inputs, decide which
record to write to the output, read another file from the appropriate
Naturally that should say "read another record...."
 
F

Fred Kleinschmidt

Don't top-post. I have moved your reply to where it belongs...
(e-mail address removed) ha scritto:
Thanks but how I can make ? I have not idea.

Read a line from file 1 (element A)
Read a line from file 2 (element B)
In a loop:
if elementB is alphabetically before A,
write B
read new element B
else
write A
read new element A
endif
endloop

Remember to add code that handles the end-of-file,
so you don't try to read past it.
 
C

CBFalconer

Defected said:
Thanks but how I can make ? I have not idea.

We don't do homework. We may help after a proper effort. We also
intensely dislike top-posting. Your answer belongs after the
material to which you reply. See the links below.

--
Some informative links:
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)
 
D

Defected

Hello thanks for help,
but I don't understand how i can read, line from file 1 (element A)

because I use this:

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

and I don't know other method :(

Thanks and Best Regards


Fred Kleinschmidt ha scritto:
 
C

Chris Torek

... I use this:

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

As a beginning C programmer, you should never (really, never!) use
feof(). (feof() is for intermediate-level C programmers, who need
to distinguish between "read failed because of EOF" from "read
failed because floppy disk has gone bad".)

The fscanf() function has a return value. Use that.
 

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,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top