I have a problem reading from a file into a memory. In the file I have in the same line "float variable, char variable". Now, I want this to be read into a memory.
How to do it??
I have a problem reading from a file into a memory. In the file I have in the same line "float variable, char variable". Now, I want this to be read into a memory.
How to do it??
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
ok, but I want this with pointers.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
And? Did you look at either function? They both take pointers as arguments. Post your attempt, we'll help fix your problems.
Quzah.
Hope is the first step on the road to disappointment.
struct Top10
{
char Name[80]
struct Top10 *next;
}
void READ(struct Top10 **first, char file[])
{
FILE *fin;
float temp[100];
struct Top10 *new;
*first = NULL;
fin = fopen(file, "rt+");
while (fgets(temp, 100, fin) != NULL)
{
new = (struct Top10*) malloc(sizeof(struct Top10));
sscanf(temp, "%s", new->Name);
new->next = *zac;
*first = new;
}
fclose(fin);
}
This function reads name from the "fin". I want this to change that this function will read a float and char variable. How to do it?
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
Well, how long is a float in bytes? How long is a char, in byes? Read that number of bytes in... sheesh.
Float is 4 bytes and char is 1 byte long, but I still don't know how to do it. Please give me an example.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
You will have to use either fgets or fscanf not to mention fread.
fgets will only work with text-files.
Not sure about fscanf. Maybe only text files, check it out.
fprintf to write a formated/non-formated character input.
ssprintf lets you print data directly on a string of chars.
fread it if you wrote it to a non-text file, and fwrite it to the non-text file. fscanf allows you to copy file data directly into your structures, as does fread for non-text files.
Can anyone write me an example?
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
Read 'Smurfs' post.
Last edited by Sebastiani; 12-13-2001 at 06:46 AM.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Okay. Stop wasting time with little iddy-biddy routines. Use your drive like a block device like it was meant to be. This isn't rocket science--
The above isn't complete, and isn't expected to run. It is just enough to show you how you can use the actual calls.Code:// whether you read or write, you need the following things: // // - ram buffer to read/write from/to // - size of smallest unit you are reading/writing // - number of units you are reading or writing // - file you are reading/writing from/to // #define MAXRECORDS 50 typedef struct // format of smallest unit of data in db { int data1; long data3; char letter1; }obj1; char myArrayMAXRECORDS]; // array of arbitrary size obj1 myDataBase[MAXRECORDS]; // array of "records" for db size_t count; // bytes read or written */ FILE *fp; // my file pointer */ void foo(void) { obj1 myRecord; // record-sized "buffer" // assume file has already been opened and 'fp' is valid // further assume that the position has already been set // with fpos() count = sizeof(obj1); count = fwrite(&myRecord,count,1,fp); if(count != sizeof(obj1)) error(WRITE_FAILED); // bytes written didn't match, bail else { // reposition fp to start using fpos() count = (sizeof(obj1) * MAXRECORDS); count = fread(&myDataBase[0],count,1,fp); // or "count = fread(&myDataBase[0],sizeof(obj1),count,fp);" if(count != (sizeof(obj1) * MAXRECORDS)) error(READ_FAILED); // whole db couldn't be read in }; // reposition fp at start using fpos() count = MAXRECORDS; fread(&myArray[0],1,count,fp); // show reading an array }
Everything is bytes. Just bytes. Doesn't matter if it's an array, or a struct, or a union, or a piece of object code-- it is just "x number of bytes" to the computer and to RAM. In other words just a "block" of RAM or "block" of disk space.
Also remember, the format you view everything through, whether you look at memory as an array, or a string, or a malloc()'d pointer, or whatever-- is all the same. Just consecutive bytes. It is _you_ who is change how you look at the same information based on what language constructs you choose to use.
Truthfully, all file read functions eventually call fread(). whether you are reading 1 byte, or a million. Same thing with fwrite(), whether writing 1 byte or a million.
enjoy, and I hope that helps.
Just to nit pick...
count = sizeof(obj1);
count = fwrite(&myRecord,count,1,fp);
There is no need to set 'count' the first time.
count = fwrite(&myRecord,sizeof(obj1),1,fp);
Additionally:
And as such:fwrite
fread and fwrite return the number of items successfully
read or written (i.e., not the number of characters). If
an error occurs, or the end-of-file is reached, the return
value is a short item count (or zero).
if(count != sizeof(obj1))
This is incorrect I think. It would instead be '1', not 'sizeof(obj1)'. Right?
Still, all in all, a nice way to reuse 'count' throughout.
Quzah.
Hope is the first step on the road to disappointment.
>Truthfully, all file read functions eventually call fread().
Okay, then if I wanted to could I use fread to get all of my data? I thought fread was only for binary files, which is why I've never used it before. I'm just a bit confused as to when fread and fwrite should be used as opposed to fgets and fgetc.
pointer = NULL
fgetc is a very useful function if you need to read a single character at a time. If you need a single character from a file, there is no reason to use fread.
fread is very useful for reading blocks of data at a time.
fgets is also a very nice function. It shines when reading text files. It will read upto N-1 characters, or until it encounters a newline character, whatever comes first.
In a nutshell, they're all useful.
Quzah.
Hope is the first step on the road to disappointment.
Please download this file and analize the code. PLEASE.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)