Noob help

This is a discussion on Noob help within the C Programming forums, part of the General Programming Boards category; Hey hi all! i am reading The C Programming Lenguage, doing the exercies and i need some help Exercise 1-17 ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    Noob help

    Hey hi all! i am reading The C Programming Lenguage, doing the exercies and i need some help

    Exercise 1-17. Write a program to print all input lines that are longer than 80 characters.

    Code:
    #include <stdio.h>
    
    #define MIN 2              /* print lines longer than this one */
    #define MAXLENGHT  10000     /* the main arrays where all the lines will we saved */
    #define MAXLINE  1000      /* mac line length */
    
    int getline(char s[], int lim);
    void copy_to_lines(char from[], char to[], int position, int len);
    int strlen(char s[]);
    
    int main()
    {
        char line[MAXLINE];
        char lines_saved[MAXLENGHT];
        int current_position = 0;
        int len;
    
        while(( len = getline(line, MAXLINE)) > 0)
        {
            if (len > MIN) {
                copy_to_lines(line, lines_saved, current_position, len);
                current_position += len;
            } 
        }
        printf("%s", lines_saved);
    
        return 0;
    }
    
    int getline(char s[], int lim)
    {
        int c, i;
    
        for (i = 0; i < lim && (c = getchar()) != EOF && c != '\n'; ++i)
        {
            s[i] = c;
        }
        if (c == '\n') {
            s[i] = c;
            ++i;
        } 
    
        return i;
    }
    
    void copy_to_lines(char from[], char to[], int position, int len)
    {
        int i;
    
        for(i = 0; i < len; ++i)
        {
            to[position] = from[i];
            ++i;
            ++position;
        }
    }
    I am not using any resource not present in the book at that point.
    Any help is much appreciated.
    Thanks.
    Last edited by Alejandrito; 02-09-2009 at 08:54 AM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    i need some help
    What is your question?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    Quote Originally Posted by vart View Post
    What is your question?
    The program only prints parts of a string and not all the string longer than MIN

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    MAXLENGHT
    Please try to use correct, English, spelling (so TH, not HT at the end). [No, it doesn't make your code work better - but at some point sooner or later, you will wonder why the **** MAXLENGTH is "undefined" according to the compiler].

    I don't see anything directly wrong in your code, so perhaps you can show us some input and respective output?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I see the opposite effect - your programs prints too much - becuase your lines_saved buffer is not nul-terminated
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    getline returns the number of characters read, but it also stores the '\n' at len-1. If you try to read MIN characters and there's no '\n', that means the line was too long and you can loop to get the rest of it. mucho easier than what you were doing before.
    Code:
    int main()
    {
        char line[MAXLINE];
        int len;
        while((len = getline(line, MIN)) > 0)
        {
            if (line[len-1] != '\n')
            {
                while (line[len-1] != '\n')
                {
                    printf("%s", line);
                    len = getline(line, MIN);
                }
                printf("%s", line);
            }
        }
        return 0;
    }
    getline is wrong too. you don't ever store the '\0' at the end. you have to make room for it and store it or printing the strings won't work right.
    Code:
    int getline(char s[], int lim)
    {
        int c, i;
        for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; i++)
            s[i] = c;
        if (c == '\n') s[i++] = c;
        s[i] = '\0';
        return i;
    }

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    Quote Originally Posted by matsp View Post
    Code:
    MAXLENGHT
    Please try to use correct, English, spelling (so TH, not HT at the end). [No, it doesn't make your code work better - but at some point sooner or later, you will wonder why the **** MAXLENGTH is "undefined" according to the compiler].
    Mats
    You are right, sorry...

    Quote Originally Posted by vart View Post
    I see the opposite effect - your programs prints too much - becuase your lines_saved buffer is not nul-terminated
    Thanks! This one do the trick.

    Now it works

    Code:
    #include <stdio.h>
    
    #define MIN 80             /* print lines longer than this one */
    #define MAXLENGTH  10000     /* the main arrays where all the lines will we saved */
    #define MAXLINE  1000      /* mac line length */
    
    int getline(char s[], int lim);
    void copy_to_lines(char from[], char to[], int position, int len);
    
    int main()
    {
        char line[MAXLINE];
        char lines_saved[MAXLENGHT];
        int current_position = 0;
        int len = 0;
        int i;
    
        while((len = getline(line, MAXLINE)) > 0)
        {
            if (len > MIN) {
                copy_to_lines(line, lines_saved, current_position, len);
                current_position += len;
            } 
        }
        printf("%s", lines_saved);
    
        return 0;
    }
    
    int getline(char s[], int lim)
    {
        int c, i;
    
        for (i = 0; i < lim && (c = getchar()) != EOF && c != '\n'; ++i)
        {
            s[i] = c;
        }
        if (c == '\n') {
            s[i] = c;
            ++i;
        } 
        s[i] = '\0';
    
        return i;
    }
    
    void copy_to_lines(char from[], char to[], int position, int len)
    {
        int i = 0;
    
        while((to[position] = from[i]) != '\0')
        {
            ++i;
            ++position;
        }
    }
    i will ad more question in this post, when i have problems or doubts.
    Both, many thanks!

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    in your getline
    what about exiting on i < lim condition?

    you will write to s[lim] char - which is out of bounds access
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    in your getline
    what about exiting on i < lim condition?

    you will write to s[lim] char - which is out of bounds access
    Yes, good point - although I doubt it explains why the lines are printed wrong [although non-terminated string may of course cause all sorts of weird stuff].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob Q: How to read a value of a byte in binary?
    By Hitsuin in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 03:46 PM
  2. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  3. I'm a noob and I need help.
    By nifear4 in forum C Programming
    Replies: 17
    Last Post: 10-14-2008, 02:20 PM
  4. noob needs help!!!!:(
    By tykenfitz in forum C Programming
    Replies: 1
    Last Post: 07-10-2005, 09:49 AM
  5. noob: compiling error... plz help!
    By chosii in forum C Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21