Read strings line by line until Ctrl+C is pressed

This is a discussion on Read strings line by line until Ctrl+C is pressed within the C Programming forums, part of the General Programming Boards category; Hello folks, Here is what i have Code: #include <stdio.h> int main(void) { char ca[10][10],*sp[10]; int i=0; sp[0]=ca[0]; while((scanf("%s",sp[i]))) { ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    46

    Question Read strings line by line until Ctrl+C is pressed

    Hello folks,
    Here is what i have
    Code:
    #include <stdio.h>
    
    int main(void)
    {    
    	char ca[10][10],*sp[10];
    	int i=0;
    
    	sp[0]=ca[0];
    	while((scanf("%s",sp[i])))
    	{
    		i++;
    		sp[i]=ca[i];
    	}
    	
    	for (i=0;i<10 ;i++ ) printf("%s\n",sp[i]);
    	return 0;
    }
    The above code properly reads every string entered. It is correct.
    However, this is an infinite loop and I want it to break from it when the user presses Ctrl+C or Ctrl+Z depending on the shell. Break from the while loop, not exit from a program.
    I know there is getchar().... but it doesnt suite here.

    Any help is appreciated!

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    I think your program is working like as you want can you explain better

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You need to set a signal handler to catch the CTRL+C, and then process accordingly.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's not CTRL+C, it's CTRL+D.


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

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    46
    im using mingw in windows so it is ctrl+c... if i were using unix cmd it would be ctrl+d..

    RockyMarrone, i want to get input of strings(line by line) until the user hits ctrl+c, and then sort it like unix sort
    example:
    >program.exe
    >aaaa
    >bbbb
    >dddd
    >cccc
    >eeee
    >^C
    aaaa
    bbbb
    cccc
    dddd
    eeee

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by r00t View Post
    im using mingw in windows so it is ctrl+c...
    No it isn't. It's either CTRL Z or CTRL D.


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

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by r00t View Post
    im using mingw in windows so it is ctrl+c... if i were using unix cmd it would be ctrl+d..

    RockyMarrone, i want to get input of strings(line by line) until the user hits ctrl+c, and then sort it like unix sort
    example:
    >program.exe
    >aaaa
    >bbbb
    >dddd
    >cccc
    >eeee
    >^C
    aaaa
    bbbb
    cccc
    dddd
    eeee

    hmm dude then if you are linux then you have to catch the singals

    i can give you example like

    check this out

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <signal.h>
    
    
    void exit_program(int sig) {
      printf("Wake up call ... !!! - Catched signal: %d ... !!\n", sig);
      (void) signal(SIGINT, SIG_DFL);
    }
    
    int main(void) {
      (void) signal(SIGINT, exit_program);
    
      while(1)
        printf("I wanna catch Ctrl + C\n"), sleep(1);
    
      return 0;
    }

  8. #8
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by quzah View Post
    No it isn't. It's either CTRL Z or CTRL D.


    Quzah.

    Mr. Quzah, I dont know about windows but in linux

    ctrl + c = for quiting or exiting the running application / utility
    ctrl + d = logs out
    ctrl + z = run into the background

    if he is working on linux may be he wants ctrl + c

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,705
    Because you don't know apparently:

    Sending EOF will cause scanf to fail eventually, breaking the loop with its return value, and sending EOF is exactly what CTRL D or CTRL Z does on either linux or windows, respectively.

    CTRL C kills the application in my experience and you only do that when there is nothing else to do.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by whiteflags View Post
    Because you don't know apparently:

    Sending EOF will cause scanf to fail eventually, breaking the loop with its return value, and sending EOF is exactly what CTRL D or CTRL Z does on either linux or windows, respectively.

    CTRL C kills the application in my experience and you only do that when there is nothing else to do.

    Yah you are absolutely correct with ctrl + C in linux but dude what r00t wants to do he want to perform sorting at ctrl + c signal catching then he can just do sorting in the handler which he wrote

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,705
    And I'm saying that's a stupid decision. Do you really want to argue when I know how scanf actually works and the result will be the same in a less cumbersome way?

  12. #12
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by whiteflags View Post
    And I'm saying that's a stupid decision. Do you really want to argue when I know how scanf actually works and the result will be the same in a less cumbersome way?
    I am not arguing on that point but i am saying with ctrl + c sorting can be done yah its not a good practice to do that but may be this is home work

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by RockyMarrone View Post
    Yah you are absolutely correct with ctrl + C in linux but dude what r00t wants to do he want to perform sorting at ctrl + c signal catching then he can just do sorting in the handler which he wrote
    I doubt it. I think he's confused as to what he really wants, just like everyone else that shows up and says they need EOF as "ctrl (d|z) and ctrl c". More than likely he thinks it's "ctrl c", because when he tried that, his program "got EOF" (died), and so he wants it to break the loop like it would on the other OS...


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

  14. #14
    Registered User
    Join Date
    Nov 2009
    Posts
    46
    Thanx all for replies.
    First of all, this is an assignment so I have to implement it as professor said.

    I think there is no need to catch any signals.
    Code:
    while ((c = getchar()) != EOF){
    	printf("%c", c);
    }
    }
    The code works exactly as i told. It takes input until Ctrl+C(or D whatever) is hit. The main thing is that i need to send EOF.

    As an example you can run "sort" in windows cmd as well as in linux.

    to Quzah,
    I am not confused, i got EOF in the above code with hitting Ctrl+C and it did not die, I proccessed an array of chars after that. And again I'm using windows right now. And Ctrl+C or D or Z... it doesn't matter for me.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Quote Originally Posted by r00t
    And Ctrl+C or D or Z... it doesn't matter for me.
    It should matter since "main thing is that (you) need to send EOF", so not sending EOF when you are testing how your program would behave when EOF is sent is clearly wrong.

    By the way, in your most recent example, is c an int or a char?
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OPen a file and read it from the last line
    By c_geek in forum C Programming
    Replies: 14
    Last Post: 01-26-2008, 05:20 AM
  2. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 05:04 AM
  3. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 03:53 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. How do I read file line by line?
    By Blurr in forum C Programming
    Replies: 1
    Last Post: 09-22-2001, 12:32 AM

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