Thread: My last incorrect code needs some help!!!

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    My last incorrect code needs some help!!!

    And this is the last incorrect code

    Question:

    Write a program that:
    - Opens up each filename in argv[] for reading, starting from the second arg
    ument
    - - For each file, if the open fails, print, to stderr, the name of the file
    that failed and an error message
    - - - For each file that succeeds,
    - - - Print out each line in the file to stdout, if that line contains the s
    tring in argument 1
    - - - At the front of the line, also print the name of the file it came from
    , and the line number, separated by colons
    - - - Close it
    - Uses function prototypes:
    - - int partial_match( char*, char* );
    - - int find_string( char*, char* );

    Example:

    file.txt:
    hello
    world
    this
    is a test

    file2.txt:
    goodbye
    world
    this
    is a test
    so is this
    and so on
    we blissfully go

    ./prog is none.txt file.txt file1.txt
    Error: could not open none.txt
    file.txt:3:this
    file.txt:4:is a test
    file2.txt:3:this
    file2.txt:4:is a test
    file2.txt:5:so is this
    file2.txt:7:we blissfully

    - Note that the error was printed to stderr rather than stdout

    - Note that this essentially only printed lines with "is" in them, from seve
    ral files
    Here's the pseudocode:
    main:
    for as long as we have arguments
    open a file with filename of the current argument
    if it opens successfully
    for each line in the file
    if it contains the word we're looking for
    print the line
    close the file
    otherwise
    print an error

    contains:
    for as long as we have non-null characters in a string
    if the string starting from the current location partially matches the w
    ord
    true
    by the end, if we never found it, false

    partial-match:
    for as long as we have characters in the word we're searching for
    if the string's current character doesn't match the corresponding word's
    current character
    false
    by the end, if we never found a discrepancy, true

    Here's my incorrect code:

    Code:
    #include <stdio.h>
    int partial_match (char* string1, char* string2);
    void find_string (char* find_this, char* within_this);
    int main (int argc, char** argv)
    {
       File* funny;
       int i = 2;
       char buffer [1024];
       for (i=2; argv [i] != '\0'; i = i + 1)  
      {  
        funny = fopen (argv [i], "r");
        if (funny != NULL)
          {
             while (fgets (buffer, 1024, funny)!= NULL)
               { 
                 find_string (argv [1], argv [i]);
               }
                  fclose (funny);
               }
                 fprintf (stdout, "no argument!\n");
               }
    void  find_string (char* find_this, char* within_this)
    {
    int j;
    for (j = 0; within_this[j] != '\0'; j = j + 1)
       {
    if (find_this[j] == &within_this[j] )
         {
             return 1;
         }
             return 0;
       }
    }
    
    int partial_match (char* string1, char* string2)
    {
      in m;
      for (m = 0; string1[m] != '\0'; m = m+1)
           {
              if (string1 [m] != string2 [m])
                {
                    return 0;
                 }
                     return 1;
            }
    }
    That's it! Thanks for your help!!!
    Last edited by Salem; 11-13-2006 at 06:07 AM. Reason: Remove more font abuse

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You know, we're not here to do everything for you. Why is your code incorrect? At which point are you having problems? Instead of just pasting in all of your homework problems as they were provided to you, provide something useful. What part doesn't work? I suspect this is a "here's some code that doesnt' work, it is you the student's job to fix it", and you're trying to take the lazy way out.

    Why doesn't it work? What part? What part works? Learn to debug. You may not always have a debugger (us) handy.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Well maybe reading the forum guidelines may be useful before posting!

    http://cboard.cprogramming.com/annou...t.php?f=4&a=39

    And also, I would post some compiler error/warning messages or something like that instead of the whole code and question itself (as quzah said).

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're missing a 't' here:
    Code:
    in m;
    Your case is wrong here:
    Code:
    File* funny;
    I don't think this does what you want:
    Code:
    if (find_this[j] == &within_this[j] )
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    U need a proper alignment in your code. It is very difficult read a code, otherwise . Indent your code first, which make a bit readable

    ssharish2005

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    It is not badly aligned, it just needs a bit of whitespace.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It is badly aligned (or indented). This program "just needs a bit of whitespace":
    Code:
    int main(void) {
     int x, y, z, a, b, c;
     double h, j, k, l, z;
     if(x && y && z || h+1) {
      printf("%i%i%i%i%i\n", x, x, x, x, x);
     }
     //...
    }
    Look at this function, for instance. It's indentation isn't that good.
    Code:
    int main (int argc, char** argv)
    {
       File* funny;
       int i = 2;
       char buffer [1024];
       for (i=2; argv [i] != '\0'; i = i + 1)  
      {  
        funny = fopen (argv [i], "r");
        if (funny != NULL)
          {
             while (fgets (buffer, 1024, funny)!= NULL)
               { 
                 find_string (argv [1], argv [i]);
               }
                  fclose (funny);
               }
                 fprintf (stdout, "no argument!\n");
               }
    Last edited by dwks; 11-15-2006 at 01:58 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Yes, but that indentation doesn't really make a difference in the readibility of this code. It's pointless to argue about, anyway.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    manutd I disagree, having well indented code can help improve readability and makes code easier to debug
    Double Helix STL

  10. #10
    Java and C newbie Xero's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    21
    And to add, it will greatly boost development time when working in groups.
    (Since it has good readability)
    Code:
    Noob - a word used to describe someone like me. (noun)

  11. #11
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Yes, I am saying that indentation is important, but in this code the indentation is not that bad.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM