0

I am trying to read in the Constitution as a text file from the command line into my program to print out the lines in reverse order. My for loop looks like this:

for(int i = 0; i >= 0; i--) {
    if(strings[i] == '\0') //counts through array until it finds a line break
    { 
        break;
    }
    printf("%s", strings[i]);
}

When the program runs, the only thing that prints is the first line of the Constitution. If I modify my for loop to increment i, the program runs smoothly and outputs the Constitution like normal, and therefore I believe my entire problem is summed up in this for loop. This is the rest of my program for reference.

int clearBuffer() {
    char junk;
    while((junk = getchar()) != feof(stdin) && junk != '\n');
    return 0;
}

int getAline(char ** bufferPointer, int * sizePointer){ 
    char * buffer = *bufferPointer;
    int count = 0;
    int size = *sizePointer;
    while(!feof(stdin)){
        if(count >= size - 1){
            char * tempBuffer = (char * )malloc(size * 10); 
            //strcpy(tempBuffer, buffer );
            for (int i = 0; i < size; i++){
                tempBuffer[i] = buffer[i];
                //putchar(tempBuffer[i]);
            }
            free(buffer);
            buffer = tempBuffer;
            size *= 10;
        }
        buffer[count] = getchar();

        if(buffer[count] == '\n'){
            break;      
        }
        if(buffer[count] == EOF){
            buffer[count] = '\0';
            break;
        }

        count++;
    }
    *bufferPointer = buffer;
    *sizePointer = size;
    return count-1;
    }

int main(){

    char * buffer;

    char * strings[1000]; 


    int arrayCount =0;
    int size = 10;

    while(!feof(stdin))
    {
        buffer= (char*) malloc(size);
        getAline(&buffer, &size);

        strings[arrayCount++] = buffer;
    }

    for(int i = 0; i >= 0; i--) {
        if(strings[i] == '\0'){
            break;
        }
        printf("%s", strings[i]);
    }
    return 0;
}
Emma Barrett
  • 83
  • 1
  • 10
  • `for(int i = 0; i >= 0; i--) {` only once. start form last number. – BLUEPIXY Sep 29 '16 at 03:48
  • 2
    http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – William Pursell Sep 29 '16 at 03:50
  • `EOF` is not a character. – melpomene Sep 29 '16 at 03:51
  • @Emma What melpomene is saying is, to test for EOF, you have to get the character into a temp variable of type `int`, then test for `EOF`, and only then store it to char buffer, if it wasn't `EOF`. – hyde Sep 29 '16 at 04:30
  • @hyde Okay that makes sense. It still works just fine though if I don't do that. Is it an efficiency deal? – Emma Barrett Sep 29 '16 at 05:07
  • @EmmaBarrett If you use `char`, then character code 0xFF is equal to `EOF`. It might never matter if you only read text files which are in English language or UTF8 encoding. But for example in common Latin1 encoding 0xFF is `ÿ`, and a binary file might of course contain any byte, so if you read one of those using `char` variable, it will detect these as `EOF` and not read the rest of the file. – hyde Sep 29 '16 at 05:17

2 Answers2

3

When you reverse a loop's iteration direction, you also have to reverse the beginning and ending values.

for(int i = arrayCount-1; i >= 0; i--)

Now, this loop starts at the end, then works back down to the beginning of the array.

Jonny D
  • 2,244
  • 15
  • 22
1

Make int i equal the number of lines in the Constitution instead of 0 that should do it.

Sam K9
  • 99
  • 9