hi, i can't seem to a.out the program for some reason. When i type in a.out, the cursor just stays there. I think it goes in a infinite loop.
I am trying to read two text files and then putting the atomic number in order. what seems to be the problem?

txt
1 Hydrogen H 1.01
1 Hydrogen H 1.01
1 Hydrogen H 1.01
50 Tin Sn 118.69
84 Polonium Po 209
88 Radium Ra 226.03

txt
1 Hydrogen H 1.01
1 Hydrogen H 1.01
1 Hydrogen H 1.01
1 Hydrogen H 1.01
1 Hydrogen H 1.01
1 Hydrogen H 1.01
11 Sodium Na 22.99
20 Calcium Ca 40.08
84 Polonium Po 209.00

#include<stdio.h>
#include<math.h>
#include<string.h>

void merge_chem(FILE * inp1, FILE* inp2, FILE* outp);

int main(void)
{
  FILE *inp1, *inp2, *outp;
  inp1 = fopen("chem1.txt", "r");
  inp2 = fopen("chem2.txt", "r");
  outp = fopen("chem.out", "w");

  merge_chem(inp1,inp2,outp);

  fclose(inp1);
  fclose(inp2);
 fclose(outp);
        return 0;
}
void merge_chem(FILE * inp1, FILE* inp2, FILE* outp)
{
  int atomic_num, atomic_num2;
  int  i = 0, j = 0, m =0, status, status2, count =1;
  double weight, weight2;
  char name[40], name2[40], symbol[40], symbol2[40];


    status = fscanf(inp1, "%d%s%s%lf", &atomic_num, name, symbol, &weight);
    status2 = fscanf(inp2, "%d%s%s%lf", &atomic_num2, name2, symbol2, &weight2);

   while(status != EOF || status2 != EOF)
  {
    count = 1;
    while(count)
    {

    if(atomic_num < atomic_num2)
    {
    fprintf(outp,"%d  %s  %s  %.2f\n", atomic_num, name, symbol, weight);
    count =0;
    }
    else if(atomic_num > atomic_num2)
    {
    fprintf(outp,"%d  %s  %s  %.2f\n", atomic_num2, name2, symbol2, weight2);
    status2 = fscanf(inp2, "%d%s%s%lf", &atomic_num2, name2, symbol2, &weight2);
    }
    else
    {
    fprintf(outp,"%d  %s  %s  %.2f\n", atomic_num, name, symbol, weight);
    status2 = fscanf(inp2, "%d%s%s%lf", &atomic_num2, name2, symbol2, &weight2);
     }

    }

    status = fscanf(inp1, "%d%s%s%lf", &atomic_num, name, symbol, &weight);
   }
}

Recommended Answers

All 8 Replies

a.out

What is that?

Use code tags & re-post the code along with the errors you are getting..

It's probably in your while (status != EOF || status2 != EOF) statement.

That || should be an && .

Oh yeah, don't int main(void) .
Always use either:
1. int main() 2. int main( int argc, char *argv[] ) Main takes arguments. If you want to ignore them, use form 1 (which says to the compiler: "I don't care about any arguments this function takes.")

Jishnu
"a.out" is the default executable name that compilers produce on *nix systems. On Windows you compile "foo.c" and get "foo.exe". On Unix you compile and get "a.out".

Ah, I have learned something. (Further proof that C99 is weird...)

What is that? [a.out]

The default name of the executable when using *nix compilers is a.out. Don't know why but probably just the custom by now.

When the only tool for writing code was the assembler, the default output file was a.out
As in "assembler.out" without all the verbage.
The tools evolved (like linkers, compilers and librarians), but the name stuck.

I would also check if opening the file(s) was a succes or not. This will spare you the memory exceptions in the future.

Niek

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.