473,779 Members | 2,047 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
15 2553
In article <11************ **********@t31g 2000cwb.googleg roups.com>,
"Shuch" <Sh**********@g mail.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_iterato r<int>( file ), istream_iterato r<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
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
5014
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
10437
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
9632
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
10302
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...
1
10071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8958
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...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
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
5372
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
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2867
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.