Hi all,
I am relatively new at programming in C so forgive me if this question sound a bit idiotic. I have a problem regarding passing file pointers in a program I am writing. So my input is in the following format from a file:
Code:
0 3 1
0 2
0 3
0 4
1 2
1 3
1 4
2 3
2 4
3 1 4
What I want to do is compare each pair of numbers in each line but NOT do comparisons in between lines. What I have right now looks something like this:
Code:
void betweeness(int n){
int a;
int b;
char line[LINE_MAX];
int flag=0;
char d;
FILE * fin = fopen("path.txt" ,"r");
flag=0;//new line flag
while(fgets(line, LINE_MAX, fin)!=NULL)
{
flag=0;
fputs ( line, stdout );
while((sscanf(line,"%c",&d)) != '\n')
{
if(flag==0)
{
sscanf(line, "%d %d",&a,&b);
printf("Test one %d %d \n" ,a,b);
betweenmat[a][b]=betweenmat[a][b]+1;
a=b;
flag=1;
}
else if(flag==1)
{
sscanf(line, "%d", &b);
printf("Test two %d %d \n" ,a,b);
betweenmat[a][b]=betweenmat[a][b]+1;
a=b;
}
}}
fclose(fin);
FILE * fout = fopen("between.txt" ,"w");
for (a = 0; a < n; ++a) {
for (b = 0; b < n; ++b)
{
fprintf(fout,"%d to %d :%d \n",a,b, betweenmat[a][b]);
}}
fclose(fout);
}
the program is supposed to get a single line(say 0 3 1) then while the end of the line is not reached, it is supposed to scan the pairs of numbers(say 0 3) and add 1 to the matrix element[0,3]. Thus at the end I am supposed to get a matrix which has the number of times every pair appears.
The problem in the code is that the pointer line is not moving forward in the second "while" loop. I tried the following things:
1). using a secondary pointer
2). moving code around
3). various getc, scanf,fgets and other functions
4). blood sacrifice
but none of them are working and either cause the program to go into an infinite loop(like in the case of the code above). Can someone please either guide me in the right path or help me correct the code. I will be most obliged.
Cheers!