I need help on how to properly combine two programs into a single program.

This is the first program:

int main()
{
  FILE *input, *output;
  char doc;
  input = fopen("input3.txt","r");
  output = fopen("output.txt","w");
  while(!feof(input))
  {
    doc=fgetc(input);
    if(doc==EOF) break;
    else
    {
      if(doc==';')
      {
        while(!feof(input))
        {
          doc=fgetc(input);
          if(doc=='\n')
          goto out;
        }
      }
    }
    out: fputc(doc, output);
  }
  fclose(input);
  fclose(output);
  return 0; 
}

This removes comments

This is the second program:

int main ()
{
  FILE *input, *output;
  input = fopen("output3.txt","r");
  output = fopen("output4.txt","w");
  char cmd[5], op[20], temp;
  int x;
  while(!feof(input))
  {
      fscanf(input, "%s %s", &cmd, &op); //no, str, cmd, operand
      temp = fgetc(input);    
      char *search=",";
      char *op1=strtok(op, search);
      char *op2=strtok(NULL,search);
      char *op3=strtok(NULL,search);
        if(op3!=NULL)
        {
          fprintf(output, "%s\n\n%s\n,\n%s\n,\n%s\n", cmd, op, op2, op3);
        }
        else if(op2!=NULL)
        {
          fprintf(output, "%s\n\n%s\n,\n%s\n", cmd, op, op2);
        }
        else fprintf(output, "%s\n\n%s\n", cmd, op);
  }
  fclose(input);
  fclose(output); 
  return 0;
}

This breaks down the input

I tried it using this

int main()
{
    FILE *src, *noc, *brk;
    char doc;
    char no[5], str[10], cmd[5], op[50];
    int x;
    src = fopen("lexer.asm","r");
    noc = fopen("nocomment.noc","w+");
    brk = fopen("breakdown.brk","w");
    //Removes comments
    while(!feof(src))
    {
        doc=fgetc(src);
        if(doc==EOF) break;
        else
        {
            if(doc==';')
            {
                while(!feof(src))
                {
                    doc=fgetc(src);
                    if(doc=='\n')
                    goto out;
                }
            }
        }
        out: fputc(doc, nocw);
    }
    //Breakdown
    while(!feof(noc))
    {
        fscanf(noc, "%s %s %s %s", &no, &str, &cmd, &op); 
        char *search=",";
        char *op1=strtok(op, search);
        char *op2=strtok(NULL,search);
        char *op3=strtok(NULL,search);
        if(op3!=NULL)
        {
            fprintf(brk, "%s\n\n%s\n,\n%s\n,\n%s\n", cmd, op, op2, op3);
        }
        else if(op2!=NULL)
        {
            fprintf(brk, "%s\n\n%s\n,\n%s\n", cmd, op, op2);
        }
        else fprintf(brk, "%s\n\n%s\n", cmd, op);
    }
    fclose(src);
    fclose(noc);
    fclose(brk);
    return 0;
}

However using this solution, it only removes the comments, and doesn't do the second program.

My input in this program is:
1 byte MOV a,b ;comment
2 bytes SUB b,a ;comment
3 bytes HHH ga,me ;comment

Can anyone suggest anything to help me combine the two programs.

Recommended Answers

All 14 Replies

instead of everything writing in to main use function to write every thing then call those function from main.
their could be only one main method, hence in another file just have functions and include that in to the file having main() function. then call function of that file from main();

How do I do that exactly? I get what your saying, put I don't know how to call functions outside of main.

I did the functions outside of main, and I got this. Can anyone tell me what's wrong with the code?

char nocomment( );
char breakdown( );

char nocomment() //Removes comments
{
    FILE *src, *noc;
    char doc;
    src = fopen("lexer.asm","r");
    noc = fopen("nocomment.noc","w");
    doc=fgetc(src);
    while(!feof(src))
    {
        if(doc==EOF)break;
        else
        {
            if(doc==';')
            {
                while(!feof(src))
                {
                    doc=fgetc(src);
                    if(doc=='\n')
                    goto out;
                }
            }
        }
        out: fputc(doc, noc);
    }
}

char breakdown() //Breakdown
{
     FILE *noc, *brk;
     noc = fopen("nocomment.noc","r");
     brk = fopen("breakdown.brk","w");
     char no[5], str[10], cmd[5], op[50], temp;
     fscanf(noc, "%s %s %s %s", &no, &str, &cmd, &op); //no, str, cmd, operand
     char *search=",";
     char *op1=strtok(op, search);
     char *op2=strtok(NULL,search);
     char *op3=strtok(NULL,search);
     if(op3!=NULL)
     {
        fprintf(brk, "%s\n\n%s\n,\n%s\n,\n%s\n", cmd, op, op2, op3);
     }
     else if(op2!=NULL)
     {
        fprintf(brk, "%s\n\n%s\n,\n%s\n", cmd, op, op2);
     }
     else fprintf(brk, "%s\n\n%s\n", cmd, op);
}

int main()
{
    nocomment();
    breakdown();
    return 0;
}

>Can anyone tell me what's wrong with the code?
Why don't you tell us what doesn't work?

Oh, sorry about that. When I run the program, it doesn't close at return 0, and doesn't show a proper output.

You don't appear to be closing your files. That could be part of the problem. Also, both your functions have been defined as returning a value of type char , but neither returns anything -- why?

I tried it and it still doesn't work. The nocomment part of the program keeps increasing the size of the file, the longer the program runs.

Uh, this is my first time using functions like these so I have little clue on what I'm doing. Can I replace it with void instead?

Perhaps a little more practice is required with easier concepts, before attending such modifications?

Went through the first function step by step. Look at the logic written in comments.

char nocomment() /* void nocomment() since there's no return by this function */
{
    FILE *src, *noc; /* two pointers to files */
    char doc;		 /* a character hold place, needs to be int, fgetc might return EOF */

    src = fopen("lexer.asm","r");   /* open lexer.asm for reading, what if can't open it? */

    noc = fopen("nocomment.noc","w"); /* open noc for writing, what if it can't open? */

    doc=fgetc(src); /* read a character from src, doc needs to be an int */

    while(!feof(src)) /* loop through the file until feof encounters EOF. Not a good idea */

    {
        if(doc==EOF)break; /* doc can't hold EOF because that's an integer */
        else /* other wise */
        {
            if(doc==';') /* if is a ; */
            {
                while(!feof(src)) /* start going through the file again, bad idea */
                {
                    doc=fgetc(src); /* whatever was in doc gets overwritten again */
                    if(doc=='\n')   /* When a newline appears jump to out and write a newline to noc */
                    goto out;
                }
            }
        }
        out: fputc(doc, noc); /* write to noc what is in doc */
    }
	
}

Is it possible if you tell me the things I should do to make it work or suggest a much easier method?

A minor note ... I think that actually

if(doc==EOF)break;

is OK since the compiler is to promote the char to int for the comparison.

A comment on that: default char may be unsigned. If EOF is negative, do you suppose a nonnegative value will ever be equal?

commented: Ctrl + Z will become a feature. +7

I remade the program, and it finally gives out an output, however, it seems that the noc file is being read, while it is being written to. By this I mean that when it's still removing the comments in the form of a ; it already breaks it down to parts. Can anyone suggest something to fix this error?

Here's the code:

int main()
{
    FILE *src, *noc, *brk;
    char no[5], str[10], cmd[5], op[20], com[100], temp, doc;
    int x=0;
    src = fopen("lexer.asm","r"); //Removes comments
    noc = fopen("nocomment.noc","w");
    while(!feof(src))
    {
        doc=fgetc(src);
        if(doc==EOF) break;
        else
        {
            if(doc==';')
            {
                while(!feof(src))
                {
                    doc=fgetc(src);
                    if(doc=='\n')
                    goto out;
                }
            }
        }
        out: fputc(doc, noc);
    }
    fclose(src);
    fclose(noc);
    
    src = fopen("nocomment.noc","r"); //Breakdown
    brk = fopen("breakdown.brk","w");
    while(!feof(src))
    {
        fscanf(src, "%s %s %s %s", &no, &str, &cmd, &op);
        temp = fgetc(src);    
        char *search=",";
        char *op1=strtok(op, search);
        char *op2=strtok(NULL,search);
        char *op3=strtok(NULL,search);
        if(temp == EOF) break;
        if(op3!=NULL)
        {
            fprintf(brk, "%s\n\n%s\n,\n%s\n,\n%s\n", cmd, op, op2, op3);
        }
        else if(op2!=NULL)
        {
            fprintf(brk, "%s\n\n%s\n,\n%s\n", cmd, op, op2);
        }
        else fprintf(brk, "%s\n\n%s\n", cmd, op);   
    }
    fclose(src);
    fclose(brk);
    return 0;
fscanf(src, "%s %s %s %s", &no, &str, &cmd, &op); /* remove the & in front of all of the variables */

All of them are strings ( array of chars ) and by that nature the address of the first element are passed already to fscanf.
The same behavior is encounter with any function of the families: printf and scanf.

temp = fgetc(src);

functions like fgetc and getchar might return an EOF if unsuccessful. Therefore the recipient must be an int type and not a char.

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.