473,324 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

reading file and copying it into array...

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[i][++t]= j+1;

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

}

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

curr[i]= 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.

Apr 1 '06 #1
15 2515
In article <11**********************@e56g2000cwe.googlegroups .com>,
"Shuch" <Sh**********@gmail.com> wrote:
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[i][++t]= j+1;

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

}

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

curr[i]= 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.


--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 1 '06 #2
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[i][++t]= j+1;

//printf(" %d ", Adj[i][j]);

}

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

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

}

-------------------------------------
now the output is comin as:
C:\Documents and
Settings\shuch\cbproject\readFile\windows\Debug_Bu ild\readFile.exe
00000000000491215000000811141700000021316000000041 31600000002500000000258111800000258111600000581116 00000058111600000049121500000081114170000002131600 00000413160000000250000000025811180000025811160000 05811160000005811160000005811161111108111417000000 21316000000041316000000025000000002581118000002581 11600000581116000000581116000000581116111111111111 11102131600000004131600000002500000000258111800000 25811160000058111600000058111600000058111611111111 11111111111111111041316000000025000000002581118000 00258111600000581116000000581116000000581116111111 11111111111111111111111111111025000000002581118000 00258111600000581116000000581116000000581116111111 11111111111111111111111111111111111111102581118000 00258111600000581116000000581116000000581116111111 11111111111111111111111111111111111111111111111110 25811160000058111600000058111600000058111611111111 11111111111111111111111111111111111111111111111111 11111110581116000000581116000000581116111111111111 11111111111111111111111111111111111111111111111111 111111111111105811160000005
------------------------------------------------------------------
THIS IS SOOO RANDOM...wat should i do?????

Apr 1 '06 #3
In article <11**********************@j33g2000cwa.googlegroups .com>,
"Shuch" <Sh**********@gmail.com> wrote:
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[i][++t]= j+1;

//printf(" %d ", Adj[i][j]);

}

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

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

}

-------------------------------------
now the output is comin as:
C:\Documents and
Settings\shuch\cbproject\readFile\windows\Debug_Bu ild\readFile.exe
00000000000491215000000811141700000021316000000041 3160000000250000000025811180
00002581116000005811160000005811160000004912150000 0081114170000002131600000004
13160000000250000000025811180000025811160000058111 6000000581116000000581116111
11081114170000002131600000004131600000002500000000 2581118000002581116000005811
16000000581116000000581116111111111111111021316000 0000413160000000250000000025
81118000002581116000005811160000005811160000005811 1611111111111111111111111110
41316000000025000000002581118000002581116000005811 1600000058111600000058111611
11111111111111111111111111111111102500000000258111 8000002581116000005811160000
00581116000000581116111111111111111111111111111111 1111111111111110258111800000
25811160000058111600000058111600000058111611111111 1111111111111111111111111111
11111111111111111110258111600000581116000000581116 0000005811161111111111111111
11111111111111111111111111111111111111111111111110 5811160000005811160000005811
16111111111111111111111111111111111111111111111111 1111111111111111111111111110
5811160000005
------------------------------------------------------------------
THIS IS SOOO RANDOM...wat should i do?????


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[i][j] << " ";
cout << '\n';
}
}

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 1 '06 #4
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...:((

Apr 1 '06 #5
Shuch wrote:
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[i][++t]= j+1;

//printf(" %d ", Adj[i][j]);

}

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

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

}

-------------------------------------
now the output is comin as:
C:\Documents and
Settings\shuch\cbproject\readFile\windows\Debug_Bu ild\readFile.exe
00000000000491215000000811141700000021316000000041 31600000002500000000258111800000258111600000581116 00000058111600000049121500000081114170000002131600 00000413160000000250000000025811180000025811160000 05811160000005811160000005811161111108111417000000 21316000000041316000000025000000002581118000002581 11600000581116000000581116000000581116111111111111 11102131600000004131600000002500000000258111800000 25811160000058111600000058111600000058111611111111 11111111111111111041316000000025000000002581118000 00258111600000581116000000581116000000581116111111 11111111111111111111111111111025000000002581118000 00258111600000581116000000581116000000581116111111 11111111111111111111111111111111111111102581118000 00258111600000581116000000581116000000581116111111 11111111111111111111111111111111111111111111111110 25811160000058111600000058111600000058111611111111 11111111111111111111111111111111111111111111111111 11111110581116000000581116000000581116111111111111 11111111111111111111111111111111111111 111111111111111111111111105811160000005 ------------------------------------------------------------------
THIS IS SOOO RANDOM...wat should i do?????

Apr 1 '06 #6
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;
}
-----------------------------------------------
rest is the same code....now can ANYONE JUST TELL ME THE MAIN
IDEA...INSTEAD OF ASKING ME SOOOO MANY QUESTIONS...just tell me how can
i write from a file into a 2D ARRAY....if this program is wrong...then
just tell me the correct version of it...

Apr 1 '06 #7
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. "

Apr 1 '06 #8
In article <11**********************@v46g2000cwv.googlegroups .com>,
"Shuch" <Sh**********@gmail.com> wrote:
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...
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 1 '06 #9
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....

Apr 1 '06 #10
In article <11**********************@t31g2000cwb.googlegroups .com>,
"Shuch" <Sh**********@gmail.com> wrote:
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 ];
}

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 1 '06 #11
Shuch wrote:
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
Apr 1 '06 #12
Shuch wrote:
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
Apr 1 '06 #13
THANKS A LOT DANIEL...thanks for all your help throughout..
Regards.

Apr 2 '06 #14
THANKS A LOT DANIEL...thanks for all your help throughout..
Regards.

Apr 2 '06 #15
thanks Larry.

Apr 2 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
8
by: Darsant | last post by:
I'm currently reading 1-n number of binary files, each with 3 different arrays of floats containing about 10,000 values a piece for a total of about 30,000 values per file. I'm looking for a way...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
29
by: yourmycaffiene | last post by:
Okay, this if my first post so go easy on me plus I've only been using C for a couple of weeks. I'm working on a program currently that requires me to read data from a .dat file into a 2d array and...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
1
by: RonLandreth | last post by:
I am writing an accounting system for a class I'm taking at SLU. I need help figuring out the best way to go about copying a 2D array to a temporary 2D array, for eventually copying it back. ...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
0
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.