i having problem saving a txt file and printing it back out , i have the hints of using fgets(v[i],14,infile); and sscanf(v[i],"%d: %s %s,%x"%line_num[i],inst[i],reg[2],reg2[0]); i have no idea on how to imply this correctly can someone show me a correct implemtation of the code so i can move forward to write the instructions for performing the asm language

text file is as follow:
1: MOV A, 00
2: ADD A, 01
3: CMP A, FF
4: JZ 6
5: JMP 2
6: OUT A
7: NOP

can someone show me the correct code for scanning and printing it out please

`Inline Code Example Here

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define boolean_t int
#define TRUE 1
#define FALSE 0

FILE *infile;

main()
{
      char text[10];
      char a[100];
      char *data="data.txt";
      char *test="test.txt";
      int integer;
      int textcheck,selection1,selection2,repeat;


      while(textcheck==FALSE)
      {
      printf("enter string:");
      scanf("%s",&text);
      if(strcmp(text,data)==0)
      {
       textcheck=TRUE;
       selection1=TRUE;
       }
      else if(strcmp(text,test)==0)
      {
       textcheck=TRUE;
       selection2=TRUE;
       }
       else
       {
       printf("reenter text file name\n");
       textcheck=FALSE;
       }                       
       }//testcheck while  
       if(selection1==TRUE)
       {
        infile = fopen("data.txt", "r");
        if(infile == NULL)
        {
        printf("file missing!");
        }
        else
        {
         while(!feof(infile)) 
         {

          for(integer=0;integer<5;integer++) 
  {
  fscanf(infile, "%c", &a[integer]);
  }
          for(integer=0;integer<5;integer++) 
  {
  printf( "%c", a[integer]);
}


          }                       
        }     
        }//selection 1 
        if(selection2==TRUE)
       {
        infile = fopen("test.txt", "r");
        if(infile == NULL)
        {
        printf("file missing!");
        }
        else
        {
         while(!feof(infile)) 
         {
          fscanf(infile, "%c\n", &a);                   
          }                       
        }     
        }//selection 2

      system("pause");
      }

`

Recommended Answers

All 4 Replies

First: Learn to format your code consitently. It's difficult to follow.
Second: feof() is used incorrectly.
Third: system("pause") is not a good way to pause a program.
Last: scanf("%s",&text); is dangerous. fgets() is much safer.

Since you didn't explain what you are trying to do beyond reading and writing the file, just use fgets() to read each line and write the line where you want.

ading and writing the file, just use fgets() to read ea

i have a problem implying fgets. i keep getting errors after adding fgets. and also after using fgets i have no idea how to put the storage properly since the number at start is the line number and the operation in the line thus that why sscanf is needed but i have no idea how to do it

i keep getting errors after adding fgets.

Nobody can help if you don't post your code or tell us what the errors are.

i have no idea how to put the storage properly since the number at start is the line number and the operation in the line thus that why sscanf is needed but i have no idea how to do it

In my infinite generosity, I'll offer you a basic example that parses the specified file into tokens using fgets() and sscanf() then prints the result accordingly. I also added the capability for including comments and blank lines (which are ignored):

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define COMMENT_START ';'
#define MAX_ARGS 2

void panic(const char *message)
{
    perror(message);
    exit(EXIT_FAILURE);
}

char *skip_ws(char *s)
{
    while (isspace(*s))
        ++s;

    return s;
}

int main(void)
{
    FILE *in = fopen("test.txt", "r");

    if (in) {
        char line[BUFSIZ];

        while (fgets(line, sizeof line, in)) {
            char args[MAX_ARGS + 1][10] = {0};
            char instruction[10];
            char *p = line;
            int lineno, argno;
            int i, n;

            p = skip_ws(p);

            /* Skip blank lines and comments */
            if (*p && *p != COMMENT_START) {
                /* Extract the line number and instruction */
                if (sscanf(p, "%d: %9s%n", &lineno, instruction, &n) != 2)
                    panic("Invalid instruction line");

                printf("Parsing instruction '%s' on line %d\n", instruction, lineno);

                /* Move to the first potential argument */
                p = skip_ws(p + n);

                /* Extract instruction arguments */
                for (argno = 0; argno < MAX_ARGS; ++argno) {
                    if (sscanf(p, "%9[^,\n ]%*c%n", args[argno], &n) != 1)
                        break;

                    p = skip_ws(p + n);

                    /* Check for a comment */
                    if (*p == COMMENT_START) {
                        ++argno;
                        break;
                    }
                }

                if (argno != 0)
                    printf("%d arguments detected:\n", argno);
                else
                    puts("No arguments detected");

                for (i = 0; i < argno; ++i)
                    printf("\tArgument #%d: '%s'\n", i + 1, args[i]);

                putchar('\n');

                /* TODO: At this point you'd check validity of arguments relative to the instruction */

                /* TODO: At this point you'd run the instruction (for an interpreter) */
            }
        }

        fclose(in);
    }

    return 0;
}

Please use this code as a base for education rather than just mindlessly expecting it to fit your needs perfectly.

thank you so much for the example it had helped me greatly already u had help me overcome a huge hurdle and i thank you for it

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.