Please help me with this in C.....

I'm trying to write random integers to another file ..

I just don't get it work....

I'm new at this....

So what's wrong here......."maybee everything"..?

I'd be very grateful for help.

Thanks

erwin




.......

FILE *fp;

int numbers[100];

int *pts;

int i = 0;

int t = 0;

pts = &numbers[0];


{

random();

for ( t = 0; t < 100; t++)

numbers[t] = rand() % 255;

}


if ((fp=fopen("c:\test.txt", "w+")) == NULL)

{

fprintf(stderr, "can't open file....!
");

exit(1);

}



for (i = 0 ; i < 100; i++);

fprintf(fp, "%d", numbers[t]);

fclose(fp);



}




Comments

  • : I'm trying to write random integers to another file ..


    :

    : .......

    : FILE *fp;

    : int numbers[100];

    : int *pts;

    : int i = 0;

    : int t = 0;

    : pts = &numbers[0];


    : {

    : random();

    : for ( t = 0; t < 100; t++)

    : numbers[t] = rand() % 255;

    : }




    Okay, I'm presuming 'random' is some routine somewhere in your program because it's not listed here. Something to seed the random number generator perhaps?


    I can't see an immediate flaw with this.



    : if ((fp=fopen("c:\test.txt", "w+")) == NULL)

    : {

    : fprintf(stderr, "can't open file....!
    ");

    : exit(1);

    : }

    :

    : for (i = 0 ; i < 100; i++);

    : fprintf(fp, "%d", numbers[t]);

    : fclose(fp);

    :

    : }




    Well, if you examine the for loop just above, you'll see that you're controlling the loop with one variable, and then in the next line, you're attempting to index the array with....





  • Hello , thanks for your answer.

    Yes , it seeds random numbers , but it doesen't work,

    ....

    I'm getting "one" instead of 100 different random numbers ,why.....?


    erwin






  • To write the numbers in the file do that


    for (i = 0 ; i < 100; i++)

    fprintf(fp, "%d", numbers[i]);


    1. No semicolon at the end of the for loop

    2. You use the wrong variable to write the numbers

    into the file: numbers[i]







  • : I'm trying to write random integers to another file ..

    : I just don't get it work....

    : I'm new at this....

    : So what's wrong here......."maybee everything"..?

    : I'd be very grateful for help.

    : Thanks

    : erwin


    :

    : .......

    : FILE *fp;

    : int numbers[100];

    : int *pts;

    : int i = 0;

    : int t = 0;

    : pts = &numbers[0];


    : {

    : random();

    : for ( t = 0; t < 100; t++)

    : numbers[t] = rand() % 255;

    : }


    It looks to me like you have your curly braces in the wrong place, but I hav eonly glanced file I/O so I could be wrong.




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