Thread: program skips over code

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    program skips over code

    so i'm having this problem, where my program does not execute correctly. i'm not entirely positive what i'm doing wrong, but i wrote the program at like 5am when i was tired as a dog so that could have something to do with it. but here's the code..
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    
    /* Prototype */
    int fixgets(char *, int ,FILE *);
    void wait(long);
    
    int main(void)
    {
        FILE *fptr, *outptr;
        char infoIn[255] = {0}, infoOut[255] = {0}, fname[255] = {0}, oname[255] = {0};
        char link[] = "<a target=\"_blank\" href=\"http://www.imdb.com/find?q=", query[200] = {0};
        int i = 0, x = 0, y = 0;
        
        printf("Please enter the location of the file to be processed.\n");
        fixgets(fname, 255, stdin);
        printf("Please enter the location and name of the new file to be saved.\n");
        fixgets(oname, 255, stdin);
        
        if ((fptr = fopen(fname, "r")) == NULL || (outptr = fopen(oname, "w")) == NULL)
        {
            if (fptr == NULL)
            {
                printf("\n\nI'm sorry, but the file specified could not be opened for reading.\n");
                printf("Please close any program accessing the file and make sure the disk is readable.\n");
            }
            if (outptr == NULL)
            {
                printf("\n\nI'm sorry, but the file specified could not be opened for writing.\n");
                printf("Please ensure you have write access to the disk and try again.\n");
            }
            
            printf("\nPress any key to exit..\n");
            getch();
            if (fptr != NULL)
                fclose(fptr);
            if (outptr != NULL)
                fclose(outptr);
            return 1;
        }
        else
        {
            do
            {
                fixgets(infoIn, 255, fptr);
                if (infoIn[0] != 0)
                {              
                    if (strstr(infoIn, "<td class=\"title\">") != NULL && strstr(infoIn, "imdb.com") == NULL)
                    {
                        x = 0;
                        y = 0;
                        strcpy(infoOut, "<td class=\"title\">");
                        printf("infoOut = %s\n", infoOut);
                        system("pause");
                        strcat(infoOut, link);
                        printf("infoOut = %s\n", infoOut);
                        
                        for (i = 17; i < strlen(infoIn); i++)
                        {
                            while (infoIn[i] != '<')
                            {
                                query[y] = infoIn[i];
                                ++y;
                            }
                        }
                        printf("query: %s", query);
                        system("pause");
                                
                        }
                        for (i = 18; i < y; i++)
                        {
                            if (infoIn[i] != ',')
                                query[x] = infoIn[i];
                            else
                                y = 1000;
                            ++x;
                        }
                        printf("query: %s\n", query);
                        system("pause");
                        
                        strcat(infoOut, query);
                        
                        y = 0;
                        for (i = 0; i <= strlen(infoOut); i++)
                        {
                            if (infoOut[i] == '>')
                                ++y;
                            if (infoOut[i] == ' ' && y == 2)
                                infoOut[i] = '+';
                        }
                        
                        strcat(infoOut, "\">");
                        strcat(infoOut, query);
                        strcat(infoOut, "</a></td>");
                        fprintf(outptr, "%s\n", infoOut);
                    }
                    else
                        fprintf(outptr, "%s\n", infoIn);
                    
                    for (i = 0; i < sizeof(infoOut); i++)
                    {
                        infoOut[i] = 0;
                        infoIn[i] = 0;
                    }
            } while (!feof(fptr));
            fclose(fptr);
            fclose(outptr);
        }
        
        return 0;
    }
    
    
    /* This routine fixes fgets incorporation of line feeds and other
     * ASCII values less than 32 into an input string.  Written by H. Brown.
     */
    int fixgets(char input_array[], int length, FILE *file_name)
    {
       register int position=0;
    
       if (fgets(input_array,length,file_name) != NULL)
         {
         while(input_array[position] != 0)
    	  {              if (input_array[position] < 32) input_array[position]=0;
    	  else ++position;
    	  }
         }
       /* The length of the string is returned. */
       fflush(stdin);
       return position;
    }
    
    
    void wait ( long seconds ) {
      clock_t limit, now = clock();
      limit = now + seconds * CLOCKS_PER_SEC;
      while ( limit > now )
        now = clock();
    }
    the problem is: it doesn't output anything but: "></a></td>
    it should be outputting a lot more than that. in this conditional: if (strstr(infoIn, "<td class=\"title\">") != NULL && strstr(infoIn, "imdb.com") == NULL)
    it skips the first two printf's and goes directly to the 3rd. the problem with the 3rd conditional is that it contains no data. at all. SO...some light would be much appreciated on this dark situation i'm experiencing.. :] thanks
    Last edited by willc0de4food; 11-16-2006 at 06:41 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    system("pause");

    }
    that closing bracket in the right place?

    I moved it down in between
    }
    ---->here
    } while (!feof(fptr));
    looks like its workin better
    Last edited by sl4nted; 11-16-2006 at 12:07 PM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >system("pause");
    Slow, non-portable, and dangerous. Not a good combination.

    >for (i = 17; i < strlen(infoIn); i++)
    strlen is a potentially expensive operation. Why not just look for a null character?

    >for (i = 0; i <= strlen(infoOut); i++)
    This is an off-by-one error. It doesn't cause a problem, but it could after a little maintenance.

    >fflush(stdin);
    This is wrong, fflush is undefined for input streams.

    >while ( limit > now )
    This is antisocial, busy wait loops eat CPU time without giving anything back to the community.

    And you've been here long enough to have almost 400 posts?
    My best code is written with the delete key.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    Code:
    /* This routine fixes fgets incorporation of line feeds and other
     * ASCII values less than 32 into an input string.  Written by H. Brown.
     */
    int fixgets(char input_array[], int length, FILE *file_name)
    {
       register int position=0;
    Gotta love that old code which uses register where it really makes no difference at all, and where even average compilers are perfectly good at deciding what might be good in a register and what isn't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Gotta love that old code which uses register where it really makes no difference at all
    register is a glorified no-op, just like auto. But hey, it confuses the bejeezus out of new C programmers, so it can't be all bad, right?
    My best code is written with the delete key.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    You mean like using "const" and "volatile" in the same declaration
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You mean like using "const" and "volatile" in the same declaration
    Feh, with most of these guys you could just use volatile and confuse them.
    My best code is written with the delete key.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    Could have been worse, it could have been
    register clock_t limit, now = clock();
    for that uber-efficient do-nothing loop
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    Quote Originally Posted by Prelude
    >system("pause");
    Slow, non-portable, and dangerous. Not a good combination.

    >for (i = 17; i < strlen(infoIn); i++)
    strlen is a potentially expensive operation. Why not just look for a null character?

    >for (i = 0; i <= strlen(infoOut); i++)
    This is an off-by-one error. It doesn't cause a problem, but it could after a little maintenance.

    >fflush(stdin);
    This is wrong, fflush is undefined for input streams.

    >while ( limit > now )
    This is antisocial, busy wait loops eat CPU time without giving anything back to the community.

    And you've been here long enough to have almost 400 posts?
    the only reason i was using the system pause was so that i could see for myself where the error was in my program. the final product would never have such a function.

    thank you for the idea, i'll do that.

    mm...yes, it is. like i said, it was written at 5am so i dont know what logic i was (or wasn't) using.

    the fixgets is a function one of my old instructors wrote and i haven't modified it. theres no reason why, i just haven't.



    but thanks prelude and slanted, you've both been helpful.

    salem, sarcastic and seeminly cold as usual :]
    Registered Linux User #380033. Be counted: http://counter.li.org

  10. #10
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    yess.. :-D i finally have it working how i'd like.
    and just for reference, my purpose for the program is so that i can specify an html file to read in, have the program find the first td of each row, read in the title of a movie and then create a link to a search for that movie on imdb. the reason i want a program to do it is theres > 600 movies :]

    Code:
    #include <stdio.h>
    #include <string.h>
    
    /* Prototype */
    int fixgets(char *, int ,FILE *);
    
    int main(void)
    {
        FILE *fptr, *outptr;
        char infoIn[255] = {0}, infoOut[255] = {0}, fname[255] = {0}, oname[255] = {0};
        char link[] = "<a target=\"_blank\" href=\"http://www.imdb.com/find?q=", query[200] = {0};
        char yn = 'n';
        int i = 0, x = 0, y = 0, ch = 0;
        
        printf("Please enter the location of the file to be processed.\n");
        fixgets(fname, 255, stdin);
        
        while (yn == 'n')
        {
            printf("\nPlease enter the location and name of the new file to be saved.\n");
            fixgets(oname, 255, stdin);
            if ((outptr = fopen(oname, "r")) != NULL)
            {
                printf("\n\nWARNING: the file you're about to write to already exists.\n");
                printf("If you choose to continue, the file will be overwritten.\n");
                printf("Would you like to continue anyways? (y/n): ");
                scanf("%c", &yn);
                while ((ch = getchar()) != '\n' && ch != EOF);
                
                i = 0;
                while (oname[i] != 0)
                {
                    oname[i] = 0;
                    ++i;
                }
                fclose(outptr);
            }
            else
                yn = 'y';
        }
        if ((fptr = fopen(fname, "r")) == NULL || (outptr = fopen(oname, "w")) == NULL)
        {
            if (fptr == NULL)
            {
                printf("\n\nI'm sorry, but the file specified could not be opened for reading.\n");
                printf("Please close any program accessing the file and make sure the disk is readable.\n");
            }
            if (outptr == NULL)
            {
                printf("\n\nI'm sorry, but the file specified could not be opened for writing.\n");
                printf("Please ensure you have write access to the disk and try again.\n");
            }
            
            printf("\nPress any key to exit..\n");
            getch();
            if (fptr != NULL)
                fclose(fptr);
            if (outptr != NULL)
                fclose(outptr);
            return 1;
        }
        else
        {
            do
            {
                fixgets(infoIn, 255, fptr);
                if (infoIn[0] != 0)
                {              
                    if (strstr(infoIn, "<td class=\"title\">") != NULL && strstr(infoIn, "imdb.com") == NULL)
                    {
                        strcpy(infoOut, "<td class=\"title\">");
                        strcat(infoOut, link);
                        
                        i = 0;
                        while (infoIn[i] != '>')
                        {
                              ++i;
                        }
                        ++i;
                        
                        y = 0;
                        while (infoIn[i] != '\0' && infoIn[i] != '<' && infoIn[i] != ',')
                        {
                            query[y] = infoIn[i];
                            ++y;
                            ++i;
                        }                                        
                        
                        strcat(infoOut, query);
                        strcat(infoOut, "\">");
                        strcat(infoOut, query);
                        strcat(infoOut, "</a></td>");
                        fprintf(outptr, "%s\n", infoOut);
                        
                        for (i = 0; i < 200; i++)
                        {
                            query[i] = 0;
                            infoIn[i] = 0;
                        }
                    }
                    else
                        fprintf(outptr, "%s\n", infoIn);
                    
                    for (i = 0; i < sizeof(infoOut); i++)
                    {
                        infoOut[i] = 0;
                        infoIn[i] = 0;
                    }
                }
            } while (!feof(fptr));
            fclose(fptr);
            fclose(outptr);
        }
        
        return 0;
    }
    
    
    /* This routine fixes fgets incorporation of line feeds and other
     * ASCII values less than 32 into an input string.  Written by H. Brown.
     */
    int fixgets(char input_array[], int length, FILE *file_name)
    {
       int position=0;
    
       if (fgets(input_array,length,file_name) != NULL)
         {
         while(input_array[position] != 0)
    	  {              if (input_array[position] < 32) input_array[position]=0;
    	  else ++position;
    	  }
         }
       /* The length of the string is returned. */
       return position;
    }
    Last edited by willc0de4food; 11-16-2006 at 06:57 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. scanf skips lines of code in DOS using Dev-C++ and Windows XP
    By jenovanomusuko in forum C Programming
    Replies: 9
    Last Post: 12-21-2008, 03:10 AM
  3. Run A Program from within a cpp code
    By Hexxx in forum C++ Programming
    Replies: 6
    Last Post: 01-02-2006, 08:05 PM
  4. how do I morse code converting program
    By panfilero in forum C Programming
    Replies: 17
    Last Post: 10-29-2005, 09:16 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM