Thread: Fellow c programmers

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    9

    Fellow c programmers

    Hello,
    I am writing a program and I have data stored in a 2d array. The data is a combination of integers and floats. I have opened a file in which to write the data into using
    Code:
    my_out = fopen ("C:\\results.txt","wt")
    however i do not know how to write the data into my file from the array.
    Could anyone help me with this?
    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    The most important thing is that you're consistent in how you read and write. Just use a loop to step through one of the arrays (a 2d array is just an array of arrays), writing each element to the file. When you get to the end, step to the next array and do the same thing.

    You just have a loop to go through each dimension in the array, writing the elements as you go.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    9
    I need the data from the arrays to go into the file in a certain order. Could you please show me how to write one of the values into the file?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I need the data from the arrays to go into the file in a certain order
    Well what's the certain order? Without knowing the exact order you need, my only advice is that you write two loops, and you make sure you read and write in the same order.

    Code:
    for(i = 0; i < height; i++) {
        for(j = 0; j < width; j++) {
            write(array[j][i]);
        }
    }

  5. #5
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Something like this?
    This would write one line, obviously a loop would help to write more, obviously you need to
    fclose(my_out); when you have finished.

    Code:
    fprintf(my_out,"\n%d %f %d %f",some_it, some_float, another_int, another_float);
    This would write one line, obviously a loop would help to write more, obviously you need to
    fclose(my_out); when you have finished.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cprogra
    By BobMcGee123 in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 05-05-2006, 03:34 PM
  2. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  3. Game Programmer's AIM Circle: Join Today
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 12-20-2003, 12:12 PM
  4. Are programmers engineers?
    By joshdick in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 04-01-2003, 01:55 AM
  5. Programming Puns
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 03-23-2002, 04:38 PM