470,819 Members | 1,629 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,819 developers. It's quick & easy.

File

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_nu mber,
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_nu mber,
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_nu mber,
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_nu mber,
chemical_2.name,
chemical_2.symbol,
&chemical_2.weight);
fclose(element_2);
fclose(element);

}
Thanks
Jan 23 '07 #1
7 1901

Defected wrote:
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...

Jan 23 '07 #2

mark_blue...@pobox.com wrote:
Defected wrote:
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
Naturally that should say "read another record...."
input, and so on...
Jan 23 '07 #3
Thanks but how I can make ? I have not idea.
ma**********@pobox.com ha scritto:
Defected wrote:
>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...
Jan 23 '07 #4

Don't top-post. I have moved your reply to where it belongs...
ma**********@pobox.com ha scritto:
>Defected wrote:
>>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...
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.

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project

Jan 23 '07 #5
Defected wrote:
>
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)
Jan 23 '07 #6
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_nu mber,
chemical_1.name,
chemical_1.symbol,
&chemical_1.weight);
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);
}

and I don't know other method :(

Thanks and Best Regards
Fred Kleinschmidt ha scritto:
Don't top-post. I have moved your reply to where it belongs...
>ma**********@pobox.com ha scritto:
>>Defected wrote:
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...
>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.
Jan 23 '07 #7
In article <Z0******************@twister2.libero.it>
Defected <de**********@gmail.comwrote:
>... I use this:

fscanf(element_1,"%d%s%s%lf",&chemical_1.atomic_n umber,
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.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jan 23 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by matt | last post: by
7 posts views Thread by Joseph | last post: by
3 posts views Thread by StGo | last post: by
3 posts views Thread by Shapper | last post: by
reply views Thread by thjwong | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.