Fscanf does not read from file as expected

I have some code where I am trying to read a clique instance from a file. The first and second lines in the instance file indicate the number of vertices and edges, respectively. However, my code doesn't seem to read correctly. This is the part of my code related to the question:

int num_edges=0;
int num_vertices=0;

char *clique_file = "cliqueinstance";
char *mono_file= "monotone2sat";

FILE* fp_input = fopen(clique_file, "r");
FILE* fp_output = fopen(mono_file, "w");

if (fp_input == 0 )
{
    printf( "Could not open input file\n" );
    return 0;
}

if (fp_output == 0 )
{
    printf( "Could not open output file\n" );
    return 0;
}

fscanf(fp_input, "%d ", &num_vertices);
fscanf(fp_input, "%d ", &num_edges);

printf("\n num of vertices = %d, num of edges = %d ", num_vertices, num_edges); fflush(stdout);

      

My clique instance file looks like this:

12
50
<5,1>
etc.

      

I expect my code to read 12 as the number of vertices and 50 as the number of edges, however when I print out what it reads, both values ​​are 0.

I'm not sure why this is happening, any thoughts?

Update my question:

My actual program is a series of cuts. Problem P1 is reduced to problem P2, which is reduced to problem P3, etc. I have four of these shortcuts, each presented with a different function. In my main function, I call each of these four functions sequentially. In each function, I open an input file and write my solution to the output file, which is then the input file for the next reduction and so on.

I notice that if I limit my main function to one function, then I can read from the file as expected. To do this, I will comment on calls to other functions, in turn, recompile, etc. However, if I have multiple uncommented function calls, it is not being read from the file as expected. Does this have to do with how fscanf / fgets works? Is the buffer cleared before it is used on the next function call? Any thoughts / ideas would be appreciated.

+3


source to share


3 answers


Remove the trailing space immediately after %d

in your calls fscanf

.

fscanf(fp_input, "%d", &num_vertices);
fscanf(fp_input, "%d", &num_edges);

      



If you put a space after %d

the first fscanf

, the second will not read the number, unless the trailing space from the first has been consumed (by reading a space, \n

or a tab).

0


source


I ran the posted code using a file containing

12
20

      

using the following function: (just to make it complete)

#include <stdio.h>

int main( void ) 
{
    int num_edges=0;
    int num_vertices=0;

    char *clique_file = "cliqueinstance";
    char *mono_file= "monotone2sat";

    FILE* fp_input = fopen(clique_file, "r");
    FILE* fp_output = fopen(mono_file, "w");

    if (fp_input == 0 )
    {
        printf( "Could not open input file\n" );
        return 0;
    }

    if (fp_output == 0 )
    {
        printf( "Could not open output file\n" );
        return 0;
    }

    fscanf(fp_input, "%d ", &num_vertices);
    fscanf(fp_input, "%d ", &num_edges);

    printf("\n num of vertices = %d, num of edges = %d ", 
        num_vertices, 
        num_edges); 
    fflush(stdout);
    getchar();
    return(0);      
}

      



result:

num of vertices = 12, num of edges = 20

      

Could it be the input file is actually a binary file and not a text file

0


source


I figured out why I was not able to read from the file as expected. I was not careful about closing files at the end of functions. In my program, I have a sequence of steps where the output file of a step is the input file for the next step, and so on. When I ensure that at the end of each step, I close the files with fclose (), my program reads from all files as expected. Thank you!

0


source







All Articles