473,406 Members | 2,707 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,406 software developers and data experts.

How can I know how many elements are there in multi-dimentional array?

From my code below, it will open a file specified in argv[1], then
read line by line and store each line in an array call IPTemp.
After reading until EOF, it will return array IPTemp to main, which
you'll see a for loop here "for(i=0;i<6;i++)" ... I use 6 here,
assuming there's 6 lines in the file I opened. How can I know exactly
how many elements in my array? Please help...thanks a lot.
#include <stdio.h>

#define MAXLINES 200
#define MAXLINELENGTH 30

char (*ReadIPFile(int argc, char * argv))[MAXLINELENGTH];

int main(int argc, char * argv[]){
char (*IP)[MAXLINELENGTH];
int i, j;

IP = ReadIPFile(argc, argv[1]);

//take out newline character
for(i=0;i<6;i++){ //<------- problem here
j = strlen(IP[i]);
if (IP[i][j - 1] == '\n') {
IP[i][j - 1] = '\0';
}
}

return 0;
}

char (*ReadIPFile(int argc, char * argv))[MAXLINELENGTH]{

FILE * IPaddr;
char buff[30];
int count=0, i, j;
static char IPTemp[MAXLINES][MAXLINELENGTH];

IPaddr = fopen(argv, "r");

if(!IPaddr){
printf("Error opening file\n");
exit(1);
}

i = 0;

//store each line to IPTemp[i]
while (!feof(IPaddr)) {
fscanf(IPaddr, "%s", IPTemp[i]);
if (IPTemp[i] == NULL){
if(i != 0){
i--;
}
}
else {
i++;
}

}
fclose(IPaddr);

return IPTemp;

}
Nov 13 '05 #1
2 3071


Abby wrote:
From my code below, it will open a file specified in argv[1], then
read line by line and store each line in an array call IPTemp.
After reading until EOF, it will return array IPTemp to main, which
you'll see a for loop here "for(i=0;i<6;i++)" ... I use 6 here,
assuming there's 6 lines in the file I opened. How can I know exactly
how many elements in my array? Please help...thanks a lot.
#include <stdio.h>

#define MAXLINES 200
#define MAXLINELENGTH 30

Instead of fixing the maxlines and maxstringlength, you would
create a struct like

struct IP
{
char **ip;
unsigned total;
};

Then dynamically allocate the file lines into the struct. Member ip
is an array of strings of the ips and the member total represents the
total lines read and stored in the array. The max quantity of data
stored in the array would be moderated by the available memory.

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #2

"Abby" <ab*****@yahoo.com> wrote in message
news:b4*************************@posting.google.co m...
From my code below, it will open a file specified in argv[1], then
read line by line and store each line in an array call IPTemp.
After reading until EOF, it will return array IPTemp to main, which
you'll see a for loop here "for(i=0;i<6;i++)" ... I use 6 here,
assuming there's 6 lines in the file I opened. How can I know exactly
how many elements in my array? Please help...thanks a lot.
#include <stdio.h>

#define MAXLINES 200
#define MAXLINELENGTH 30

char (*ReadIPFile(int argc, char * argv))[MAXLINELENGTH];

int main(int argc, char * argv[]){
char (*IP)[MAXLINELENGTH];
int i, j;

IP = ReadIPFile(argc, argv[1]);

//take out newline character
for(i=0;i<6;i++){ //<------- problem here
j = strlen(IP[i]);


You can use malloc( ) to allocate memory dynamically.

Method 1: Find out the number of lines in the target file, and allocate
memory for "char **line". It hold the char pointers for each line of the
file (a array containing many string address), then you can use for-loop to
read the file and allocate memory for each line everytime.

line[index] = malloc(line_length);

Method 2: Use linked list structure to hold the lines. It is efficient and
good for adding elements at runtime.
--
Jeff
-je6543 at yahoo.com
Nov 13 '05 #3

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

Similar topics

7
by: Rodrigo Daunaravicius | last post by:
Is there an elegant way to directly refer the 2nd dimension of a multi-dimensional sequence (like the nth character in a list of strings). An example would be deleting the newline in all the...
0
by: DP | last post by:
(2nd post, I think my first may have been to the wrong group - sorry) Hello Perl-ers - I´m hoping I can get some help here, because I'm very lost. Don't know Perl, I'm not a programmer. And...
3
by: kj | last post by:
When I run the attached query, I get duplicates when there is one to many relationship between tableA and tableB. The query, tested schema and the result is attached. Sorry for the long post. ...
0
by: Ron Harter | last post by:
I have an XML DOM document containing several elements. 2 of the elements are multi valued. When I parse the document using XPATH (MSXML control) I am able to retrieve only one of the elements. the...
8
by: mikea_59 | last post by:
I am having trouble combining similar elements. Elements can be combined if they have the same name and the same attribute values. I can handle single level elements but am having problems with...
20
by: Simon Harvey | last post by:
Festive greetings fellow programmers! I've been programming now for about 4, maybe 5 years now. 4 of those years were at university so and I havent had much work experience of making real world...
7
by: Nick Calladine | last post by:
Hi On my form i have multiple select which all have an id value total1, total2, total3 etc so i am trying to detect how many there are and then use this to caculate a total. Is there a...
6
by: BerkshireGuy | last post by:
I need to create a query that will filter out records based on records. There can be many combinations depending of what the uses selects. For instance: Field: Telemed - This is a Y or N...
1
by: Zeng | last post by:
Hello, If I have an object that exposes many methods which don't change anything about the object (at least I'm not aware in terms of implementation of the ..net class). The methods are called...
13
by: howa | last post by:
e.g. <div class="main"> <p>ssssss</p> <p>ssssss</p> <p>ssssss</p> <p>ssssss</p>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.