A
Abby
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);
if (IP[j - 1] == '\n') {
IP[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
while (!feof(IPaddr)) {
fscanf(IPaddr, "%s", IPTemp);
if (IPTemp == NULL){
if(i != 0){
i--;
}
}
else {
i++;
}
}
fclose(IPaddr);
return IPTemp;
}
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);
if (IP[j - 1] == '\n') {
IP[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
while (!feof(IPaddr)) {
fscanf(IPaddr, "%s", IPTemp);
if (IPTemp == NULL){
if(i != 0){
i--;
}
}
else {
i++;
}
}
fclose(IPaddr);
return IPTemp;
}