473,769 Members | 3,872 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("n ick.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 2550
In article <11************ **********@e56g 2000cwe.googleg roups.com>,
"Shuch" <Sh**********@g mail.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("n ick.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("n ick.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\readF ile\windows\Deb ug_Build\readFi le.exe
000000000004912 150000008111417 000000213160000 000413160000000 250000000025811 180000025811160 000058111600000 058111600000049 121500000081114 170000002131600 000004131600000 002500000000258 111800000258111 600000581116000 000581116000000 581116111110811 141700000021316 000000041316000 000025000000002 581118000002581 116000005811160 000005811160000 005811161111111 111111110213160 000000413160000 000250000000025 811180000025811 160000058111600 000058111600000 058111611111111 111111111111111 110413160000000 250000000025811 180000025811160 000058111600000 058111600000058 111611111111111 111111111111111 111111111025000 000002581118000 002581116000005 811160000005811 160000005811161 111111111111111 111111111111111 111111111111110 258111800000258 111600000581116 000000581116000 000581116111111 111111111111111 111111111111111 111111111111111 111102581116000 005811160000005 811160000005811 161111111111111 111111111111111 111111111111111 111111111111111 111111105811160 000005811160000 005811161111111 111111111111111 111111111111111 111111111111111 111111111111111 111111110581116 0000005
------------------------------------------------------------------
THIS IS SOOO RANDOM...wat should i do?????

Apr 1 '06 #3
In article <11************ **********@j33g 2000cwa.googleg roups.com>,
"Shuch" <Sh**********@g mail.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("n ick.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\readF ile\windows\Deb ug_Build\readFi le.exe
000000000004912 150000008111417 000000213160000 000413160000000 250000000025811 180
000025811160000 058111600000058 111600000049121 500000081114170 000002131600000 004
131600000002500 000000258111800 000258111600000 581116000000581 116000000581116 111
110811141700000 021316000000041 316000000025000 000002581118000 002581116000005 811
160000005811160 000005811161111 111111111110213 160000000413160 000000250000000 025
811180000025811 160000058111600 000058111600000 058111611111111 111111111111111 110
413160000000250 000000025811180 000025811160000 058111600000058 111600000058111 611
111111111111111 111111111111111 111025000000002 581118000002581 116000005811160 000
005811160000005 811161111111111 111111111111111 111111111111111 111110258111800 000
258111600000581 116000000581116 000000581116111 111111111111111 111111111111111 111
111111111111111 111102581116000 005811160000005 811160000005811 161111111111111 111
111111111111111 111111111111111 111111111111111 111105811160000 005811160000005 811
161111111111111 111111111111111 111111111111111 111111111111111 111111111111111 110
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("n ick.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\readF ile\windows\Deb ug_Build\readFi le.exe
000000000004912 150000008111417 000000213160000 000413160000000 250000000025811 180000025811160 000058111600000 058111600000049 121500000081114 170000002131600 000004131600000 002500000000258 111800000258111 600000581116000 000581116000000 581116111110811 141700000021316 000000041316000 000025000000002 581118000002581 116000005811160 000005811160000 005811161111111 111111110213160 000000413160000 000250000000025 811180000025811 160000058111600 000058111600000 058111611111111 111111111111111 110413160000000 250000000025811 180000025811160 000058111600000 058111600000058 111611111111111 111111111111111 111111111025000 000002581118000 002581116000005 811160000005811 160000005811161 111111111111111 111111111111111 111111111111110 258111800000258 111600000581116 000000581116000 000581116111111 111111111111111 111111111111111 111111111111111 111102581116000 005811160000005 811160000005811 161111111111111 111111111111111 111111111111111 111111111111111 111111105811160 000005811160000 005811161111111 111111111111111 111111111111111 1111111111111 111111111111111 111111111105811 160000005 ------------------------------------------------------------------
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("n ick.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...jus t 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************ **********@v46g 2000cwv.googleg roups.com>,
"Shuch" <Sh**********@g mail.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

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

Similar topics

5
5468
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. I want to develop a webpage where people can send attachments that are stored on their local PC.
8
2905
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 to load them all into memory. I've tried using vector pushback with reserving, but it was horribly slow. The current method I am using is upon opening the file and reading the number of values, resizing the vectors (I have 3, one for each data...
50
5012
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 -- the character input is a different matter, at least if I want to remain within the bounds of the standard library.
3
9512
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 the end of this message is the inflate method this is where I get stuck I know that I need a byte array but because I am decompressing a string I have no idea of how big the byte array will need to be in the end (the inflate and deflate methods...
29
10433
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 then print out the contents of the 2d array to the screen. I wil also need to append data to the .dat file but mostly right now I'm worrying about getting the info into the 2d array. My .dat file looks like this 1 20000 2 30000 3 40000
7
5560
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, with different file names each time. So i'm writing into the code to ask the user how many files, and what their names are. From each we'll read in 2 lines, then do some math using all of those lines. Then do it again on another set of lines. ...
1
2795
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. Here's my flow of commands: 1. copy a single row of a 2D array 2. change that single row (either add a cell or delete a cell). 3. copy that single row back into the 2D array by the use of a temporary array. So I'm basically copying a single row...
6
5273
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
2196
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 CSV file without any issues only if all the fields are not null. If any field is blank, it moves the values to the left and displays the value under invalid column. Example is shown below: A part of CSV file. I am reading the first row as...
0
9422
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9851
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.