Hi!
I'm just starting to code in C, and I'm trying to read the information in a dat file, to then use it in some calculations. I read the information in the file and then save it in an array. However when I try later to access that array I only get zeros. I've checked that the code is indeed reading the values, but I don't know what to do to actually save them. I appreciate any input you can give me about it!

This is the section of the code that reads the dat file:

/* Open the file                             */
 fp = fopen("VELOCITY_nodes.dat", "r");
/*file=fopen("VELOCITY_nodes.dat", "r");*/

/* Check that the file is open               */
if (fp==NULL){
    Message("ERROR: cannot open the velocity file");
}
else
{
    /* Now that the file is open, loop until */
    /* the max of the data is reached        */
    while (i<=max)
    {
        /* Initialize j                      */
        j=0;

        /* Do only up to 2, because the positions of */
        /* the velocity vecto are [0,1,2]            */
        while (j<=2)
        {
            fscanf(fp,"%f",&vel);
            velxyz[i][j]=vel;

            /* Print the positions (i,j) for the matrix */
            Message("i=%d j=%d\n",i,j);
            /* Print the velocity value read           */
                          /* at this point I check that the values are read*/
            Message("vel (i,j) = %f \n", velxyz[i][j]);

            ++j;
        }
        ++i;
    }
}
fclose(fp);

/*then here I try to read any position on the matrix velxyz, and there are only zeros    */
Message("vel (i,j) = %f \n", velxyz[10][1]);

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Welcome to this forum, mrestrepo30!

Apparently you haven't read how to use code tags, so I'll provide you with a link:
http://www.daniweb.com/forums/misc-explaincode.html
(please read this carefully, and in your next post, you apply what's stated in the link)

I don't want to blame you for not using code tags (since it is only your first post), and that's exactly why I just kept my head cool, and gave you a link to the page where they describe the use of code tags.
It's for your own good, because you'll probably get into trouble if you refuse to use them.

Hi!
I'm just starting to code in C, and I'm trying to read the information in a dat file, to then use it in some calculations. I read the information in the file and then save it in an array. However when I try later to access that array I only get zeros. I've checked that the code is indeed reading the values, but I don't know what to do to actually save them. I appreciate any input you can give me about it!

This is the section of the code that reads the dat file:

/* Open the file                             */
     fp = fopen("VELOCITY_nodes.dat", "r");
    /*file=fopen("VELOCITY_nodes.dat", "r");*/

    /* Check that the file is open               */
    if (fp==NULL){
        Message("ERROR: cannot open the velocity file");
    }
    else
    {
        /* Now that the file is open, loop until */
        /* the max of the data is reached        */
        while (i<=max)
        {
            /* Initialize j                      */
            j=0;

            /* Do only up to 2, because the positions of */
            /* the velocity vecto are [0,1,2]            */
            while (j<=2)
            {
                fscanf(fp,"%f",&vel);
                velxyz[i][j]=vel;

                /* Print the positions (i,j) for the matrix */
                Message("i=%d j=%d\n",i,j);
                /* Print the velocity value read           */
                              /* at this point I check that the values are read*/
                Message("vel (i,j) = %f \n", velxyz[i][j]);

                ++j;
            }
            ++i;
        }
    }
    fclose(fp);

/*then here I try to read any position on the matrix velxyz, and there are only zeros    */
Message("vel (i,j) = %f \n", velxyz[10][1]);

end quote.

can u post a stand alone code which can be compilable along with a sample dataset to operate on.

So i can be able to check the bug.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Forget it Dream2code, the OP ran away just after posting it, when asked to use the code tags.

Forget it Dream2code, the OP ran away just after posting it, when asked to use the code tags.

And I did my best to politely ask him :'(

Comments
You sure did :)

Thanks salem.But i really didn't get your meaning?
Good Luck.

Well, he meant that the OP (= Original Poster = the person who started this thread) is probably never going to come back again.

Well, he meant that the OP (= Original Poster = the person who started this thread) is probably never going to come back again.

Meh.
I really hate it when people do that. Especially when I actually put quite a lot of effort in answering their question.
Too lazy to do their own damn homework and even too lazy to read the replies from people that are trying to help...

But with such an attitude the lazy will never get a descent job and thus no money and no big house and no....
And in the end they will be miserable and alone and all of this will just be because they posted a 'hit and run' thread :)

A classic case of Darwin's 'natural selection'. Lazy bast*rds won't get far.

Comments
I agree :)
This article has been dead for over six months. Start a new discussion instead.