By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,315 Members | 1,672 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,315 IT Pros & Developers. It's quick & easy.

Any ideas on how to skip ahead 2 lines in the file your reading from?

P: 11
right the program is nearly complete just a couple of little tweaks and i should have it. The purpose of this program is to write config files for colubrius wireless access points but it's falling over in this section of code:

while(line!=32){
fgets(c, 1, sourceFile);
strncpy (compare,c,1);
if (compare[0]=='\n'){
line++;
}
}

this section is supposed to send the pointer (sourceFile) 2 lines forward but instead i'm getting stuck in a never ending loop.
i was getting this before when i was trying to use fgetc so gave up and used fgets i tried changing the string to length 1 as shown in the above snipet
i have printed the contents of compare[0] and it's not changing. been messing about with this for about an hour and still can't work out my mistake
as i say all i want to do is move the pointer 2 lines ahead and i'm sure there must be a much more simple way to do it


#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",&count);
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 *destinationFile;
FILE *dumpFile;

char formatr[] = "%s.cfg";
char readFile[sizeof formatr+100];
sprintf(readFile,formatr,file);
sourceFile = fopen(readFile,"r");

printf("file opened\n");

char formatw[] = "%s%d.cfg";
char writeFile[sizeof formatw+100];
sprintf(writeFile,formatw,file,current);
destinationFile = fopen(writeFile,"w");

printf("file created \n");

dumpFile = fopen("dump.txt","w");

printf("dump file created");

if(sourceFile==NULL) {
printf("Error: can't access file.c.\n");
return 1;
}
else if(destinationFile==NULL) {
printf("Error: can't create file for writing.\n");
return 1;
}
else {
char c[34];

while(fgets(c, 34, sourceFile)!=NULL) {
/* keep looping until NULL pointer... */
strncpy (compare,c,34);
//printf("String: %s \n", c);
fputs(c, destinationFile);
//printf("compare = %s",compare);
/* print the file one line at a time */
for(i=0; i!=34; i++){
//printf("checking character %c\n",compare[i]);
if (compare[i]=='\n'){
//printf("found it!\n");
//fprintf(destinationFile, "%d",line);
line++;
if (line==30){ /*if line 30 then add the remote access ports */
fprintf(destinationFile," webport-insecure = %d\n webport-secure = %d\n",port,sport);
while(line!=32){
fgets(c, 34, sourceFile);
strncpy (compare,c,34);
for(i=0; i!=34; i++){
if (compare[i]=='\n'){
line++;
}
}
}
}
}
}
}

printf("text grabbed\n");

printf("text written\n");

fclose(sourceFile);

fclose(destinationFile);
}
IP++;
}
return 0;
}
Nov 28 '08 #1
Share this Question
Share on Google+
3 Replies


Expert 10K+
P: 11,448
Something like this?

Expand|Select|Wrap|Line Numbers
  1. int skip(FILE* fp) {
  2.    int c;
  3.    while ((c= fgetc(fp)) != '\n')
  4.       if (c == -1) /* anticipate for an EOF condition
  5.          return 0;
  6.    return 1;
  7. }
kind regards,

Jos
Nov 28 '08 #2

P: 63
The problem you have is that you tend to use a != b in loops instead of a < b. If compare contains more than 2 newlines, then line becomes 33, and you loop forever because line != 32 is not going to be true until integer overflow happens and then it comes back to 32, which is a very long time. Instead of having while line != 32, just have while line < 32.

good luck
Nov 28 '08 #3

P: 11
thanx everyone for all your help
the program is now working here is it is, it takes a config file for a preconfigured Colubrius Wireless Access Point and then creates up to 254 config files (limit set by ip address size). the config files set the access points to sequential IP addresses and also sequential remote access ports. You still have to then put them on the WAPs but it speeds things up if you don't own a multi access controller. If it's of any use to anyone please let me know would love feedback if anyone has any additions to it please let me know.

#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",&count);
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;

for(current = 1; current!=count + 1; current++) { /* loop untill current = count */

line = 1;
port = 9000 + IP;
sport = 8000 + IP;

FILE *sourceFile;
FILE *destinationFile;
//FILE *dumpFile;

char formatr[] = "%s.cfg";
char readFile[sizeof formatr+100];
sprintf(readFile,formatr,file);
sourceFile = fopen(readFile,"r");

//printf("file opened\n");

char formatw[] = "%s%d.cfg";
char writeFile[sizeof formatw+100];
sprintf(writeFile,formatw,file,current);
destinationFile = fopen(writeFile,"w");

//printf("file created \n");

//dumpFile = fopen("dump.txt","w");

//printf("dump file created");

if(sourceFile==NULL) {
printf("Error: can't access file.c.\n");
return 1;
}
else if(destinationFile==NULL) {
printf("Error: can't create file for writing.\n");
return 1;
}
else {
char c[34];

while(fgets(c, 34, sourceFile)!=NULL) {
/* keep looping until NULL pointer... */
strncpy (compare,c,34);
//printf("String: %s \n", c);
fputs(c, destinationFile);
//printf("compare = %s",compare);
/* print the file one line at a time */
for(i=0; i!=34; i++){
//printf("checking character %c\n",compare[i]);
if (compare[i]=='\n'){
//printf("found it!\n");
//fprintf(destinationFile, "%d",line);
line++;
if (line==30){ /*if line 30 then add the remote access ports */
//printf("changing ports");
fprintf(destinationFile," webport-insecure = %d\n webport-secure = %d\n",port,sport);
int x;
while ((x= fgetc(sourceFile)) != '\n')
if (x == -1)
return 0;
while ((x= fgetc(sourceFile)) != '\n')
if (x == -1)
return 0;
}
else if (line==361){ /*if line 363 then change ip address */
fprintf(destinationFile," address = %d.%d.%d.%d\n",ip1,ip2,ip3,IP);
int x;
while ((x= fgetc(sourceFile)) != '\n')
if (x == -1)
return 0;
}
}
}
}

//printf("text grabbed\n");

//printf("text written\n");

fclose(sourceFile);

fclose(destinationFile);
}
IP++;
}
printf("DONE!!! :D");
return 0;
}
Nov 28 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.