Need help in file I/O

Started by
4 comments, last by Heimdall99 23 years, 11 months ago
I am trying to write a simple program that manages my CDs. The problem is that I cannot find info on how to read/write structures to and from files. Right now I have a struct song {char name[]; char artist[]; double time;}; i need to find a way to save an array of those to a file, and later on, to read that array from file. How do I do that ? All I found is how to do so with same data types. Thought about using fread, but it needs values for size and count Is there another way? I am trying to write this in C. Any help will be much appreciated.
Advertisement
you can use fread and fwrite to read/write your own sturctures to and from disk just use the struct as sizer and 1 for count to read/write on of your structures
example:

struct song {char name[];
char artist[];
double time;
};

song song01;

then save to file using:
fwrite(&song01, sizeof(song), 1, fp);

reading goes the same way, only thing I'm not sure about is if the arrays inside the stuct without sizes will work, myabe just use an constant array size like 200 or something
don't think memory/diskspace is important in this case
I this helps you (a bit)...

chris


Edited by - Christiaan on May 10, 2000 3:53:47 AM
Hey Heimdall99,

It might not look so hightech, but why not use fscanf and fprintf?
It''s very practical, and uses text files you can easy read and change! Also good for future re-use and understanding. scanf is more powerfull than I thought! For example to read an integer from: "the index: [11]\n"
use: fscanf(F, "the index: [%i]\n", &iIndex);

Hope it helps,
Bas.
I don''t think fsanf would work, since I am trying to save structs (what identifier would I use with that?). Maybe I can just save the whole array of structs and try to read/write IT. The whole problem is with the count of items in the file that I have to provide for fread. With the single array I would only have 1.
>> I don''t think fsanf would work, since I am trying to save structs (what identifier would I use with that?) <<

Sure you can use fscanf() and fprintf() with struct. You''ll have to save your structs like this:


struct song {char name[];
char artist[];
double time;
}song01;

fprintf(fp, "Name: %s Artist %s Time %f", song01.name, song01.artist, song01.time);
}


And read them like this:

struct song {char name[];
char artist[];
double time;
}song01;

fscanf(fp, "Name: %s Artist %s Time %f", song01.name, song01.artist, &song01.time);
}

/. Muzzafarath
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Your file doesn''t have to be structured very rigidly. You can include header sections so that in the future you can use different data structures. Your header section should have as the first bit of information a version number. This will tell you what the rest of the section should contain. Next, I would include the number of data elements that follow. After that, you have your data elements.

Now, reading this information back in, you read the first element. It''s a zero. You must''ve written data using version 0. The next thing you do is based on this 0. If it was a 1, and you didn''t implement version 1, you have an error and don''t bother reading the file. Otherwise, we can do version 0''s implementation (described in the above paragraph). Read in the number of data elements. Set aside enough memory. Do a simple for - next loop to read in that many elements.

Here''s where something interesting can happen. You can read the next header section... this allows you to have as many different pieces (and types) of information as you want, all stored in a single file. MFC does something similar to this in its file handling functions.

-MartinJ
Better living through creative confusion.

This topic is closed to new replies.

Advertisement