What's wrong with this code? URGENT!!!

here's a code that would open a text file and get the contents of that text file line by line and assign them to variables.
the problem is that my text file has only these data

reserved.dat
--------------------------------------
2330
2345
2679
2345
--------------------------------------

but when i assign them to 10 variables, it gives me weird values like -23390 -1 etc..
see if you guys can figure out my mistake on this, and if possible if you could give a better solution
for this. thanks.

start of code
-------------------------------------------------------------

FILE *ResFile;
int Res[10],x;

ResFile = fopen("reserved.dat", "r+t");

for(x=0;x<=10;x++) {
fscanf(ResFile, "%d", &Res[x]);
}

if(Res[0]!=NULL)
reserved1->Text=Res[0];
reserved1->Show();

if(Res[1]!=NULL)
reserved2->Text=Res[1];
reserved2->Show();

if(Res[2]!=NULL)
reserved3->Text=Res[2];
reserved3->Show();

if(Res[3]!=NULL)
reserved4->Text=Res[3];
reserved4->Show();

if(Res[4]!=NULL)
reserved5->Text=Res[4];
reserved5->Show();

if(Res[5]!=NULL)
reserved6->Text=Res[5];
reserved6->Show();

if(Res[6]!=NULL)
reserved7->Text=Res[6];
reserved7->Show();

if(Res[7]!=NULL)
reserved8->Text=Res[7];
reserved8->Show();

if(Res[8]!=NULL)
reserved9->Text=Res[8];
reserved9->Show();

if(Res[9]!=NULL)
reserved10->Text=Res[9];
reserved10->Show();

fclose(ResFile);

---------------------------------------------------------

as you can see i am testing the Res array for NULL values. but it seeams i am getting results with null values.

thanks in advance.

arvin


Comments

  • : here's a code that would open a text file and get the contents of that text file line by line and assign them to variables.
    : the problem is that my text file has only these data
    :
    : reserved.dat
    : --------------------------------------
    : 2330
    : 2345
    : 2679
    : 2345
    : --------------------------------------
    :
    : but when i assign them to 10 variables, it gives me weird values like -23390 -1 etc..
    : see if you guys can figure out my mistake on this, and if possible if you could give a better solution
    : for this. thanks.
    :

    Okay, i had to delete the program, so look at the question for it. However, your problem is that you did not initalize your variables to NULL. You need to add this code after you declare your Res array:

    for (x=0;x<10;x++)
    Res[x]=NULL;

    Thats it! That sould solve your problem. Also, because x is already declared, no extra variables.


  • : here's a code that would open a text file and get the contents of that text file line by line and assign them to variables.
    : the problem is that my text file has only these data
    :
    : reserved.dat
    : --------------------------------------
    : 2330
    : 2345
    : 2679
    : 2345
    : --------------------------------------
    :
    : but when i assign them to 10 variables, it gives me weird values like -23390 -1 etc..
    : see if you guys can figure out my mistake on this, and if possible if you could give a better solution
    : for this. thanks.
    :

    Okay, i had to delete the program, so look at the question for it. However, your problem is that you did not initalize your variables to NULL. You need to add this code after you declare your Res array:

    for (x=0;x<10;x++)
    Res[x]=NULL;

    Thats it! That sould solve your problem. Also, because x is already declared, no extra variables.


  • : here's a code that would open a text file and get the contents of that text file line by line and assign them to variables.
    : the problem is that my text file has only these data
    :
    : reserved.dat
    : --------------------------------------
    : 2330
    : 2345
    : 2679
    : 2345
    : --------------------------------------
    :
    : but when i assign them to 10 variables, it gives me weird values like -23390 -1 etc..
    : see if you guys can figure out my mistake on this, and if possible if you could give a better solution
    : for this. thanks.
    :

    Okay, i had to delete the program, so look at the question for it. However, your problem is that you did not initalize your variables to NULL. You need to add this code after you declare your Res array:

    for (x=0;x<10;x++)
    Res[x]=NULL;

    Thats it! That sould solve your problem. Also, because x is already declared, no extra variables.


  • Oopps. Hate when that happens...


  • thanks man, such a stupid i am with just a simple solution.... thanks again.

    arvin


    : Oopps. Hate when that happens...
    :



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories