0

My text file is as following:

Random Words //this only appears once at the top

Occupation1

1 2 3 4 5

6 7 8 9 10

Occupation2

11 12 13 14 15

16 17 18 19 20

I am having some trouble with the input and was wondering if you could take a look at my code.

typedef struct foo
{
    char occupation[256]; 
    int numbers[limita][limitb];
}FOO;

void function(FOO input[]);

int main()
{
    FOO input[limit];
    function(input);
    return 0;
}

void function(FOO input[])
{
    FILE* file;
    file = fopen("textfile.txt","r");
    int a=0; int b =0; 
    char temp[81];
    char randomwords[81];

    while(fgets(temp,sizeof(temp)-1,file))
    {
        sscanf(temp, "%[^\n] %[^\n] %d", randomwords,&input[a].occupation[a], &input[a].numbers[a][b]);
        a++;
    }
}

So I tried printing out (with printf) the random words and the occupation but to no avail. I don't think i'm using numbers correctly at all as it has to be a 2D array and don't seem to be changing the columns.

I would really appreciate a step in the right direction/some help. Please explain simply. Thank you very much.

EDIT: technomage brought up an interesting point regarding what i'm doing vs what I want. I'm not sure how to change in reflection to what he suggested though.

  • How do you compile this? To my knowledge arrays in a struct need to be constant, otherwise the compiler complains (i.e. no variables used as sizes). Cf. http://stackoverflow.com/questions/14925803/variably-modified-variable-name-at-file-scope-error – Nobilis Jun 20 '13 at 02:01
  • Yeah, I complied this. But even if you change the variables to number constants the input bit is still not correct. :( – user2503381 Jun 20 '13 at 02:03
  • Have a look at my answer that should address at least one problem with your code. – Nobilis Jun 20 '13 at 02:09
  • 1
    You're telling `sscanf` that every line of input is "string string digits", which doesn't match the example input you provide. – technomage Jun 20 '13 at 02:47
  • @technomage Oh. How should it be then? should I use multiple sscanf statements? – user2503381 Jun 20 '13 at 02:58
  • Either you use a single `sscanf` which matches *any* line, or you have a different `sscanf` for each unique pattern. Or use a regular expression library which will generally make it easier to extract patterns and groups of patterns. – technomage Jun 26 '13 at 15:21

1 Answers1

0

You probably want to do something like this. This implementation assumes that there are no blank lines in the input file. There are lot of hard coding here, you may want to make it better. I hope this helps you...

typedef struct foo
{
    char occupation[256]; 
    int numbers[2][5];
}FOO;

void function(FOO input[]);

void function(FOO input[])
{
    FILE* file;
    file = fopen("textfile.txt","r");
    int a = 0, count, i; 
    char temp[81];
    char randomwords[81];

    if (file == NULL)
    {
        return;
    }

    fscanf(file, "%[^\n]\n", randomwords);
    printf("%s\n", randomwords);
    while (feof(file) != EOF)
    {
        i = 0;
        i = fscanf(file, "%[^\n]\n", input[a].occupation);
        if (i == -1)
            break;

        for (count=0; count < 5; count++)
        {
            i = 0;
            i = fscanf(file, "%d", &(input[a].numbers[0][count]));
        }

        for (count=0; count < 5; count++)
        {
            i = 0;
            i = fscanf(file, "%d", &(input[a].numbers[1][count]));
        }

        a++;
        i = 0;
        i = fscanf(file, "\n");
        if (i == -1)
           break;
    };
    fclose(file);
}

int main(int argc, char *argv[], char *envp[])
{
    int     i, j, count;
    FOO input[10];

    function(input);

    for (count = 0; count < 2; count++)
    {
        printf("%s\n", input[count].occupation);
        for (i = 0; i < 2 ; i++)
        {
            for (j = 0 ; j < 5 ; j ++)
            {
                printf ("%d ", input[count].numbers[i][j]);
            }
            printf ("\n");
        }
        printf ("\n");
    }
}
sukumarst
  • 265
  • 1
  • 5