473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Writing a 2D array to a file

105 100+
I have a 2D array a[336][336] and I wanted to write this array into a text file.

I have created a 2d pointer but I am not able to understand how to make this pointer point to this array. Could anyone please help me with this? Thanks in advance.

Here’s the part of the code that I have written.
Expand|Select|Wrap|Line Numbers
  1. float a[336][336];
  2. FILE *qfile;
  3. qfile=fopen("D:/c.txt","wb");
  4. float **img;
  5. img=new float*[336];
  6. for(unsigned long int i=0;i<336;i++)
  7. {
  8.  
  9. *(img+ i) = new float[336];
  10. }
  11.  
  12. for (unsigned long int i = 0; i < 336; i++)
  13. {
  14.  fwrite(*(img + i), 4, 336, qfile);
  15. }
  16. fclose(qfile);
Mar 26 '07 #1
3 15762
horace1
1,510 Expert 1GB
is there any reason why you can do this
Expand|Select|Wrap|Line Numbers
  1. FILE *qfile;
  2. qfile=fopen("D:/c.txt","wb");
  3. int i;
  4.  
  5. for(i=0;i<336;i++)
  6. {
  7.  fwrite(a[i], sizeof(float), 336, qfile);
  8. }
  9. fclose(qfile);
Mar 26 '07 #2
mickey22
105 100+
I was trying to use pointer but I did not know I can just do it so simple.It worked and thank you so much for your help.
Mar 26 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
A couple of things here:

float **img;

is not a 2D pointer. A float** is a pointer to a pointer to a single float.

This array:

float a[336][336];

is an array of 336 elements where each element is itselt and array of 336 floats. Following the rule that the name of the array is the address of element 0, you need a pointer to an array of 336 floats:

float (*ptr)[336] = a;

Here ptr is such a 2D pointer and can point to the array "a".

However, all array elements are contiguous in memory so the 336 floats for
a[0] precede the 336 floats of a[1]. This is done so pointer arithmetic works in locating an element of the array. That makes the memory layout of a 2D array [336][336] exactly the same as a 1D array of [336 * 336].

Following that, just write out 336 * 336 floats:

float* ptr;

ptr = &a[0][0];

fwrite(ptr, sizeof(float), 336 * 336, qfile);

fclose(qfile);
Mar 26 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: George Stout | last post by:
First off I do not know alot about writing queries to an Access Database from an ASP page. This is why I need help. I have an Events database for 6 colleges in our metro area. On the homepage I...
5
by: rob | last post by:
hey every1, I've got alot of data to write out to file and it's all just 1's and 0's. It's all stored in 2 dimensional arrays of width 32 and varying height. At the moment it's all just...
2
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
2
by: simonc | last post by:
Is there an easy way of writing a number in 32 bit integer format into four bytes of a file? I am experimenting with FilePut but I can only make it work by defining a four byte array and doing some...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
2
by: totoro2468 | last post by:
Here is my code and output. Why is it writing to the array incorrectly, when I rewinded the file to the beginning? CODE: void ReadString (char *filename, int *lengthPtr) { FILE *ifp; char...
9
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No,...
4
by: daqmaneng | last post by:
does anyone have any suggestions for writing a two dimensional array of data to a file. Currently, I have an array of 5 columns, with 1000 elements per array. I know that I can use a for next...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.