I wrote a short piece of code to give me a .dat file for use in another program. But i cant figure out what is wrong. I run on a mac, and use Xcode.

#include <stdio.h>


#define ARRAYSIZE 1500
#define BINDATA "file://localhost/Users/Hub/Desktop/231A4binData.dat"

int main (void)
{
	int i;
	
	double dblData[ARRAYSIZE];
	
	FILE * dblPoint;
	
	int dblSize = sizeof(double);
	
	for (i = 0; i < ARRAYSIZE; i++)
	{
		dblData[i] = i * 2;
	}
	
	dblPoint = fopen(BINDATA, "wb");
	
	fwrite(dblData, dblSize, ARRAYSIZE, dblPoint);
	
	fclose(dblPoint);
	
	return 0;
}

any help or words of advice are greatly appreciated.

Recommended Answers

All 6 Replies

A few things...First what doesn't work? Next you should check to see if fopen was successful.

dblPoint = fopen(BINDATA, "wb");

Did fopen succeed or fail here?

I dont get any errors, it should correctly write out to this file and if the file doesn't exist it should create it. (Or i believe thats how it works) But in console i get this error

Running…
Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
(gdb)

I re-wrote to include a file open checker which indeed does return a false. So i guess i dont understand how to properly create a binary (.dat) file.

#include <stdio.h>


#define ARRAYSIZE 1500
#define BINDATA "file://localhost/Users/Hub/Desktop/231A4binData.dat"

int main (void)
{
	int i;
	
	double dblData[ARRAYSIZE];
	
	FILE * dblPoint;
	
	int dblSize = sizeof(double);
	
	for (i = 0; i < ARRAYSIZE; i++)
	{
		dblData[i] = i * 2;
	}
	
	dblPoint = fopen(BINDATA, "wb");
	
	if (dblPoint == NULL)
	{
		printf("File opening was not successful");
		exit(1);
	}
	
	fwrite(dblData, dblSize, ARRAYSIZE, dblPoint);
	
	fclose(dblPoint);
	
	return 0;
}

I'm not familiar with mac but I would ensure that you have a proper file path and the permissions to create a file.

Hey Atramposch, I'm far from a high caliber C programmer, but I spent some time looking through your code to see if I could locate your problem. Here's something I found. Although I may be wrong, it seems that I found that the boolean condition, "NULL" is a subset of the <string.h> library. So you could try adding the line-

#include <string.h>

-before the main function, and if this was your problem, it would then be solved.

I dont get any errors, it should correctly write out to this file and if the file doesn't exist it should create it. (Or i believe thats how it works) But in console i get this error

Running…
Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
(gdb)

I re-wrote to include a file open checker which indeed does return a false. So i guess i dont understand how to properly create a binary (.dat) file.

#include <stdio.h>


#define ARRAYSIZE 1500
#define BINDATA "file://localhost/Users/Hub/Desktop/231A4binData.dat"

int main (void)
{
	int i;
	
	double dblData[ARRAYSIZE];
	
	FILE * dblPoint;
	
	int dblSize = sizeof(double);
	
	for (i = 0; i < ARRAYSIZE; i++)
	{
		dblData[i] = i * 2;
	}
	
	dblPoint = fopen(BINDATA, "wb");
	
	if (dblPoint == NULL)
	{
		printf("File opening was not successful");
		exit(1);
	}
	
	fwrite(dblData, dblSize, ARRAYSIZE, dblPoint);
	
	fclose(dblPoint);
	
	return 0;
}

Reduce your array size [ARRAYSIZE] to a low value and see if it is nt giving any error - It should not. Then, it is a stack overflow when you are using ARRAYSIZE as 1500. So as a work around, move the array declaration to outside main() and make it as file global and see[which essentially means, moving your array out of stack]


or Before you run in your shell [or whatever in mac, I dont know], increase the default stack size.

If its a stack problem as ^^^ suggested you could also just allocate the array double* dblData = malloc(ARRAYSIZE * sizeof(double));

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.