I'm sorry because I think this is a popular fault but I haven't found why it happened yet.
this is a code that has that fault:

void readfile(){
	FILE *f;
	int u,v;
	f=fopen(fi,"r");
	fscanf(f,"%d%d%d%d",&n,&m,&s,&t);
	for (int i=1;i++;i<=n)
	for (int j=1;j++;j<=n) c[i][j]=(i!=j)? Oo:0;
	for(int i=1;i++;i<=m) fscanf(f,"%d%d%d",&u,&v,&c[u,v]);
	fclose(f);
}

when I compile, no error.
But when I debug, the message appears at line fscanf(f,......)
anyone help me, please?

thanks :)

Recommended Answers

All 8 Replies

I see this problem for some days but not found why I happened yet :( I'm learning pascal, and changing to C/C++, so anyone help me please :)

thanks :)

There are two lines with fscanf().
What are c, n, m, s, and t?
c[u,v] is probably not what you meant.
You are using u and v prior to initialization.

n,m,s,t is integer
c is an array.
I use (n,m,s,t,c) through of my program, so I init their before
And this is clearer version.

#include<stdio.h>
#define fi "input.txt"
#define fo "output.txt"
#define maxN 101
#define maxC 32000
#define Oo 32000
int n,m,s,t;
int c[maxN][maxN];
bool free[maxN];
void readfile(){
	FILE *f;
	int u,v;
	f=fopen(fi,"r");
	fscanf(f,"%d%d%d%d",&n,&m,&s,&t);
	for (int i=1;i++;i<=n)
	for (int j=1;j++;j<=n) c[i][j]=(i!=j)? Oo:0;
	for(int i=1;i++;i<=m) fscanf(f,"%d%d%d",&u,&v,&c[u,v]);
	fclose(f);

And the problem when I run to line 14.
So, is there something wrong, please help me!

thanks :)

It's because you never store any values so they don't have locations in the memory. Assign a value to each of the integers and then you should be able to get their address locations.
Also, you should post the error it hits on line 14, is it an access violation or something? (OH I didn't see the title, my first line is most likely correct then.)

@Nowayz: No, you don't need to initialize the variables for them to have an address.

@hqt:
1. Check wether the file was opened successfully.
2. In line 17 you still use u and v uninitialized which will likely cause an access violation.
3. And you still didn't change the &c[u,v].
4. You should probably use another name for free[].

Sorry for all my silly question :( In those days, I cannot connect to network, so I cannot reply.

#include<stdio.h>
#define fi "input.txt"
#define fo "output.txt"
#define maxN 10
#define maxC 32000
#define Oo 32000
int n,m,s,t;
int c[maxN][maxN] ,trace[maxN],d[maxN];
bool free[maxN];
void readfile(){
	FILE *f;
	int u,v;
	int n,m,s,t;
	f=fopen(fi,"r");
	fscanf(f,"%d%d%d%d",&n,&m,&s,&t);
	for (int i=0;i++;i<=n){
		for (int j=0;j++;j<=n) {
			if (i!=j) c[i][j]=Oo; else c[i][j]=0;
		}
	}
	int tmp;
	for(int i=0;i++;i<=m){
		 fscanf(f,"%d%d%d",&u,&v,&tmp);
		 c[u][v]=tmp;
	}
	fclose(f);

with above code, I run through line15 successful (No error, and every variables assign exactly what I want) ----> I don't know why :(. Before that, I ALWAYS see that error, so who can explain for me why, please.

But now, I have another error with that code: when I start to debug, I see that the highlight go over line17 to line21 (and I see array C no change). And when run to line23, the error appear again: an access violation raised in your program.

And I still have some question: From line 16-->18: if I write again:

int i,j; //declare i,j before
	for (i=0;i++;i<=n){
		for (j=0;j++;j<=n){
			c[i][j]=(i!=j)? Oo:0;
		}

the loop will loop from i=1 to j=Oo (It means It loop again and again, and j will increase forever !!!!!)

and If I write:

for (int i=1;i++;i<=n){
		for (int j=1;j++;j<=n){
			c[i][j]=(i!=j)? Oo:0;
		}

the loop will loop again and again between two line 2 and 3 too !!! but I see i and j is no change >,<

All above questions I'm thinking so long and doesn't have answered yet :(
@:I'm starting to learning C/C++. before that, I learned Pascal, I don't know why I always see C/C++ harder Pascal. (specially in debugger :( )

Declarations of for loops have three sections that are in a specific order:

for({initialiser}; {break_condition}; {increment/decrement})

But the increment and conditional sections in all of your for loops are in the wrong place. And because you're putting things in the wrong place, your loops are not doing what you're expecting them to!

In all of your for loops you are doing this:

for(int i=1; i++; i<=n) // for(init; increment; break_cond)

When you should be using:

for(int i=1; i<=n; i++) // for(init; break_cond; increment)
commented: good :) +1

Oh, :-O I don't know what word to say to you !!! thank you very much. A answer very easy and very obvious.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.