LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-13-2007, 02:43 PM   #1
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Rep: Reputation: 30
What is wrong with this C code?


Hi,

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;

}
 
Old 08-13-2007, 02:50 PM   #2
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
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?
 
Old 08-13-2007, 03:31 PM   #3
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
Quote:
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.
 
Old 08-14-2007, 01:43 AM   #4
reverse
Member
 
Registered: Apr 2007
Distribution: Gentoo
Posts: 337

Rep: Reputation: 30
Code:
printf("%s" is found, buff);
Chnage it to: printf("%s is found", buff);

--

I think you'd be better off with a shell script. Pick the best tool for the job.

Last edited by reverse; 08-14-2007 at 01:55 AM.
 
Old 08-14-2007, 06:31 AM   #5
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by reverse View Post
Code:
printf("%s" is found, buff);
Chnage it to: printf("%s is found", buff);

--

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;

}
Thanks

Last edited by daYz; 08-14-2007 at 06:33 AM.
 
Old 08-14-2007, 08:51 PM   #6
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
relpace with "; ;" should do:

Code:
// 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
   }
}
 
Old 08-15-2007, 09:22 AM   #7
daYz
Member
 
Registered: Nov 2003
Distribution: Ubuntu
Posts: 164

Original Poster
Rep: Reputation: 30
Thanks SciYro
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What wrong in this code kits Programming 10 01-10-2007 03:55 AM
anything wrong with this code? bytez Linux - Software 1 09-19-2006 04:52 AM
What is wrong with the following C code? indian Programming 17 12-04-2005 09:01 AM
What is wrong in this code hhegab Programming 12 08-13-2005 11:23 AM
what ' s wrong in code ? phoenix_fei Programming 4 12-17-2004 12:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration