Question:

Need help with file i/o in c

by  |  earlier

0 LIKES UnLike

I have a text file like this

7311

6234

6534

3465

and an array in my program ' map [4] [4];

i was wondering how you could read the right integers into the right spaces in the array... if you get what i mean.

Or if anyone knows how to read a particular integer at a particular space in the text file into the right space in the array.

Thanks

 Tags:

   Report

2 ANSWERS


  1. int y=0

    int z=0

    while (y < 5){

       while(z < 5){

          read next integer into map[y][z]

    z++

       } //end inner z loop

    y++

    } //end outer y loop

    Sorry, the yahoo messes up the spacing. But what you're doing is a nested loop. The outside loop is for the first [4], the inside loop is for the second [4]. So the first time around, its going to read [0][0], then [0][1], then [0][2], etc.

    Also, if your array has its bounds set as [4][4], then i'm assuming we know how many integers are in the input file.


  2. FILE fPtr;

    int a ,b;

    char number;

    a = b = 0;

    fPtr = fopen("numbers.txt", "r");

    if (fPtr == 0)

    return;

    while (!feof())

    {

    fread(number, 1, 1, fPtr); //I think the parameters are right

    if (number >= '0' && number <= '9') //assuming ascii characters

    {

    map[a][b] = number;

    b++;

    }

    else

    {

    a++;

    b = 0;

    }

    }

    fclose(fPtr);

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.