More C Programming help!

Joined
5 Aug 2006
Posts
11,419
Location
Derbyshire
I am learning to write to files in c programming. I have a program that reads a file and can display that file on the screen (it is a little tricky as the file has table-style data in it, so there is an array of structures, with a structure holding a line of data).

If I have this (this is not what I have, but something similar as I want to try myself).
Code:
struct people
{
int age;
double weight, height;
};
struct people people_array[100];
How would I write this to a .dat file??
I think I need a for loop with a counter to increment a counter, so it writes a line, increments, writes next line, increments etc etc until the end of file is found.

This is just a tutorial but I have spent hours trying to find the right approach. It is really bugging me!

Many thanks:)
 
Last edited:
int i;
FPTR pFile = fopen ("tmp.dat", "rw")

for (i = 0; i < 100; i++)
{
/* Write entry to .dat file however you see fit... */
}

fclose(pFILE);



^^ Probably contains at least one error as it's off the top of my head, but should get you pointed in the right direction at least...
 
My lecture notes have plenty of example of writing stuff to .dat files, but nothing on an array of structures!

A 'for' loop is needed I am quite confident on that:)

Whats the FPTR?
iirc I need FILE *output_stream somewhere and an fprintf or sprint f to write to a string?

I see you called the file tmp (any name will do I think), why you written "rw"? r is to read, w to write but can both be done at the same time?

My C skills are not much better than novice I am afraid!
 
Last edited:
you need to do something along the lines of:

Code:
FILE* fp;
fp = fopen("file.bat","rw");
for(i=0;i<100;i++) {
    fprintf(fp, ..........);
}
fclose(fp);

Haven't checked this though ^^^^
 
you need to do something along the lines of:

Code:
FILE* fp;
fp = fopen("file.bat","rw");
for(i=0;i<100;i++) {
    fprintf(fp, ..........);
}
fclose(fp);

Haven't checked this though ^^^^

Changed the rw bit to r and it works:p (No idea what rw means btw!)

Is fclose(fp) absolutely essential?? The compiler shows no errors if it isn't there.
 
The compiler generally only checks that the code will run, not whether it does bad things.

E.g. Leaving the front door wide open when you go to work is a valid thing to do by the rules of the universe, but may cause bad things to happen when you come to perform it.
 
Last edited:
dunno why I thought it was FPTR ¬_¬

If you're unsure how a function works, but know it's name, then read the manual, if you're on linux go to a terminal and type something like 'man fopen' and you'll get a page describing fopen (and probably a couple of other related functions) including what possible arguments there are and what they do. If you're using windows then just google 'man fopen' and you'll get the same thing basically...

and yeah, to expand on what wush is saying, C doesn't have any garbage collection, so leaving pointers open is a big no-no (unlike C# for example where it's fine). So in the case of a FILE * which is opened with the special fopen command, it needs fclose, if you're using a normal pointer that you allocated memory to with malloc/calloc/realloc then you need to use the free function.
 
dunno why I thought it was FPTR ¬_¬

If you're unsure how a function works, but know it's name, then read the manual, if you're on linux go to a terminal and type something like 'man fopen' and you'll get a page describing fopen (and probably a couple of other related functions) including what possible arguments there are and what they do. If you're using windows then just google 'man fopen' and you'll get the same thing basically...

and yeah, to expand on what wush is saying, C doesn't have any garbage collection, so leaving pointers open is a big no-no (unlike C# for example where it's fine). So in the case of a FILE * which is opened with the special fopen command, it needs fclose, if you're using a normal pointer that you allocated memory to with malloc/calloc/realloc then you need to use the free function.

Technically you dont malloc for pointers you just get a pointer back when you malloc.
 
Back
Top Bottom