2

I am having a problem and cant tell what is it.

struct arrayDB {
char *user[MAX_SIZE];
char *pass[MAX_SIZE];
char db[10][2];
};

void readFile(char fileName[100])
{
    char* word ;
    char line[90];
    FILE *passFile;
    int rowC=0;
    int chk=0;

    passFile=fopen(fileName,"rt");
    while(fgets(line,90,passFile)!=NULL)
    {
        word=strtok(line," ");
        rowC=rowC+1;
        while(word!=NULL)
        {
            printf("Count=%i \n",rowC);
            if(chk==0)
            {
                printf("word:%s\n",word);
                DB.user[rowC]=word;
                chk=1;
            }
            else
            {
                printf("word:%s\n",word);
                DB.pass[rowC]=word;

            }
            printf("r=%s , c=%s\n",DB.user[rowC],DB.pass[rowC]);
            word=strtok(NULL," ");

        }

        chk=0;
    }
    int i;

    for(i=1; i<6;i++)
    {
        printf("- %s , %s \n",DB.user[i],DB.pass[i]);
    }

 }

but the output I am getting that all the array elements is the same value which is the last word in the file as you can see in the pic

enter image description here

thanks

2
  • 1
    Is it really easier to put a screenshot instead of text copy/paste? Commented Jan 29, 2016 at 20:19
  • 1
    The code you are showing is not the code you ran the example with. For one, in the line DB.pass[rowC]=word; DB is not defined. Commented Jan 29, 2016 at 20:37

2 Answers 2

1

You're reading every line into the same string line. Then when you use strtok(), it's returning pointers into this string, and you're storing these pointers into DB. So all the records in DB are pointing to locations in line, which gets overwritten each time you read another line from the file. When everything is done, line contains the contents of the last line of the file, and all the DB entries point to that.

Another problem is that line is a local variable, and pointers to it become invalid when the function returns.

To solve both problems, you need to make copies of the string and store these in DB. For example:

        DB.user[rowC]= strdup(word);

This also means that when you're done with a DB record, you need to call free(DB.user[i])

Sign up to request clarification or add additional context in comments.

Comments

1

Some suggestions:

First, learn to use a debugger. There are free ones, get one and turn it on to find all of these errors (that is what I did here)

Next, for the code example you show to compile, the struct definition needs to support your code (currently, DB is not defined)

typedef struct  
{
    char *user[MAX_SIZE];
    char *pass[MAX_SIZE];
    char db[10][2];
}arrayDB;

arrayDB DB;//define DB

Next,

you need to allocate space for your string arrays: something like:

for(i=0;i<MAX_SIZE;i++ )
{
    DB.user[i] = malloc(100);
    DB.pass[i] = malloc(100);
}

Next, don't forget to free them when done using them.

for(i=0;i<MAX_SIZE;i++ )
{
    free(DB.user[i]);
    free(DB.pass[i]);
}

Next, you cannot assign a string using an equal operator:

DB.pass[rowC]=word;

use strcpy (or some other string function) instead:

strcpy(DB.pass[rowC],word);

Next, this line:

printf("r=%s , c=%s\n",DB.user[rowC],DB.pass[rowC]);

Is called after a conditional statement where either DB.user[rowC] or DB.pass[rowC] will be written to, never both. Suggest splitting this printf statement to print one or the other, and place it into the appropriate conditional branch.

3 Comments

thanks for all the tips. The thing is that I am java programmer. but i am trying to do socket programming in C and everything is different in C. Thank you again. I will try to do your recommendations
He is incrementing the index. Didn't you see the line rowC = rowC + 1;?
Yes. Saw that after I put the last comment in. I will edit that last comment out. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.