Thread: Determine file types in C?

  1. #1
    Registered User
    Join Date
    Jul 2002

    Determine file types in C?


    I need to open a file in C but I want to make sure that it is a ASCII text file before I open it.

    What is the best way to accomplish this in C? (UNIX)

    This is the only way I can think of:

    Call the shell command "file filename > tempfile" from my C code and then read tempfile for the output from the command. (is there a way to avoid the step of creating tempfile and returning stdout from the file command directly into my C prog?)

    Anyone know a better way?



  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    In C, there is no difference between "text files" and "binary files" as far as it is concerned. (Not entirly true, since it does give you text mode and binary mode for opening a file.)

    Basicly, you read a byte from the file, and check if it's a printable character. If it isn't, you're basicly looking at a binary file.
    FILE *fp = fopen( "myfile", "rb" );
    int c,x=0;
    if( fp )
        while( (c=fgetc(fp) != EOF )
            if( !isprint(c) )
                printf("This file contains unprintable characters, and as such, is likely binary.\n");
                x = 1;
    if( x )
        printf("Unusable file. Terminating.");
    If x is not zero, you have a bad file, so you terminate the program. Otherwise, you reopen it in text mode and use it.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2002


    thanks fer the reply...I figgered as much but had some hope....

    How about returning stdout from a process to a C prog?

    a la:

    system("file somefile");

    Is there a way to pipe stdout from the file program back into the C prog?



  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    >Is there a way to pipe stdout from the file program back into the C prog?
    Yes, popen()/pclose() is one way to do it.

    #include <stdio.h>
    int main(void)
    	FILE *fp;
    	char buf[BUFSIZ];
    	if ((fp = popen("ping", "r")) == NULL)
    		return (1);
    	while (fgets(buf, sizeof buf, fp))
    		printf ("OUTPUT: %s", buf);
    	return 0;
    This will work in Unix (or at least should ). For those running a Windows based compiler, you might be lucky have _popen() and _pclose() which will do the same thing.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM