input file problem

I write a code for a problem. My roblem is:
Enter n x m integers from text file
Arrange above integers on the way from from outside to inside in a certain order of the matrix nx m.
Export results to a text file.
I don't know how to make "input.txt" to make the program run perfectly. If u can, please send me ur file "input.txt"
Here is my code:

#include <stdio.h>

#define MAXN 110

int a[MAXN][MAXN], m, n;

void input(){
	scanf("%d %d", &n, &m);
}

void initial(){
	int i;
	for (i = 0; i<=n+1; ++i){
		a[i][0]=-1;
		a[i][m+1]=-1;
	}
	for (i = 0; i<=m+1; ++i){
		a[0][i]=-1;
		a[n+1][i]=-1;
	}
}

void solve(){
	int dx[] = {-1, 0 , 1, 0};
	int dy[] = {0, 1, 0, -1};
	int tx = 1, ty = 1, dir = 0, i;
	a[1][1] = 1;
	for (i = 2; i<=n*m; ++i){
		while (a[tx+dx[dir]][ty+dy[dir]]){
			dir = (dir+1)%4;
		}
		tx = tx + dx[dir];
		ty = ty + dy[dir];
		a[tx][ty] = i;
	}
}

void output(){
	int i, j;
	for (i = 1; i<=n; ++i){
		for (j = 1; j<=m; ++j){
			printf("%4d ", a[i][j]);
		}
		printf("\n");
	}
}

int main(){
	freopen("D:\input.txt", "r", stdin);
	freopen("D:\output.txt", "w", stdout);
	input();
	initial();
	solve();
	
	output();
	return 0;
}
Last edited on
you are using c. I dont know how it works in c but I had a similar problem in c++ and found out this that works. try this;
int main()
{
freopen("C:\\Users\\Public\\input.txt", "r", stdin)
}

notice how I used double slash (\\) instead of single slash(\).
hope it was helpful.
Topic archived. No new replies allowed.