Cannot read input file correctly with fscanf...

Hi, I'm trying to check if the code scans correctly the data from the input file, which is like this...

1 1 1723 1 2 1
1 4 2158 4 27 2
1 1 1567 1 7 1
1 1 1908 1 5 1
1 4 1481 4 24 2
1 1 3217 1 1 1
1 4 2358 4 25 2
1 1 1252 1 12 1
1 1 1281 1 9 1


Supossedly, it must be the same in the output file, because I'm just printing what it scanned, to check if it is doing it correctly, but the output data that I get is like this...

4288944 4288852 0.000000 4288848 4288908 4288892
4288944 4288852 0.000000 4288848 4288908 4288892
4288944 4288852 0.000000 4288848 4288908 4288892
4288944 4288852 0.000000 4288848 4288908 4288892
4288944 4288852 0.000000 4288848 4288908 4288892
4288944 4288852 0.000000 4288848 4288908 4288892
4288944 4288852 0.000000 4288848 4288908 4288892
4288944 4288852 0.000000 4288848 4288908 4288892
4288944 4288852 0.000000 4288848 4288908 4288892

I'm using Visual C++ 2008, when compiling no errors appear, it runs fine, so when I was trying to verify if this part of my program worked correctly I realized that it is not scanning right. So I suppose I'm not using correctly the fscanf function or something, I've tried specifing the integer with %i or with %d or even %hi, but I got the same output.

The code I'm using is...

#include <iostream>
FILE *in1, *out1;

int TotEnc = 120;
int i = 0;
int ImageEACC;
int ImageERESP;
float ImageERT;
int RespCor;
int StimNum;
int StimTypeE;

void main(int argc, char *argv[])
{
static char col_enc[30];
static char results[30];

strcpy_s(col_enc, "col_enc_e-");
strcpy_s(results, "res_ctx_erp_");

if (argc < 2)
printf ("Sorry, I need an argument.\n");

else
{
strcat_s(col_enc, argv[1]);
strcat_s(results, argv[1]);
strcat_s(col_enc, ".txt");
strcat_s(results, ".txt" );

in1 = fopen(col_enc, "r");
out1 = fopen(results, "w");
fseek( in1, 0L, SEEK_SET );
fseek( out1, 0L, SEEK_SET );

while (i < TotEnc)
{
i++;
fscanf( in1, "%i", &ImageEACC );
fprintf( out1, "%i ", &ImageEACC );
fscanf( in1, "%i", &ImageERESP );
fprintf( out1, "%i ", &ImageERESP );
fscanf( in1, "%f", &ImageERT );
fprintf( out1, "%f ", &ImageERT );
fscanf( in1, "%i", &RespCor );
fprintf( out1, "%i ", &RespCor );
fscanf( in1, "%i", &StimNum );
fprintf( out1, "%i ", &StimNum );
fscanf( in1, "%i", &StimTypeE );
fprintf( out1, "%i\n", &StimTypeE );

fseek( in1, 0L, SEEK_SET );
}
fclose( in1 );
fclose( out1 );
}
}


Can you help me understand where does the data printed in the output come from, or what am I missing, or doing wrong. Thank you very much in advance...

Best regards!!!

Cin




When doing I/O operations, you need to check for errors every step of the way. So, after every fopen(), fscanf(), fprintf(), fseek(), etc. check the return value for error conditions (and ferror() if needed) and report any errors encountered.
Thanks for your advice, that is what I intended by checking if the code was scanning the data correctly when trying to print it, although as you suggest I must check every step deeper. I've been trying to check the return value of each function I use in the code, as far as I've got it does open the file correctly. But when I tried to check the return value of the fscanf function this error appeared...

Compiler Error C2660
'fscanf' : does not take 0 arguments

I've been looking how to fix it, but as far as I have found it is caused because the function is called with an incorrect number of parameters.

I'm not sure if I'm checking the return value correctly, I added this while after each fscanf:

fscanf( in1, "%i", &ImageEACC );
while( true )
{
int ImageEACC = fscanf( /* ... */ );
if( ImageEACC == 1 ) {
} else if( feof( in1 ) )
break;
}

Is it the correct way to do it?

Thank you, and I apologize if I result annoying, I suppose my questions are too obvious, or something, since only you replied me. I've been searching in the forums before I posted, but I really didn't find an answer that help me understand what am I doing wrong.

Sincerely,

Cin
Cin,

The error is telling you that this line of your code doesn't work:
int ImageEACC = fscanf( /* ... */ );
I believe the problem is that %i is not a format specifier for fscanf, you want %d for integers.
btw- this isn't the actual code line is it? I mean the actual code line looks like the fscanf right before the loop right?

What you should do is make a normal call to fscanf, but use ImageEACC as the return value. Then you can check ImageEACC for EOF or for the number of items successfully scanned.
So
int ImageEACC;
ImageEACC = fscanf(in1,"%d", &ImageEACC);
Hi Cin,

You have used fscanf correctly in fscanf( in1, "%i", &ImageEACC ); But int ImageEACC = fscanf( /* ... */ );
definitely looks wrong.

Last edited on
Hi, thank you both (jpeg and kevinchkin) for your replies...

Yes I know the error message refers to the line that includes

int ImageEACC = fscanf( /* ... */ );

That is why I'm not sure if I was checking the return value correctly (since PanGalactic suggested me to check the return value of each function I use in the code), in the actual code I didn't include the while I mentioned in my second post, I tried to use that while to check the return value of the fscanf.

As I mentioned in my first post, where the actual code is, I've also used %d to specify the integer, but the result is the same as with %i.

I've done what you suggested...

The code with the modifications I've made:

#include <iostream>
#include <stdio.h>
FILE *in1, *out1;

int TotEnc = 120;
int i = 0;
int ImageEACC;
int ImageERESP;
float ImageERT;
int RespCor;
int StimNum;
int StimTypeE;

void main(int argc, char *argv[])
{
static char col_enc[30];
static char results[30];

strcpy_s(col_enc, "col_enc_e-");
strcpy_s(results, "res_ctx_erp_");

if (argc < 2)
printf ("Sorry, I need an argument.\n");

else
{
strcat_s(col_enc, argv[1]);
strcat_s(results, argv[1]);
strcat_s(col_enc, ".txt");
strcat_s(results, ".txt" );

in1 = fopen(col_enc, "r");
out1 = fopen(results, "w");
fseek( in1, 0L, SEEK_SET );
fseek( out1, 0L, SEEK_SET );

while (i < TotEnc)
{
i++;
ImageEACC = fscanf( in1, "%d", &ImageEACC );
fprintf( out1, "%d ", &ImageEACC );
ImageERESP = fscanf( in1, "%d", &ImageERESP );
fprintf( out1, "%d ", &ImageERESP );
ImageERT = fscanf( in1, "%f", &ImageERT );
fprintf( out1, "%f ", &ImageERT );
RespCor = fscanf( in1, "%d", &RespCor );
fprintf( out1, "%d ", &RespCor );
StimNum = fscanf( in1, "%d", &StimNum );
fprintf( out1, "%d ", &StimNum );
StimTypeE = fscanf( in1, "%d", &StimTypeE );
fprintf( out1, "%d\n", &StimTypeE );

fseek( in1, 0L, SEEK_SET );
}

fclose( in1 );
fclose( out1 );
}
}


When compiling no errors appear:

------ Build started: Project: prueba1, Configuration: Debug Win32 ------
Linking...
Embedding manifest...
Build log was saved at "file://c:\Documents and Settings\Cin\Mis documentos\Visual Studio 2008\Projects\prueba1\prueba1\Debug\BuildLog.htm"
prueba1 - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

But the result is the same, the output still has data like this...

4288916 4288856 0.000000 4288848 4288912 4288896
4288916 4288856 0.000000 4288848 4288912 4288896
4288916 4288856 0.000000 4288848 4288912 4288896
4288916 4288856 0.000000 4288848 4288912 4288896
4288916 4288856 0.000000 4288848 4288912 4288896
4288916 4288856 0.000000 4288848 4288912 4288896
4288916 4288856 0.000000 4288848 4288912 4288896


And it should be the same as the data from the input file which is like this...


1 1 1723 1 2 1
1 4 2158 4 27 2
1 1 1567 1 7 1
1 1 1908 1 5 1
1 4 1481 4 24 2
1 1 3217 1 1 1
1 4 2358 4 25 2


So I still don't know what am I doing wrong :(

Any other suggestion?

Thank you very much!!!

Cin
Well... finally I found out what was the problem...

Simply, the code wasn't printing correctly the data because of...

The unnecesary '&' I was using...

fprintf( out1, "%d ", &ImageEACC );

The correct way is...

fprintf( out1, "%d ", ImageEACC );

The ampersand is only needed when scanning the data, but not when printing it.

Well, I really hope this might result useful for someone getting started with c++.

Sincerely, thanks to the people who made suggestions and tried to help me... deeply thnkful!!

:D

Cin



Topic archived. No new replies allowed.