473,473 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

11 New Member
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
3 5073
JosAH
11,448 Recognized Expert MVP
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
vmpstr
63 New Member
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
blunt
11 New Member
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

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

Similar topics

3
by: Max | last post by:
Yea this is probably a n00b question, but I haven't programmed C++ in at least 2 years and have never programmed for unix, sorry :) Anyway, I have a project in which a program is required to read...
3
by: puzzlecracker | last post by:
I want to read lines and skip blank lines: would this work considering the lines can contain tabs, spaces, etc.? file.in: ------ line1 line2
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
3
by: David Kenneally | last post by:
Hello- I'm having a problem doing a file upload from within the edit function of a datalist. I have a simple file upload that works on a standard ..net page: <td width="53"><input...
11
by: Michael McGarry | last post by:
Hi, I am reading strings from a text file using fscanf("%s", currToken); This returns strings delimited by whitespace. How can I tell if the string was followed by a carriage return in the...
2
by: fool | last post by:
Dear group, I am a beginner in php and I was little bit experience in C language. I want to read a file's content and delete the first line of the file and display those lines which has got...
25
by: 7stud | last post by:
I can't break out of the for loop in this example: ------ import sys lst = for line in sys.stdin: lst.append(line) break
9
by: antar2 | last post by:
I am a starter in python and would like to write a program that reads lines starting with a line that contains a certain word. For example the program starts reading the program when a line is...
4
by: pbj2009 | last post by:
Hello all: I'm pretty stumped on this one. I'm not looking for Code, I'm just trying to figure out the best way to start this since I am new to reading and writing from files. I can't figure out...
0
Mas Juliza Alias
by: Mas Juliza Alias | last post by:
how to skip unused lines in the imported text file? as in the text file i attached, i want to read the data started from line 24. how can i go for that?
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,...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.