0

for a school project I need to open a .txt file and copy the information on to a multidimensional array, but I'm having trouble copying the content. The file is a 52 card deck :

4O 7E AC 3E 4E TO 4C 8P 5O TE 6O 8E AP
5E 6P JO 7C 7O QO 8O 3O 2E 9C 5P TC 6C
5C 8C 9O 6E 9E KO 2P 9P QP KE 3P 4P JE 
7P 2C AO JC QE TP 2O JP 3C QC KC AE KP 

As I said, I need to store sets o 5 cards in a multidimensional array, in a total of 10 sets [52/5=10] (if a set is incomplete (=not enough cards) we're supposed to ignore the leftovers). So far I have this

void card_store(char *argv[]) { 
char cards_set[10][5][3];
FILE* fp = fopen("deck.txt", "r");
char str;
int i,j;

str = fgetc(fp);  //Here I'm using fgetc but I want fscanf (don't know how)

    while (str != EOF)
    {
        for (i = 0; i < 10; i++) {
            for (j = 0; j < 5; j++) {
                 cards_set[i][j] = str;
            }
        }
    }

}

For example: cards_set[0] = {4O,7E,AC,3E,4E}

Miradil Zeynalli
  • 423
  • 4
  • 18
Vasco
  • 1
  • 2
  • 5
    You can't start variable names with a digit. You don't read anything inside your loops. What examples have you got in your course notes or course book to illustrate how to use `scanf()`? You can't use `return 0;` in a function that returns `void`. – Jonathan Leffler May 01 '20 at 13:59
  • I should add you can't assign a single character in your misnamed `str` to an array like `cards_set[i][j]`. If you had `char str[3]`, and read two characters plus trailing null into it, you still couldn't do an array assignment like that. You'd need to use `strcpy()`. Or, to assign to a single character, use `cards_set[i][j][0] = str;`. And if you do use `fgetc()`, the return value should be assigned to an `int`, not to a `char` — EOF is different from every valid character value that can be returned from `fgetc()`, so a `char` cannot hold all possible values. – Jonathan Leffler May 01 '20 at 14:06
  • 1
    `fgetc()` returns `int` by intention (not `char`!). – alk May 01 '20 at 14:16
  • @JonathanLeffler Thank you for your head's up. That was not a literal copy of my code, I had to change some things in order to keep it simple. – Vasco May 01 '20 at 14:47
  • 1
    Please don't post approximations to your code. There are many things that can go wrong when you do that — witness my commentary. Please read about how to create an MCVE ([Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve)) (or MRE or whatever name SO now uses) or an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)). If you don't post actual code that reproduces your problem, we cannot reliably help you resolve your problem. Sometimes, things behave erratically — get as close as you can. But code posted should compile and reproduce the problem. – Jonathan Leffler May 01 '20 at 14:49

1 Answers1

1

Just like scanf, you use fscanf, with extra parameter fp as first parameter. More about fscanf here.

So, for your problem, you can use fscanf:

char card_number, card_type;

while (fscanf(" %c%c", &card_number, &card_type) == 2)  // while there is input in file
{
    // do processing here
}

or if you know the exact length of the data, you can use a for loop, with the same logic.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Miradil Zeynalli
  • 423
  • 4
  • 18
  • 2
    You should probably use `" %c%c"` to skip (optional) white space. Otherwise, you're going to get a mess. I wonder if the code should consider using `" %1[23456789JQKAT]%1[CEOP]"` to limit the valid characters (what does the T represent, I wonder?). That is definitely not something to do casually; all else apart, it requires arrays of (at least) 2 characters as the argument for each of the card number and card type. – Jonathan Leffler May 01 '20 at 14:55
  • Yeah, you are right. Changed already (excluding limiting chars, as we don't really know the whole case). – Miradil Zeynalli May 01 '20 at 16:29
  • 1
    I put the space at the start; you put the space at the end. The space at the end is a UX disaster. While it doesn't matter too much if the input is from a file, as here, if the input ever comes from a user typing, the trailing white space is a disaster. Don't do it!! See [Trailing white space in a `scanf()` format string](https://stackoverflow.com/q/19499060/15168). – Jonathan Leffler May 01 '20 at 16:32
  • Leaving the scan sets out of it is perfectly sensible — they're beyond what the OP is ready to handle. Having the possibility noted in a comment maybe beneficial — and is probably harnless. – Jonathan Leffler May 01 '20 at 16:33