473,772 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to copy a file into an 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)
{

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[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
3 8990
Sh**********@gm ail.com schrieb:
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* );
You forgot to #include <stdio.h>.
Your code does not compile.
int main()
{

FILE *pf;
int i,r;
if((pf=fopen("n ick.txt","r"))= = NULL){
printf("error opening nick.txt file");
return 1;
1 is no portable return value for main.
EXIT_FAILURE, defined in <stdlib.h> comes to mind, as
does using exit(EXIT_FAILU RE) instead.
}
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 1 '06 #2
Sh**********@gm ail.com wrote:

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.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>

Apr 1 '06 #3

Sh**********@gm ail.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;
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("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)
{

int c,
char buffer[500];
int t;
if (!fscanf (fd,"%d\n",&c))
<OT>
A question:
why do you use a "\n" in fscanf.
</OT>
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 2 '06 #4

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

Similar topics

30
13756
by: franky.backeljauw | last post by:
Hello, I am wondering which of these two methods is the fastest: std::copy, which is included in the standard library, or a manually written pointer copy? Do any of you have any experience with this? I would think that the library function std::copy would perform optimally, as it is a library function, and therefore the writers of this function would know best how to optimize it ... but some tests seem to indicate that my pointer copy...
4
2785
by: Venkat | last post by:
Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>
4
52310
by: fperfect13 | last post by:
Hi, I wanted to perform a deep copy of an array. Searching on google I ran into different opinions : C# Interview Questions (http://blogs.wwwcoder.com/tsvmadhav/archive/2005/04/08/2882.aspx) 10.What's the difference between the System.Array.CopyTo() and System.Array.Clone()?
15
435
by: Kueishiong Tu | last post by:
How do I copy the content of a string in one encoding (in my case big5) to a char array (unmanaged) of the same encoding? I try the following String line = S"123æ°´æ³¥"; char buffer; for(int i=0; i<line->get_length(); i++) {
5
21032
by: DraguVaso | last post by:
Hi, I'm looking for a way to Copy and Paste Files to the clipboard. I found a lot of articles to copy pieces of text and bitmaps etc, but nog whole files. Whay I need is like you have in windows explorer: when you do a right-click on a file and choose Copy, and than paste it somewhere in my application and vice versa.
7
11639
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard is proving to be more difficult. These pictureboxes are bound to an AccessDB. If the user wants to add an image, they select an image using an OpenFileDialog: Dim result As DialogResult = Pic_Sel.ShowDialog() If (result = DialogResult.OK) Then
0
1973
by: Favre Fan | last post by:
Can anyone help me with the array.copy? I have an array coming in from a text file. Info is being read in with my str() string array I then want to copy that array from the third element to the end (seperated by commas). I've also tried the copyTo and can't get that to work either. Array.Copy(str, gradesArray, 3) Why do I keep getting this message? Argument Null Exception was Handled.
2
7026
by: susinthaa | last post by:
Hi, How to copy an array to a file? I tried the File :: copy, But the array is not copying to the file. @susi=$telnet->cmd("logtail"); $newfile = "$movie/$song/$file"; copy(@susi,$newfile) or die "File cannot be copied.";
3
1978
by: Immortal_Nephi | last post by:
Sometimes, unsigned char array is located in the file scope. You define A Object and B Object. A Object and B Object need to share unsigned char array. They are very different object. They are like two chips. A Object modifies data in the unsigned char array. Then, B Object is in turn to modify data in the unsigned char array. Static data member is not the answer. Unsigned char array in the file scope is a bad idea because other...
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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
10264
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
9914
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
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2851
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.