trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some mistakes, i'm using cygwin on a windows pc and it seems that some functions cause stack overflows so am using fgets instead of fgetc (don't know y it solved the problem but that part is working).
my problem now is that i am reading strings a few characters at a time (was planning to do a string compare but realised that the lines i want to compare in the file are not unique). the plan now (although am probably not doing it the right way) is to look for line feeds/carriage returns and count the lines then replace the lines that i want (ip addresses and remote access ports). as i have a lot of WAPs to program the final version will just increment IPs and HTTP/SHTTP ports and save to a new file. as you will see i haven't started the last stage yet but am really enjoying the chanllenge.
the line that is causing me problems in the code is: &c=compare;
my idea is to copy the contents of the pointer array to a string so that i can search the string for /r or /n characters (as u will see in the for loop below it).
i'm sure there's loads of redundant code at the moment it just creates copies of the original file but if someone could explain to me how i can convert the contents of the pointer into a string then i would be very greatfull. i like coding so if u can see stupid errors please explain what i have done wrong as i say a lot of it is in place for the functionallity i want to add once i have got past this hurdle.
much love and thanks in advance
doug
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
int count; /* number of waps */
int current;/* current file */
int ip1; /* first part of ip address */
int ip2; /* second part of ip address */
int ip3; /* third part of ip address */
int ip4; /* forth part of ip address */
int IP; /* current ip address */
char file[15];/* initial file */
int port; /* remote access http port */
int sport; /* remote access secure http port */
int i; /* integer for scanning data read */
int line; /* line number */
char compare[34]; /* comparing string to find cr/lf */
printf("Enter number of WAPs \n");
scanf("%d",&cou nt);
printf("Enter first IP address, separated with spaces not decimal points \n");
scanf("%d %d %d %d",&ip1,&ip2,& ip3,&ip4);
printf("Enter file name, do not put .cfg \n");
scanf("%s",file );
IP = ip4;
line = 1;
for(current = 1; current!=count + 1; current++) { /* loop untill current = count */
port = 9000 + IP;
sport = 8000 + IP;
FILE *sourceFile;
FILE *destinationFil e;
char formatr[] = "%s.cfg";
char readFile[sizeof formatr+100];
sprintf(readFil e,formatr,file) ;
sourceFile = fopen(readFile, "r");
printf("file opened\n");
char formatw[] = "%s%d.cfg";
char writeFile[sizeof formatw+100];
sprintf(writeFi le,formatw,file ,current);
destinationFile = fopen(writeFile ,"w");
printf("file created \n");
if(sourceFile== NULL) {
printf("Error: can't access file.c.\n");
return 1;
}
else if(destinationF ile==NULL) {
printf("Error: can't create file for writing.\n");
return 1;
}
else {
printf("File opened successfully. Contents:\n\n") ;
char c[34];
while(fgets(c, 34, sourceFile)!=NU LL) {
/* keep looping until NULL pointer... */
&c=compare;
printf("String: %s \n", c);
fputs(c, destinationFile );
/* print the file one line at a time */
for(i=0; i!=34; i++){
//check for new line character in string
printf("checkin g string %c\n",compare[i]);
if (compare[i]="\r"){
printf("found it!\n");
fputc(line, destinationFile );
line++;
//if new line character found enter line number at start of line
}
}
}
printf("text written\n");
fclose(sourceFi le);
fclose(destinat ionFile);
}
IP++;
}
return 0;
}
my problem now is that i am reading strings a few characters at a time (was planning to do a string compare but realised that the lines i want to compare in the file are not unique). the plan now (although am probably not doing it the right way) is to look for line feeds/carriage returns and count the lines then replace the lines that i want (ip addresses and remote access ports). as i have a lot of WAPs to program the final version will just increment IPs and HTTP/SHTTP ports and save to a new file. as you will see i haven't started the last stage yet but am really enjoying the chanllenge.
the line that is causing me problems in the code is: &c=compare;
my idea is to copy the contents of the pointer array to a string so that i can search the string for /r or /n characters (as u will see in the for loop below it).
i'm sure there's loads of redundant code at the moment it just creates copies of the original file but if someone could explain to me how i can convert the contents of the pointer into a string then i would be very greatfull. i like coding so if u can see stupid errors please explain what i have done wrong as i say a lot of it is in place for the functionallity i want to add once i have got past this hurdle.
much love and thanks in advance
doug
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) {
int count; /* number of waps */
int current;/* current file */
int ip1; /* first part of ip address */
int ip2; /* second part of ip address */
int ip3; /* third part of ip address */
int ip4; /* forth part of ip address */
int IP; /* current ip address */
char file[15];/* initial file */
int port; /* remote access http port */
int sport; /* remote access secure http port */
int i; /* integer for scanning data read */
int line; /* line number */
char compare[34]; /* comparing string to find cr/lf */
printf("Enter number of WAPs \n");
scanf("%d",&cou nt);
printf("Enter first IP address, separated with spaces not decimal points \n");
scanf("%d %d %d %d",&ip1,&ip2,& ip3,&ip4);
printf("Enter file name, do not put .cfg \n");
scanf("%s",file );
IP = ip4;
line = 1;
for(current = 1; current!=count + 1; current++) { /* loop untill current = count */
port = 9000 + IP;
sport = 8000 + IP;
FILE *sourceFile;
FILE *destinationFil e;
char formatr[] = "%s.cfg";
char readFile[sizeof formatr+100];
sprintf(readFil e,formatr,file) ;
sourceFile = fopen(readFile, "r");
printf("file opened\n");
char formatw[] = "%s%d.cfg";
char writeFile[sizeof formatw+100];
sprintf(writeFi le,formatw,file ,current);
destinationFile = fopen(writeFile ,"w");
printf("file created \n");
if(sourceFile== NULL) {
printf("Error: can't access file.c.\n");
return 1;
}
else if(destinationF ile==NULL) {
printf("Error: can't create file for writing.\n");
return 1;
}
else {
printf("File opened successfully. Contents:\n\n") ;
char c[34];
while(fgets(c, 34, sourceFile)!=NU LL) {
/* keep looping until NULL pointer... */
&c=compare;
printf("String: %s \n", c);
fputs(c, destinationFile );
/* print the file one line at a time */
for(i=0; i!=34; i++){
//check for new line character in string
printf("checkin g string %c\n",compare[i]);
if (compare[i]="\r"){
printf("found it!\n");
fputc(line, destinationFile );
line++;
//if new line character found enter line number at start of line
}
}
}
printf("text written\n");
fclose(sourceFi le);
fclose(destinat ionFile);
}
IP++;
}
return 0;
}
Comment