Thread: how to define end of the input stream and of the file in c

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    how to define end of the input stream and of the file in c

    Hi there is there a way to determine end of the standart input stream and end of a file? I tried to do it like EOF but it doesn't work moreover, as an output it produces weird things.
    Here is my code:
    Code:
    #include <stdio.h>
    int strip(char *arr)
    {
            int i=0;
            while (1){
                    char c =getchar();
                    if (c!=' '&& c!= '\t')
                    {
                            arr[i]=c;
                            i+=1;
                            printf ("%c\n", c);
                    }
                    else if (c == EOF){
                    printf ("4\n");
                            break;
                    }
            }
     while(i>0)
            {
                    printf ("%c", arr[i]);
                    i-=1;
            }
    return 0;
    }
    int main()
    {
            char arr[100];
            printf ("Enter some string: \n");
            strip(arr);
    }
    what I wanted is to print standart input without spaces utill it gets to the end of the string. and if I am runing it like ./myprog <text.txt
    where text.txt contains Hello there .. the output is:


    ÿ
    ÿ
    ÿ
    ÿ
    ÿ
    ÿ
    ÿ
    ÿ
    ÿ
    Segmentation fault

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    getchar() returns an int.

    That is, change c from a char variable, to an int. Then it works.
    Last edited by Adak; 10-16-2012 at 03:52 PM.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    getchar() returns an int with good (very good) reason. You must define c as int too.

    Other than that, your ifs do not do what you hoped they'd do.
    If c is not a space and c is not a tab includes c being EOF; the else part of the if will never run when c is EOF.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Don't know if you're acquainted yet with ctype.h, but it has a function that could be useful here:

    Code:
    #include <ctype.h>
    
    
    
    if (isalpha(c)) {
        etc.
    
    
    }else if(etc.) {
       //etc.
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Grabbing input text file using a stream
    By edishuman in forum C++ Programming
    Replies: 1
    Last Post: 02-12-2012, 03:04 AM
  2. File Input Stream constructor
    By yes2 in forum C++ Programming
    Replies: 0
    Last Post: 12-15-2010, 10:59 PM
  3. Using input stream to get string from file.
    By CPPN0OB in forum C++ Programming
    Replies: 7
    Last Post: 03-27-2010, 03:48 PM
  4. Getting an input stream to stop creating a file
    By Stevek in forum C++ Programming
    Replies: 3
    Last Post: 03-21-2003, 04:45 PM
  5. Input file stream problems
    By Shadow12345 in forum C++ Programming
    Replies: 10
    Last Post: 10-06-2002, 06:33 PM