Thread: Read strings line by line until Ctrl+C is pressed

  1. #16
    Registered User
    Join Date
    Nov 2009
    Posts
    46
    laserlight
    c is a char

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by r00t
    c is a char
    Then that is incorrect. getchar() returns an int, and c should likewise be an int, not a char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Nov 2009
    Posts
    46
    lol, it worked for me when I used char, and when I pressed ctrl+c, it quit from the loop,
    and then I could work with an array

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by r00t
    it worked for me when I used char
    The problem is that char can be signed or unsigned, depending on the implementation. If char is signed, and if EOF is within the range of char, then everything is fine. However, if char is unsigned, or if EOF is not within the range of char, then you could have a problem.

    Quote Originally Posted by r00t
    and when I pressed ctrl+c, it quit from the loop,
    and then I could work with an array
    hmm... so you are saying that CTRL+C does not exit the program, but instead appears to simulate EOF? What is your current program?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Nov 2009
    Posts
    46
    the current program is the code in the first post, however i didn't implement any EOF... i don't know how it works with scanf.. any ideas?
    again, i want to read strings line by line, then when I hit ctrl+c sort it and print it out.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by r00t
    again, i want to read strings line by line, then when I hit ctrl+c sort it and print it out.
    This might not have been made clear to you, but on the Windows console, CTRL+Z is used to simulate EOF. You would press enter, press CTRL+Z, then press enter again. CTRL+C might work too, but then you'll get into a discussion about signal handling and such instead of just about simulating EOF as you want to do.

    Quote Originally Posted by r00t
    however i didn't implement any EOF... i don't know how it works with scanf.. any ideas?
    Firstly, if you want to read strings with scanf(), you should not use the %s format specifier by itself. You should also account for the maximum length of the string, in this case 9, so you would use scanf("%9s",sp[i]). Note that %s will only cause the input to be read until the first whitespace character. Here is a simple example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char str[10];
        while (scanf("%9s", str) != EOF)
        {
            printf("%s\n", str);
        }
        puts("Done!");
        return 0;
    }
    That said, it may be better to compare with 1 instead of EOF since that is the expected return value of scanf for a successful read where the format string has only 1 format specifier.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Nov 2009
    Posts
    46
    hey guys,
    i've completed the assignment
    so the task was to read strings line by line until Ctrl+D(EOF signal) is pressed.
    then sort it.
    max length of line is 100
    max number of lines is 50

    if number of lines>100 then print **truncated
    if length of line>50 then sort the first 50 chars of string and then put **more than 50 errorr

    here is the code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 100
    #define N 51
    
    void sort_lines(char **line, int *err, int n);
    void swap(char **, char **);
    void swap_int(int *, int*);
    int main(void) 
    {
     char **line;
     int i = 0, j = 0, *err;
     int c, n, k;
    
     err = malloc(N*sizeof(int));
     line = malloc(N*sizeof(char*));
     line[0] = malloc(MAX*sizeof(char));
    
    	while((c=getchar()) != EOF) 
    	{		
    		if(c=='\n')
    		{	
    			if ( j>=MAX ) err[i]=1;
    			j=0;
    			i=i+1;
    			line[i] = malloc((MAX+1)*sizeof(char));
    		}	
    		else
    		{
    			if (j>=MAX) continue;
    			line[i][j] = c;
    			j=j+1;
    			
    		}
    	}
    	sort_lines(line, err, i);
    	for (k=0; k < i ; k++)
    	{
    		if (err[k]==1) printf("*** Error**length is more than 100\n");
    		else 	printf("%s\n",line[k]);
    		if (k>50) {printf("*** Error** Truncated\n");break;}
    	}
     return 0;
    }
    
    void sort_lines(char **line, int *err, int n) 
    {
     int i = 0;
     int j = 0;
     int y = n;
     for(i = 0; i < y; ++i)
      for(j = i + 1; j < y; ++j)
       if(strcmp(line[i], line[j]) > 0)
    	{
    	 swap(&line[i], &line[j]);
    	 swap_int(&err[i], &err[j]);
    	}
    }
    
    void swap(char **p, char **q) 
    {
    	 char *tmp;
    
    	 tmp = *p;
    	 *p = *q;
    	 *q = tmp;
    }
    void swap_int(int *a, int *b)
    {
    	int tmp;
    
    	tmp = *a;
    	*a = *b;
    	*b = tmp;
    }
    thanx to all!

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by r00t View Post
    hey guys,
    i've completed the assignment
    so the task was to read strings line by line until Ctrl+D(EOF signal) is pressed.
    I told you it wasn't CTRL C.


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

  9. #24
    Registered User
    Join Date
    Nov 2009
    Posts
    46
    Quote Originally Posted by quzah View Post
    I told you it wasn't CTRL C.


    Quzah.
    Again, I was working on windows using MinGW, and it was CTRL C,
    in linux it IS CTRL D

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by r00t View Post
    Again, I was working on windows using MinGW, and it was CTRL C,
    in linux it IS CTRL D
    No it isn't. Show me where ... anywhere ... MinGW says that CTRL C is EOF. The programs run from a standard windows command prompt, MinGW doesn't magically override the windows shell.


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

  11. #26
    Registered User
    Join Date
    Nov 2009
    Posts
    46
    Quote Originally Posted by quzah View Post
    No it isn't. Show me where ... anywhere ... MinGW says that CTRL C is EOF. The programs run from a standard windows command prompt, MinGW doesn't magically override the windows shell.


    Quzah.
    thnx anyways

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, 06:20 AM
  2. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM
  3. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM
  4. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06: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