1

This program isn't completed entirely but I am taking it one step at a time. Right now I am trying to have the program open a file, detect the terminal size (row and columns), and display a message if a file is entered.

I have set fp to read mode, I have an error message if no file is specified, I have set up my if statements, if column size is less than 80, display an error and if row size is less than 20 display an error... I think this is where my issue is arising, I never actually played around with terminal sizing until this project.

So my question is: how can I get the program to display an error if sizing criteria isn't met?

Note: Like I said I know the code isn't 100% as of now I am just trying to do some debugging on the way so I can eventually get a bigger working project.

Anyways, here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>

int main(int argc, char **argv)
{
    struct winsize terminal;
    FILE *fp;
    int c;

    ioctl (0, TIOCGWINSZ, &terminal, argv[2]);

    fp              = fopen(argv[1], "r");    //set open for argv[1] and set it to read mode
    column          = terminal.ws_col;
    row             = terminal.ws_row;

    if (fp          == NULL)
    {
            fprintf(stderr, "Error opening '%s'.\n", argv[1]);                              //if not file is specified then close program and give an error
            exit(1);
    }
    c               = fgetc(fp);                                                            //set c to fgetc

    if column < 80 || row < 20)                                                               //if temrinal window is less than 80x20, display an error and exit
    {
            printf("Terminal window must be atleast 80x20!\n");                           //display error and close program if column criteria isn't met
            exit(1);
    }

    while(!feof(fp))                                                                        //if file is entered, display a test message (this is where later on I will show hex value)

    {
            printf("Good Job! You picked a file to manipulate!!\n");
            exit(1);
    }
    fclose(fp);                                                                             //close fp


    return 0;
  }
Ken White
  • 123,280
  • 14
  • 225
  • 444
Rob
  • 31
  • 1
  • 7
  • 2
    Aside: [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Weather Vane Mar 25 '16 at 16:50
  • @WeatherVane, thank you for this! My professor taught me that way, I was unaware. I appreciate it. – Rob Mar 25 '16 at 16:56
  • 1
    Did your professor also tell you to use `char c` for the large number of C functions that actually require `int c` such as `fgetc` which you use here? – Weather Vane Mar 25 '16 at 16:59
  • Unfortunately, yes. That is what he did in all the example programs we did in lecture. – Rob Mar 25 '16 at 17:05
  • 2
    `fgetc` can return `EOF` which is usually `-1`. If you use an 8-bit char, this will be impossible to distinguish from a valid file read of `0xFF` – Weather Vane Mar 25 '16 at 17:07
  • @WeatherVane, thank you again. That does make sense actually. – Rob Mar 25 '16 at 17:12
  • ...and another problem, is if your C has `signed char` (they often do by default), any values read from file > 127 will be negative when you later pass the `char` to an `int` valiable. – Weather Vane Mar 25 '16 at 17:13
  • 1
    *how can I get the program to display an error if sizing criteria isn't met?* First, check the return value from your `ioctl()` call to see that it actually worked. `stdin` may very well be a device that doesn't even *have* a "size". – Andrew Henle Mar 25 '16 at 17:15
  • I don't really know what you are asking. You have trapped the size, but it could be cleaner. `if (terminal.ws_col < 80 || terminal.ws_row < 20) { printf("Terminal window must be at least 80x20\n"); exit(1); }` – Weather Vane Mar 25 '16 at 18:10
  • @WeatherVane thank you. Everything is now the way I expected the code to execute. – Rob Mar 25 '16 at 18:17

0 Answers0