I am trying to make a compiler for my project using C but I have a problem on how to store the contents of the text file into an array or something similar so that my program can compiler or analyze my txt (code) file. Here is what I have so far in my main excluding the other functions (to make the code shorter here):

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

char *prog;  /* holds expression to be analyzed */
const char file_name[] = "c:\\test1.txt";

main()
{
    FILE *in_file;     /* input file */

    /* character or EOF flag from input */
    int *ch;

    clrscr();

    in_file = fopen(file_name, "r");
    if ( in_file == NULL ) {
        printf("Cannot open %s\n", file_name);
        exit(0);
    }

    while (1) {
        ch = fgetc(in_file);
        if ( ch == EOF )
            break;
        ch++;
    }

    fclose(in_file);

    /* this is the part where I want to store it in prog because I will be using prog in my other functions. */
}

What kind of data is in the file, how is it laid out, and do you want to store it as char, int, float etc?

I'm going to guess from your code above that you want it in an array of int.

Do you know how many int you will be reading in advance, or will that have to be worked out as you go?

Edited 3 Years Ago by Moschops

At its simplest you'd have something like this just to store the characters:

#include <stdio.h>

#define EXPR_MAX 255

int main(void)
{
    FILE *in = fopen("c:\\test1.txt", "r");

    if (in) {
        char expr[EXPR_MAX];
        size_t n = 0;
        int ch;

        while ((ch = getc(in)) != EOF) {
            expr[n++] = (char)ch;
        }

        fclose(in);

        /* Now n characters are stored in expr and can be processed in memory */
    }

    return 0;
}

@Moschops

I want everything to be stored as characters including the spaces and numbers. This is what's inside my test1.txt file:

VAR a AS INT
START
   a=1
   OUTPUT: "a = " & a
STOP

As I mentioned earlier, I am making a compiler using C.

@deceptikon

Based on your code, how do I store it in prog based on my code? I wanted to include in my main for example:

prog = (contents of the text file);

The code below is how my main looks like including the functions.

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

char *prog;  /* holds expression to be analyzed */
const char file_name[] = "c:\\test1.txt";

int gvar_index; /* index into global variable table */
int strflag;
int stpflag;

void scan_labels(void);
void enterblock(void);
void serror(int);

main()
{
    strflag=0; /* set start flag to zero */
    stpflag=0; /* set stop flag to zero */

    gvar_index = 0; /* initialize global variable index */

    FILE *in_file;     /* input file */

    /* character or EOF flag from input */
    int *ch;

    clrscr();

    in_file = fopen(file_name, "r");
    if ( in_file == NULL ) {
        printf("Cannot open %s\n", file_name);
        exit(0);
    }

    while (1) {
        ch = fgetc(in_file);
        if ( ch == EOF )
            break;
        ch++;
    }

    fclose(in_file);

    /* prog = (contents of the txt file); */

    scan_labels(); /* find the labels in the txt file */

    if (strflag!=stpflag) {
        if (strflag > stpflag)
            serror(16);
        else
            serror(15);
    }

    enterblock();
}

Based on your code, how do I store it in prog based on my code?

Honestly, I don't think you're ready for the headache of managing dynamic memory. That's why my example used an array rather than a pointer. And since this is school work, it's unlikely that you won't be able to get away with an array that's "big enough" for any reasonable test data.

On a side note, this is really bothering me:

/* character or EOF flag from input */
int *ch;

Remove the asterisk, you shouldn't be using a pointer here.

This article has been dead for over six months. Start a new discussion instead.