print specified line twice from stdin

This is a discussion on print specified line twice from stdin within the C Programming forums, part of the General Programming Boards category; Hi, I'm a complete C newbie, so i'm sorry if this sucks! Basically, I have a paragraph of text which ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    print specified line twice from stdin

    Hi, I'm a complete C newbie, so i'm sorry if this sucks!

    Basically, I have a paragraph of text which is being streamed through stdin. All I need the program to do is print the forth line twice using a variable '4p'.

    I have a VERY simple line counter program below... How can it be modified to achieve the requirement?

    Code:
    #include < stdio.h>
    
    void main()
    {
    	int c, nl = 0;
    	
    	while ( (c = getchar()) != EOF )
    	{
    	
    	if (c == '\n') nl++;
    	}
    	
    	printf("number of lines = %d\n",nl);
    	  
    }
    A point in the right direction would be most helpful. Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    First you will need to modify your program so that it stores the line being read (at least when you are on line number 4). Once you have the stored line, you can print it using printf, puts or some other "print string" function.

    --
    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.

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by nonshatter View Post
    Hi, I'm a complete C newbie, so i'm sorry if this sucks!

    Basically, I have a paragraph of text which is being streamed through stdin. All I need the program to do is print the forth line twice using a variable '4p'.

    I have a VERY simple line counter program below... How can it be modified to achieve the requirement?

    Code:
    #include < stdio.h>
    
    void main()
    {
    	int c, nl = 0;
    	
    	while ( (c = getchar()) != EOF )
    	{
    	
    	if (c == '\n') nl++;
    	}
    	
    	printf("number of lines = %d\n",nl);
    	  
    }
    A point in the right direction would be most helpful. Thanks
    main returns int so void main is non-standard, it should be int main(void).
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Code:
    #include <stdio.h>
    
    #define LINENUMBER  4   /* which line need to print */
    #define COUNT       1   /* increase counter for line */
    #define NOCOUNT     0   /* pause counter for line */
    
    /* read a standard input, prints 4th line
       and number of all lines */
    main()
    {
        int c, nl = 0, state = COUNT;
        
        while ((c = getchar()) != EOF) {
            if (state == COUNT) {
                state = NOCOUNT;
                nl++;
            }    
            if (c == '\n')
                state = COUNT;
            if (nl == LINENUMBER)
                putchar(c);    
        }
        printf("number of lines = %d\n",nl);
        return 0;
    }
    Attached Files Attached Files
    Last edited by c.user; 04-26-2009 at 06:42 AM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. Don't give out solutions to homework problems - it helps no one.
    2. Your code don't even solve the problem of printing the line twice.
    3. Is it not a bit of a Rube Goldberg solution?

    --
    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.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Quote Originally Posted by matsp
    3. Is it not a bit of a Rube Goldberg solution?
    I wrote it 5 minutes ago
    for only 4th line

    int main(int argc, char *argv[]); is not need for С89 and some compilers don't know a С99 standard, but all know C89

  8. #8
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    nonshatter, you need an array for twice printing, with one variable it will be aabbcc only

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by c.user
    int main(int argc, char *argv[]); is not need for С89 and some compilers don't know a С99 standard, but all know C89
    So? It is not like compiling with respect to C89 would cause a compiler to choke when main is explicitly declared to have a return type of int.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    laserlight, I write in C, if compiler compiles my C like C++ it is its problem
    in gcc i have no warnings, because it is know that there was C89 many years ago

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by c.user
    I write in C, if compiler compiles my C like C++ it is its problem
    This has nothing to do with C++. Even when you do not compile as C99, consider the motivation: explicitly declaring the return type as int improves readability and helps in maintenance by making it more obvious that the intention is to declare the function as returning an int, and thus rules out the possibility that the omission is due to carelessness rather than reliance on default int.

    Quote Originally Posted by c.user
    in gcc i have no warnings, because it is know that there was C89 many years ago
    Increase your warning level to -Wall (without -std=c99 of course, since that would separately activate the warning).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Ok, i have got a warning
    Code:
    [guest@station tmp]$ gcc -Wall -std=c89 test.c -o test
    If I write to C99 or C++ sometime, I use int main(int argc, char *argv[]) because they need it by their standards, but if I write in C89 its standard better (I use int i; for (i = 0 ... not for(int i = 0 ... because I need C89 and I don't use // only /* */ because I don't want mix standards)
    first step mix standards
    second step ? mix standards with compiler features ?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,781
    Quote Originally Posted by c.user
    If I write to C99 or C++ sometime, I use int main(int argc, char *argv[]) because they need it by their standards, but if I write in C89 its standard better
    Just to check: are you aware that these two programs are valid in C89? (In fact, these correspond the closest to the actual text of section 2.1.2.2 of the 1989/1990 version of the C Standard.)
    Code:
    int main(void)
    {
        return 0;
    }
    Code:
    int main(int argc, char *argv[])
    {
        return 0;
    }
    It seems to me that what you are saying is that C89 forbids functions from being declared with int return type stated explicitly, but of course that would be rubbish.

    Quote Originally Posted by c.user
    (I use int i; for (i = 0 ... not for(int i = 0 ... because I need C89 and I don't use // only /* */ because I don't want mix standards)
    I think you are confused: there is no "mixing of standards" here. The use of an explicit int return type is something permitted by C89 as well. The mixing of declarations and other code and the use of // style comments are indeed features not present in C90 but present in C99 (and C++), but those are different issues altogether. I would also recommend against their use if there is a need to support compilers that only compile with respect to C89.
    Last edited by laserlight; 04-26-2009 at 11:05 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    I used some like this:

    Code:
    void func(const char *l, int sep)
    {
        char delim[2] = { sep, '\0' };
        ...
    }
    it is C99 feature, and I get answer: program doesn't compile, I need to do:

    Code:
    void func(const char *l, int sep)
    {
        char delim[2] = { '\0' };
        
        *delim = sep; 
         ...
    }
    standard mixing
    I don't need, if I open a source (my source) I want see in which standard it has written, and in which standard not
    If I will write C89+C99 I will write int main(void) or int main(int argc, char *argv[]) and when I shall open this source, I shall see that I can use all functions from C99 (there are many new functions and new features and new comments I can use and more)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to do encryption in C
    By sankarv in forum C Programming
    Replies: 33
    Last Post: 12-28-2010, 10:01 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 05:21 PM
  5. fprintf help ,my print outs wont line up
    By plivermo in forum C Programming
    Replies: 2
    Last Post: 08-15-2001, 09:39 AM

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