ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Can someone tell me what is wrong with the following code please? I am trying to write a program that downloads the newest version of a program that is available on a server. The code searches trough apache.txt wich contains a directory listing of a server. When it finds a match it should print out buff, but for some reason this does not work as intended. If a match is found the program continues to look for a higher version in the next for loop.
The "i != 200" will need to be replaced with something like "! fseek(fptr, i, SEEK_SET) == 0" so that the program will end when the end of the file has been reached but I have not had the oppurtunity yet to take a look at that.
Thanks for your help
Code:
#include <stdio.h>
#include <string.h>
char httpd[] = "httpd-2.2.3.tar.bz2";
char buff[20];
int k = 4;
int i;
int main()
{
FILE *fptr;
fptr = fopen("apache.txt", "r");
for ( i=0; i != 200; k++){
do {
fseek(fptr, i, SEEK_SET);
i++;
fgets(buff,20,fptr);
} while ( ! strcmp(httpd, buff) == 0 );
printf("%s" is found, buff);
i = ftell(fptr);
char num[3];
sprintf(num,"%d",k);
httpd[10] = num[0];
}
fclose(fptr);
return 0;
}
Umm, why are you using "!= 200", its most probably that when 'i' is 200, it will not enter the for loop, but continue to increase in the while loop, so the for loop will never exit. Might want to have a exit clause in the for loop, or replace "!=" with a greater or less then comparison.
Speaking for which, i dont see why you even have 2 nested loops just to find a single line in a single file. Why not use a while loop that ends when end of file is reached, with a if statement to check if you have the correct line, and break out of the loop then?
Umm, why are you using "!= 200", its most probably that when 'i' is 200, it will not enter the for loop, but continue to increase in the while loop, so the for loop will never exit. Might want to have a exit clause in the for loop, or replace "!=" with a greater or less then comparison.
Thanks for your reply. I have changed "!= 200" to "<= 200", but it does not get my code working yet, unfortunately.
Quote:
Speaking for which, i dont see why you even have 2 nested loops just to find a single line in a single file. Why not use a while loop that ends when end of file is reached, with a if statement to check if you have the correct line, and break out of the loop then?
I did not think of this yet. I am going to try this out. That should also work indeed.
I think you'd be better off with a shell script. Pick the best tool for the job.
Thanks reverse.
I have considered writing the program partially in Bash, but I decided to write it fully in C so I could learn from it because I am just starting out with the language.
The following code is a working replacement of the code I provided earlier, but can someone tell me with what statement I can replace
Code:
i < 500
, so that the for loop ends when a end of file is reached, and not when i will be equal or greater than 500..
Code:
#include <stdio.h>
#include <string.h>
char httpd[] = "httpd-2.2.3.tar.bz2";
char buff[20];
int i;
int k = 3;
int main()
{
FILE *fptr;
fptr = fopen("apache.txt", "r");
for ( i=0; i < 500; i++){
fseek(fptr, i, SEEK_SET);
fgets(buff,20,fptr);
if ( strcmp(httpd, buff) == 0 ){
printf("%s is found\n\n", buff);
k++;
i = ftell(fptr);
i--;
char num[3];
sprintf(num,"%d",k);
httpd[10] = num[0];
}
}
fclose(fptr);
return 0;
}
// forgive the non-tabed indents, cant get firefox to allow me to press "tab" and work
for (unsigned int i = 0; ; i++) {
fseek(fptr, i, SEEK_SET);
if (fgets(buff,20,fptr) == -1)
break; // read error, EOF probably
if (strcmp(httpd, buff) == 0) {
printf("%s is found at %i\n\n", buff, i);
break; // stop searching upon first find
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.