1

I am working on a homework assignment and I can not figure out how to pass the information from my file into my program to use. We are working on the game of life and 2D arrays, and we were asked to use command line arguments to read a separate file that has the first two lines the height and width of the 2D array and then the rest are asterisks and spaces that we convert to 0s and 1s to play the Game of Life.

I have tried both fscanf and fgets but I can't get either one to work out for me.

int main(int argc, char*argv[])
{

    FILE * fileTable;
    fileTable = fopen(argv[1], "r");

    if(fileTable==NULL)
    {
        printf("file not found!!!\n");
        exit(1);
    }

    int i = 0;
    double g;
    char * listA[1000];

    fgets(listA, 50, fileTable);

    fclose(fileTable);

When I use fgets, I end up with an error basically telling me I am doing it wrong and using the wrong types and whatnot, but when I use scanf inside of a while loop using feof, it never exits the loop.

int i = 0;
double g;
fscanf(fileTable, %lf, &g);
while(!(feof(fileTable)))
{
   fscanf(fileTable, "%lf", &g);
   i++;
}
fclose(fileTable); 
Grace
  • 11
  • 2
  • 3
    The first argument to `fgets()` should be a pointer to a string, but you're giving it a pointer to an array of strings. You also haven't allocated any memory for the strings in the array. – Barmar Apr 04 '19 at 00:45
  • Regarding `scanf`, I think I know what you're doing wrong, but would have to see the code that's giving you problems to be sure. Please post the code that's giving you problems. Thanks. – MFisherKDX Apr 04 '19 at 00:50
  • 2
    When you get a compiler error message you don't understand, include the message in the question — for the code you post in the question, as you show it in the question. This part of making an MCVE ([MCVE]). It does mean you need at least a header (``) and a close brace added to the code shown. The line numbers in the error message should match the line numbers in the code. – Jonathan Leffler Apr 04 '19 at 00:52
  • In your code, you should check that `argv[1]` is set (because `argc` is at least 2) before trying to use it. Although you could check for 'not null' with `argv[1]`, that wouldn't work reliably with `argv[2]` or beyond, whereas checking against `argc` always works, so check against `argc`. It's also a good idea to print error messages on `stderr` (standard error), and to include the name of the file that you failed to open. – Jonathan Leffler Apr 04 '19 at 00:54
  • 3
    Note that [`while (!feof(file))` is always wrong!](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) . You should forget that `feof()` exists and essentially *never* use it. It does have occasional uses, but they are few and far between. (I did see one new answer today to a C question on SO using `feof()` correctly — but that's unusual, and I wasn't wholly convinced that it was a good idea, but it was at least used correctly…) – Jonathan Leffler Apr 04 '19 at 00:55
  • 1
    `fscanf` is probably failing to convert input to a `double` and returning 0. (Possibly because your input is a line of asterisks, I don't know). You are expecting it to return `1` as you are converting one input to the value `g`. You should check the return value of `fscanf` against the value you expect `1`, and then handle the error conditions appropriately. If you continue to call `fscanf` after an error you can [enter an infinite loop](https://stackoverflow.com/questions/5285737/scanf-ignoring-infinite-loop). Also, height and width are probably going to be integers and not doubles. – MFisherKDX Apr 04 '19 at 01:27
  • Note that the code (as of the 2019-04 04 01:08:57Z edit) doesn't compile because `fscanf(fileTable, %lf, &g);` is a syntax error — you need `fscanf(fileTable, "%lf", &g);` with the double quotes — which means we can't trust anything else in your question; we can't tell what you're actually compiling because you've not shown us. – Jonathan Leffler Apr 04 '19 at 04:01

0 Answers0