I think its not possible because fwrite writes the data in binary form and fscanf reads text data. Pls reply
I think its not possible because fwrite writes the data in binary form and fscanf reads text data. Pls reply
Do you know the difference between text and binary modes? It is possible, results may not be what is expected.
fwrite writes ANY type of data to your file, it may be text or binary. fscanf reads "text" and translates it to the types you want. If the input to fscanf is not text, then "anything" can happen - most likely "not what you wanted" (because there is an almost infinite variety of "not right", but usually only one "right").
So the answer is "it depends". But most likely, you want to use fread to fetch data written with fwrite. That's the "natural" method.
But if you have a text-string or list of numbers in text-form that is written with fwrite, then you can certainly read it with fscanf. If you have a binary integer, it's not going to come back "right" by using fscanf.
--
Mats
And if my data file are numbers in a "matrix form", i.e., organiced in several rows and columns, when a called fscanf the function will save each data of the file in a compoment of a two dimensional array, say loquesea[Nume][Number2]?
I don't understand this last question. fscanf() just reads data as text and tries to parse it as you tell it. It stores each value into each variable you give it, provided all goes well. You can have data arranged anyway you want in the file. fscanf() behaves the same way each time, assuming nothing about the data that you have except what you tell it.
if your code to write the data looks like this:
then your read should look similar, changing to fread insead of fwrite.Code:int loquesea[10][3];
...
fwrite(loquesea, sizeof(loquesea), 1, somefile);
...
If there is processing to make a text-string out of loquesa, then you could use fscanf().
[The datatype of loquesa wouldn't really change matters if it's int, long, char, float, double or some struct - it still would require a fread to read it, unless the data is processed to become text]
--
Mats
Ok; thanks a lot. :) (I love C xD)
I think its not possible because fwrite writes the data in binary form and fscanf reads text data. Pls reply
It's always possible to contrive an fwrite() which can then be read with fscanf(), but for the most part, you use fread()/fwrite() and fprintf()/fscanf() as matched pairs.
To expand a bit on Salem's reply: fwrite writes the data you give it. If you give it a set of strings like this:
"1 Mats PO73EP\n"
then fscanf can read it.
If you give it a struct of this sort:
then fscanf can't (without A LOT of contortion) [1] be made to read it.Code:struct {
int number;
char name[10];
char postcode[8];
};
[1] Using some sort of convoluted construction with many %c, you could PERHAPS do it - but it's quite likely that it will not work reliably at all, and it will certainly not be easy to use nor will it be asy to modify, should you need to add "housenumber" to the list of items in the struct.
--
Mats
Hello again; I revive this topic.
This is the problem: I create a data file, with NSTEPS rows and DIM columns, but I cannot read it using fscanf.
My code is as follows:
I know it is not reading it because I then try to print a[i][j], and all I get is zeros, numbers I don't know where came from, and long strings of digits!Code:for(i = 0; i < NSTEPS; i++){
for(j = 0; j < DIM; j++){
fscanf( fp, "%f \n ", &a[i][j]);
}
}
The numbers in the data file are double, and obviusle I define a[NSTEPS][DIM] as double.
Thanks!
Maybe try %lf then as the conversion, because that is what a double needs - see the manual.
Also, the trailing " \n " of the format adds nothing, "%lf" will suffice.
There is no such thing as "binary form." There is a concept of opening a file in "binary mode" but that's not the issue here. fwrite() and fprintf() both write data to a file. fscanf() reads and formats textual information. If you use fwrite() to write textual information, there is no reason you cannot use fscanf() to read it back in.
Forget this binary vs. text stuff. It has nothing to do with this.
It does't work. My data file, wrt.dat, has the first two lines with two numbers: the first is the number if rows, and the second, the number of colums. My code is like this:
I really don't understand where the failure is; i have found some code using fscanf just like me, and they are supposed to work.Code:
int main(){
double **a;
int i,j;
int *NSTEPS,*DIM;
FILE *fp;
fp=fopen( "wrt.dat", "r");
fscanf(fp, "%d \n", NSTEPS); /* read number of points */
fscanf(fp, "%d \n", DIM); /* read dimension */A
a = malloc( NSTEPS * sizeof( double *));
if ( a == NULL ) {
printf("No hay ubicación de memoria suficiente");
exit(0);
}
for (i = 0; i < NSTEPS; i++) {
a[i] = malloc( DIM * sizeof(double));
if (a[i] == NULL) {
printf("No hay ubicación de memoria suficiente");
exit(0);
}
}
for(i = 0; i < *NSTEPS; i++){
for(j = 0; j < *DIM; j++){
fscanf(fp, "%lf ", &a[i][j] );
printf( "%f", a[i][j] );
}
printf("\n");
}
fclose(fp);
return 0;
}
Your NSTEPS and DIM pointers are pointing to nowhere. Instead, declare them as:
Notice I made these lower-case. You should avoid all uppercase names for anything except preprocessor macros.Code:int nsteps, dim;
And call fscanf() as:
Code:fscanf(fp, "%d", &nsteps); /* read number of points */
fscanf(fp, "%d", &dim); /* read dimension */