EDIT: Please note, this assignment was already submitted.

Well for our assignment this time around, we had to write up a BASIC editor. It had to sort lines of code numerically. Each line would never be more than 80 characters long and there would never be more than 100 lines. Seeing this, I decided to use a 2-dimensional Character Array.

Basically, the program had to be able to do the following.

1.) Sort the strings entered into the array (does this automatically)
2.) Save strings into a file with the name specified by the user.
3.) Load a user-specified file into the 2-d char array
4.) Print out all of the lines of basic
5.) exit <This one works!

My teacher has a saying: PAD1 (Principle of Algorithm Design 1) students have a tendency to make programs exponentially more complicated then they have to be.

I'm assuming that this is the case this time around....

# include<stdio.h>
# include<stdlib.h>
# include<string.h>
# define MAX_COLUMNS 80
# define MAX_ROWS 100

void save(char text[MAX_ROWS][MAX_COLUMNS], char filename[100]){
        FILE *fout;
        int i;
        fout=fopen(filename+5, "wt");
        for(i=0; i<MAX_ROWS; ++i){
                if(text[i][1]>=0 && text[i][1]<=9)
                        fprintf(fout,"%s",text);
                }
        fclose(fout);
return;
}


char load(char filename[50]){
        int i, j;
        FILE *fin=fopen(filename+5, "rt");
        char txt[MAX_ROWS][MAX_COLUMNS];
        for(j=0; j<MAX_ROWS; ++j){
                for(i=0; i<MAX_COLUMNS; ++i){
                        fscanf(fin,"%c", txt[j][i]);
                        }
                }
        fclose(fin);
return 0;
}


int main(void){
        FILE *fout, *fin;
        char filename[50], spare[MAX_COLUMNS], str[MAX_ROWS][MAX_COLUMNS];


        for(int y=0; y<=MAX_ROWS; ++y){
                int z=y-1;
                fgets(str, sizeof str, stdin);

/*This is the sorter*/
                if(str[y][1]<=9 && str[y][1]>=0 ){
                        if(y>0){
                                if(strcmp(str[y],str[z])<0){
                                        strcpy(spare,str[z]);
                                        strcpy(str[z],str[y]);
                                        strcpy(str[z],spare);
                                        }
                                else;
                                }
                        }
                else if(strncmp(str[y], "save", 4)==0){
                        char filename[100];
                        str[y][strlen(str[y])-1]=0;
                        strcpy(filename,str[y]);

                        }
                else if(strncmp(str[y], "load", 4)==0){
                        char filename[100];
                        str[y][strlen(str[y])-1]=0;
                        strcpy(filename,str[y]);
                        load(filename);
                        }
                else if(strncmp(str, "list", 4)==0){
                        for(int y=0; y<MAX_ROWS; ++y){
                                if(str[y][1]<='9' && str[y][1]>=0)
                                        printf("%s", str[y]);
                                else;
                                }
                        }
                else if(strncmp(str, "exit", 4)==0){
                        exit(0);
                        }
                else;
                }

return 0;
}

Line 39; 67: for(int y=0; y<=MAX_ROWS; ++y){
Not proper C89 syntax. Declare int y outside the for loop.

Line 41: fgets(str, sizeof str, stdin); fgets( str[y], sizeof str[y] / sizeof ( char ), stdin );
Line 66: else if(strncmp(str, "list", 4)==0) if ( strncmp( str[y], "list", 4) == 0 )

Line 10: fout=fopen(filename+5, "wt"); fout = fopen( filename, "w+" );
These are the string modes that fopen accepts.
You are forgetting to check that the file was created and it can be read.

The basic flow of the program is not that complicated. You kept it simple enough. There are a few details, though....

First thing I notice is fscanf(fin,"%c", txt[j][i]); Using fscanf() to read a character is like opening your front door with C4. It works, but wouldn't a key be better? Use fgetc() . It's designed to read a character, and only a character.
Same with fprintf(fout,"%s",text); . How about fputc() ?

Instead of a double loop for input, use a single loop and use fgets() to read a line at a time. This way the BASIC file doesn't have to have SPACE-filled lines. Increase each line from 80 to 82 to take into account the trailing '\n' and '\0'.

In this code:

int main(void){
    FILE *fout, *fin;
    char filename[50], spare[MAX_COLUMNS], str[MAX_ROWS][MAX_COLUMNS];

    for(int y=0; y<=MAX_ROWS; ++y){
        int z=y-1;
        fgets(str, sizeof str, stdin);

what are you reading? Where's the prompt so someone knows what to enter? if(str[y][1]<=9 && str[y][1]>=0 ) You just read a characters. There are no numbers, they are characters. You need at least if(str[y][1]<='9' && str[y][1]>='0' ) But then, you could also just use: if(isdigit(str[y][1]) and get the same result. And why str[y][1]? Don't arrays start at str[y][0]?

>>fgets( str[y], sizeof str[y] / sizeof ( char ), stdin );
The division is unnecessary because sizeof(char) is guarenteed to always be 1. So it can be reduced to this: fgets( str[y], sizeof str[y], stdin ); .

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