Hi,

I'm working on a project where I have a text file that needs to be parsed into assembler instructions. For example: 01364820,8D280000,FFFFFFFF will be parsed as:

ADD $t1, $t1, $s6
LW $t0, 0($t1)

I need help parsing the text file. I thought I would start by first reading the file, which I got the program to do. Then I wanted to write what I read into another file, say, output.txt. I figure once I can figure that out, then I'll work on converting the code to assembler instructions. Two things seem to be giving me problems...(1) how to handle the comma in the comma delimited file so that I have a seperate instruction on individual lines. (2) how to write to a file. Thanks for any help!

#include <stdio.h>
FILE *fp; /*fp is a pointer to a FILE */

main(){
char ch;
fp=fopen("txtfile.txt","r"); /* open the file for reading */

while (fp != NULL){
ch = fgetc(fp);
if (ch == EOF)
break;
else{
putchar(ch);
printf("%c",ch);
}//end else
}
fclose(fp); /* close the file */
}

Recommended Answers

All 2 Replies

May I suggest something more do-able? This just seems WAY over your programming level.

Nevertheless - strtok() is the function most commonly used to "burst" words into a 2D char array, word by word.

I can't say this is anything but rough code, but this will burst up to 120 words from a file, onto your screen. Changing puts() to fputs(filePointer, etc.), will write each word to a file. You need a file pointer made valid by fopen, using "w" mode, instead of "r", as you see in this program. So you'll need to add that, before you can write any words to the file.

Ask back if you have any questions, but I really hope you take on a simpler project.

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

#define SIZE 120

int main ()
{

  FILE* fp;
  char str[SIZE][SIZE] = {{'\0'}};
  char* filename = "words.txt";
  char test[SIZE];
  char *ptr;
  int i, len;
  int r=0;
  printf("\n\n");

  if (!(fp = fopen(filename,"r"))) { 
    printf("\nCannot open %s for output.\n",filename);
    return 1;
  }
  ptr = NULL;
  len = r = 0;
  while(fgets(test,SIZE,fp) != NULL) {
    ptr=strtok(test, " ");  //handles the first word in the buffer
    if(ptr) {
      strcpy(str[r], ptr);
      len=strlen(str[r]);
      if(str[r][len-1]=='\n')
        str[r][len-1]='\0';
      ++r;
    } 
    while(ptr) {
      ptr = strtok(NULL, " ");  //handles subsequent words in the buffer
      if(ptr) {
        strcpy(str[r], ptr);
        len=strlen(str[r]);
        if(str[r][len-1]=='\n')
          str[r][len-1]='\0';
        ++r;
      }
    }
  }
  i=0;
  while(i<r) {                 //displays one word per row, on stdout.
    if(i % 20 == 0 && i) { 
      puts("\n\n        press enter to display the next set of words");
      (void) getchar();
    }
    printf("\n %d: %s ", i, str[i++]);
  }

  fclose(fp);
  return 0;
}

Once you compile this, you can re-direct output from the exe file, to any file you want, without code modifications. Run it from a consol (terminal) window, and use the > pipe:

programName >filenameYouWant.txt

will be your command line.

Hi,
Thanks for the input. I'm trying to understand exactly what you are doing. Would you mind answering a couple of questions?

(1) for the following line, are you declaring str as a double array? And SIZE indicates that it can be a
variable length? And I forgot to mention that the 'FFFFFFFF' is considered to be the end of file.

char str[SIZE][SIZE] = {{'\0'}};

(2) Why do you need one while loop to handle the first word and a second while loop to handle subsequent words? I know I have a lot to learn, thanks for being patient...

I actually understand some assembly, so I'm not that bad off. I plan on taking the 8-bit hex instruction and convert it to the 32-bit binary instruction. I know that the first 6 bits of the instruction will be the MIPS op code. Which will let me know what type of assembler instruction it is. And my project will only use ADD, ADDI, BEQ, BNE, JUMP, LW, and SW. What is actually holding me back is the fact that i've never written a program in c before :-(

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.