At line 45 where it says "char array[lines - 1][MAX_LEN];" my IDE gives me an error, underlining lines, that says "Expression must have a constant value. The value of variable "lines" (declared at line 10) cannot be used as a constant." How can I get my code to work? I want to use the value I found with the loop before that part, subtract 1 from it, and then I want that number value to go into my 2d array
#include<stdio.h> #include<stdlib.h> #include<string.h> #include "List.h" #define MAX_LEN 160 int main(int argc, char* argv[]) { int lines = 0; int x = 0; FILE *in, *out; char line[MAX_LEN]; //check for correct number of arguments if (argc != 3) { printf("Incorrect number of arguments (needs: <input file> <output file>)\n", argv[0]); exit(1); } //open files for reading in = fopen(argv[1], "r"); out = fopen(argv[2], "w"); if (in == NULL) { printf("Unable to open <read from> file \n", argv[1]); exit(1); } if (out == NULL) { printf("Unable to open <write to> file\n", argv[2]); exit(1); } //counting number of lines while (fgets(line, MAX_LEN, in) != NULL) { lines++; } // Return to start of file rewind(in); char array[lines - 1][MAX_LEN]; int a = -1; // Copy strings to array while (fgets(line, MAX_LEN, in) != NULL) { strcpy(array[++a], line); // Copy from file } //Create new list List end = newList(); append(end, 0); int j; // declare j to help transverse array // Loop till fully iterated for (j = 1; j < lines; j++) { char* temp = array[j]; int complete = 0; //move to tail and find appropriate insertion location moveBack(end); // Create conditions to determine where to insert new indice while (complete != 1 && strcmp(temp, array[get(end)]) <= 0) { // If at the end of the array, then insert it before the last index value and set complete codnition to true (1) if (index(end) == 0) { insertBefore(end, j); complete = 1; } // If we have not found the proper position, iterate to prev else { movePrev(end); } } // If complete is not true, then insert indice after last known index if (complete == 0) { insertAfter(end, j); } } // Convert and save to file; Start by returning to head moveFront(end); // Run through list, and convert from indce value to that of string int i; for (i = 0; i < lines; i++) { fprintf(out, "%s", array[get(end)]); moveNext(end); } //Close files and free memory fclose(out); fclose(in); freeList(&end); return 0; } https://stackoverflow.com/questions/65516037/c-programming-2-dimensional-char-error-help-char-arraylines-1max-len December 31, 2020 at 10:46AM
没有评论:
发表评论