473,386 Members | 1,908 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,386 software developers and data experts.

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

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
3 5069
JosAH
11,448 Expert 8TB
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.