reading file and copying it into array...

S

Shuch

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)
{

char buffer[500];

int t;
if (!fscanf (fd,"%d\n",&n))
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
 
D

Daniel T.

"Shuch said:
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)
{

char buffer[500];

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

The above line changes the value of 'n' to whatever the first value in
your file is (ie 0.)
printf("error reading the file");

Your loop below is never entered because at this point, 'n' equals 0.
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.
 
S

Shuch

but still...after makin those changes..now when i m tryin to
print...its printin some nonsense..:((...here is the code again...:
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)
{

char buffer[500];
int c;
int t;
if (!fscanf (fd,"%d\n",&c))
printf("error reading the file");
// do{
//}while (c != EOF);*/
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]);

}

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

curr= 1;
}
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cout<<Adj[j];


fclose(fd);

}
 
D

Daniel T.

"Shuch said:
but still...after makin those changes..now when i m tryin to
print...its printin some nonsense..:((...here is the code again...:
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)
{

char buffer[500];
int c;
int t;
if (!fscanf (fd,"%d\n",&c))
printf("error reading the file");
// do{
//}while (c != EOF);*/
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]);

}

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

curr= 1;
}
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cout<<Adj[j];


fclose(fd);

}

-------------------------------------
now the output is comin as:
C:\Documents and
Settings\shuch\cbproject\readFile\windows\Debug_Build\readFile.exe
000000000004912150000008111417000000213160000000413160000000250000000025811180
000025811160000058111600000058111600000049121500000081114170000002131600000004
131600000002500000000258111800000258111600000581116000000581116000000581116111
110811141700000021316000000041316000000025000000002581118000002581116000005811
160000005811160000005811161111111111111110213160000000413160000000250000000025
811180000025811160000058111600000058111600000058111611111111111111111111111110
413160000000250000000025811180000025811160000058111600000058111600000058111611
111111111111111111111111111111111025000000002581118000002581116000005811160000
005811160000005811161111111111111111111111111111111111111111111110258111800000
258111600000581116000000581116000000581116111111111111111111111111111111111111
111111111111111111102581116000005811160000005811160000005811161111111111111111
111111111111111111111111111111111111111111111111105811160000005811160000005811
161111111111111111111111111111111111111111111111111111111111111111111111111110
5811160000005


First, you should past the actual code rather than what you have above.
Second, you should consider posting that code in a C newsgroup instead
of a C++ newsgroup.

What is it you are trying to do? Not exactly, but in an abstract sense.
Is this the only input file the code will ever have to read? If not,
will other files all have the same width and height? If not, will they
all be squares?

Frankly, the easiest solution to your problem would be something like:

const int Adj[7][7] = { { 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 } };

int main()
{
for ( int i = 0; i < 7; ++i ) {
for ( int j = 0; j < 7; ++j )
cout << Adj[j] << " ";
cout << '\n';
}
}
 
S

Shuch

i know thats the easiest solun...but i dont have to do it...i have to
read from the input file thats called nick.txt...n it has the contents
as i mentioned above...no the size can change...the size of the file is
defined at the beginning...the size of the file is unknown...we dunno
how much it can be...n yeah it will always b in squares...
n i had already posted it in C but no one replied to me :(....i really
need a way out of this problem...but cant understand how to read it
from the file into an array....there isnt any solution that i can
follow...i dont have to have it in C...i can do it in C++ as well...do
u have some other idea that i can use...already i have spent sooo many
hours now...but cant figure it out...:((
 
L

Larry I Smith

Shuch said:
but still...after makin those changes..now when i m tryin to
print...its printin some nonsense..:((...here is the code again...:
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)
{

char buffer[500];
int c;
int t;

Where is 'n' defined?
Where is a value assigned to 'n'?
if (!fscanf (fd,"%d\n",&c))
printf("error reading the file");
// do{
//}while (c != EOF);*/
for (int i=1; i<=n;i++){

'n' has not been defined, or had its value set.
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]);

}

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

curr= 1;
}
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cout<<Adj[j];


fclose(fd);

}

-------------------------------------
now the output is comin as:
C:\Documents and
Settings\shuch\cbproject\readFile\windows\Debug_Build\readFile.exe
0000000000049121500000081114170000002131600000004131600000002500000000258111800000258111600000581116000000581116000000491215000000811141700000021316000000041316000000025000000002581118000002581116000005811160000005811160000005811161111108111417000000213160000000413160000000250000000025811180000025811160000058111600000058111600000058111611111111111111102131600000004131600000002500000000258111800000258111600000581116000000581116000000581116111111111111111111111111104131600000002500000000258111800000258111600000581116000000581116000000581116111111111111111111111111111111111110250000000025811180000025811160000058111600000058111600000058111611111111111111111111111111111111111111111111102581118000002581116000005811160000005811160000005811161111111111111111111111111111111111111111111111111111111025811160000058111600000058111600000058111611111111111111111111111111111111111111111111111111111111111111111058111600000058111600000058111611111111111111111111111111111111111111111111111111 111111111111111111111111105811160000005
 
S

Shuch

n is defined in my FIRST POST...as u can c at the declaration part....

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;
}
 
S

Shuch

and also as Daniel said its a C code....i just got a reply from
comp.lang.c....saying
"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. "
 
D

Daniel T.

"Shuch said:
i know thats the easiest solun...but i dont have to do it...i have to
read from the input file thats called nick.txt...n it has the contents
as i mentioned above...no the size can change...the size of the file is
defined at the beginning...the size of the file is unknown...we dunno
how much it can be...n yeah it will always b in squares...
n i had already posted it in C but no one replied to me :(....i really
need a way out of this problem...but cant understand how to read it
from the file into an array....there isnt any solution that i can
follow...i dont have to have it in C...i can do it in C++ as well...do
u have some other idea that i can use...already i have spent sooo many
hours now...but cant figure it out...:((

OK, so you are given a file with X integers per line, X lines long and
you need to read it into an array. Is that right? Maybe if you printed
the assignment exactly as written...
 
S

Shuch

exactly Daniel...this is exactly my problem...i have no clue wat is
there in my input file...its just some input file with X number of rows
and X number of columns...and i have to read the input file and store
them into an array....
 
D

Daniel T.

"Shuch said:
exactly Daniel...this is exactly my problem...i have no clue wat is
there in my input file...its just some input file with X number of rows
and X number of columns...and i have to read the input file and store
them into an array....

#include <algorithm>
#include <fstream>
#include <iterator>
#include <vector>

using namespace std;

int main() {
ifstream file( "nick.txt" );
vector<int> array;
copy( istream_iterator<int>( file ), istream_iterator<int>(),
back_inserter( array ) );
int length = sqrt( array.size() );

// to find the value of a particular coordinate:
// array[ y * length + x ];
}
 
L

Larry I Smith

Shuch said:
n is defined in my FIRST POST...as u can c at the declaration part....


No, I could not see; you snipped part of the code in seubsequent
posts.

Nit: this is not an IM site; it is 'you' not 'u', and 'see' not 'c'.

Have a nice day

[snip]

Larry
 
L

Larry I Smith

Shuch said:
and also as Daniel said its a C code....i just got a reply from
comp.lang.c....saying
"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. "


The 'using' directive is the only C++ specific line in
the code; all of the rest is C...

If 'MAX' is pre-define (hard coded), then read an
entire line into a buffer (use fgets() perhaps),
then use sscanf() to extract the elements for one row
from the line you just read.
Repeat that loop (read line/extract elements from line)
until you have read and processed all of the lines in the
file.

Larry
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top