how to copy a file into an array?

Joined
Feb 4, 2010
Messages
28
Reaction score
0
Hi all,
i m trying to read from a file and then copy it into an array...my
code is as follow..it runs fine but i cant understand y it doesnt show
me any output??
here is my code...
using namespace std;
int Adj[MAX][MAX]; //The matrix of adjacent nodes
int curr[MAX]; //it gives the current index to retrieve the
neigbours of a node
int n=100;
void read_file(FILE* );
int main()
{

FILE *pf;
int i,r;


if((pf=fopen("nick.txt","r"))== NULL){
printf("error opening nick.txt file");
return 1;
}


read_file(pf); //function to create the matrix Adj[][]


next_neighbour(1);
return 0;
}


void read_file(FILE *fd)
{

int c,
char buffer[500];


int t;
if (!fscanf (fd,"%d\n",&c))
printf("error reading the file");
for (int i=1; i<=n;i++){
fgets (buffer, sizeof(buffer), fd);


t=0;
for (int j=0; j<n;j++){
printf(" %d ", buffer[j]-'0');


if((buffer[j]-'0')== 1)


{
Adj[++t]= j+1;


printf(" %d ", Adj[j]);
//cout<<Ad[j].....but nothin works :(


}


}
//printf("\n");
//printf("\n");


curr= 1;
}


fclose(fd);


}
-----------------------------------------
the contents of my file looks like as follow:
nick.txt:
0 3 -1 3 -1 -1 -1
3 0 2 -1 -1 -1 -1
-1 2 0 2 2 -1 -1
3 -1 2 0 6 -1 -1
-1 -1 2 6 0 4 3
-1 -1 -1 -1 4 0 -1
-1 -1 -1 -1 3 -1 0
--------------------------------------------------
i want this input to b stored inside an array....how can i read this
file and store it inside array Adj??????any help will b greatly
appreciated...
regards.
 

CBFalconer

Usenet Archive
Joined
Jul 23, 2001
Messages
12,381
Reaction score
0
i m trying to read from a file and then copy it into an array...my code is as follow..it runs fine but i cant understand y it doesnt show me any output?? here is my code...
using namespace std;

That is C++. Out the door, down the aisle, second door on the right. Around here we deal only with the C language.
 

Michael Mair

Usenet Archive
Joined
May 23, 2004
Messages
1,563
Reaction score
0
Hi all,
i m trying to read from a file and then copy it into an array...my code is as follow..it runs fine but i cant understand y it doesnt show me any output??
here is my code...

using namespace std;

This is comp.lang.c where we discuss standard C.
The above is a using directive; C++ knows these, C does not. If you have a C++ language, ask in comp.lang.c++; C++ stream handling differs from C file I/O. <OT> Have a look at the comp.lang.c++ FAQ to find out why "using namespace XYZ;" on file level is a bad thing and what better ways there are. </OT> If you are writing C programmes and compile them with a C++ compiler: Don't do that. It may lead to subtle errors or make perfectly valid C code uncompilable for you.

int Adj[MAX][MAX]; //The matrix of adjacent nodes
int curr[MAX]; //it gives the current index to retrieve the neigbours of a node

This nicely illustrates why you should not use C99/C++
style line comments when posting to usenet: The line breaks and the code can no longer be compiled. (Worse: If it still can be compiled, it may do something different..)
int n=100;
void read_file(FILE* );

int main
{

FILE *pf;
int i,r;

if((pf=fopen("nick.txt","r"))== NULL){
printf("error opening nick.txt file");
return 1;

1 is no portable return value for main.
}

read_file(pf); //function to create the matrix Adj

next_neighbour(1);

1) next_neighbour is not defined here.
2) You do not fclose(pf) on the same "level" as you opened it.
This may be a design flaw.
return 0;
}

void read_file(FILE *fd)
{

int c,
char buffer[500];

You are not posting real code.
Come back with a minimal compiling example. In C.
Copy and paste the real code.

Cheers
Michael
 

Captain Winston

Usenet Archive
Joined
Mar 21, 2006
Messages
6
Reaction score
0
Hi all,
i m trying to read from a file and then copy it into an array...my code is as follow..it runs fine but i cant understand y it doesnt show me any output?? here is my code...
using namespace std;

It's used in C++, this is comp.lang.c, so it is off topic, please send the thread to a C++ newsgroup, such as comp.lang.c++.
int Adj[MAX][MAX]; //The matrix of adjacent nodes
int curr[MAX]; //it gives the current index to retrieve the neigbours of a node int n=100; void read_file(FILE* ); int main {

FILE *pf;
int i,r;

if((pf=fopen("nick.txt","r"))== NULL){
printf("error opening nick.txt file");
return 1;
}

read_file(pf); //function to create the matrix Adj

next_neighbour(1);
return 0;
}

void read_file(FILE *fd)
{

int c,
char buffer[500];

int t;
if (!fscanf (fd,"%d\n",&c))

<OT>
A question:
why do you use a "\n" in fscanf.
printf("error reading the file");
for (int i=1; i<=n;i++){
fgets (buffer, sizeof(buffer), fd);

t=0;
for (int j=0; j<n;j++){
printf(" %d ", buffer[j]-'0');

if((buffer[j]-'0')== 1)

{
Adj[++t]= j+1;

printf(" %d ", Adj[j]);
//cout<<Ad[j].....but nothin works :(

}

}
//printf("\n");
//printf("\n");

curr= 1;
}

fclose(fd);

}
the contents of my file looks like as follow:
nick.txt:
0 3 -1 3 -1 -1 -1
3 0 2 -1 -1 -1 -1
-1 2 0 2 2 -1 -1
3 -1 2 0 6 -1 -1
-1 -1 2 6 0 4 3
-1 -1 -1 -1 4 0 -1
-1 -1 -1 -1 3 -1 0
i want this input to b stored inside an array....how can i read this file and store it inside array Adj??????any help will b greatly appreciated...
regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,042
Messages
3,104,223
Members
248,131
Latest member
Tonypshaw1

Latest Threads

Top