Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #111813

Re: How to determine if file is binary or text?

From DFS <nospam@dfs.com>
Newsgroups comp.lang.c
Subject Re: How to determine if file is binary or text?
Date 2017-06-08 09:21 -0400
Organization A noiseless patient Spider
Message-ID <ohbips$e1a$2@dont-email.me> (permalink)
References (3 earlier) <87poefe8h1.fsf@bsb.me.uk> <oha5b6$e96$2@dont-email.me> <KU0_A.348038$op5.91911@fx39.am4> <ohahch$9su$2@dont-email.me> <6638cc40-dd84-4f1a-a60f-5860a172f4a8@googlegroups.com>

Show all headers | View raw


On 6/8/2017 5:13 AM, mark.bluemel@gmail.com wrote:
> On Thursday, 8 June 2017 04:51:09 UTC+1, DFS  wrote:
> 
>> The docs say
>>
>> "If command is not NULL, system returns the value that is returned by
>> the command interpreter. It returns the value 0 only if the command
>> interpreter returns the value 0."
>>
>> https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/system-wsystem
>>
>> So I'm not sure what's happening.
> 
> The value returned by a program is not the same as the messages output by a program...
> 
> #include <stdio.h>
> 
> int main(int argc, char** argv)
> {
>    printf("Hello World\n");
>    return 0;
> }
> 
> The value returned is not "Hello World\n". You could probably have worked
> that out from the fact that the "system" function returns an int not a char *.

If I wish hard enough will it return char data?



> If you have support for pipes, that's the appropriate way to handle command
> output. Did you actually try reading the stream created by "_popen"?

I did.  _popen returns the output of 'file', but crashes my program.

http://imgur.com/a/NfnrL


int is_textfile(char *fname)
{
	//run the GNUWin32 file command
	char psBuffer[128];
	FILE *pPipe;

	char cmd[] = {0};
	sprintf(cmd,"file %s",fname);
		
	if((pPipe = _popen(cmd,"rt")) == NULL) {exit(1);}
	while(fgets(psBuffer, 128, pPipe))
	{
	   //how to check for 'ASCII'?
	   printf(psBuffer);    //this prints the stream
	}  		
	if (feof(pPipe)) {_pclose(pPipe);}
	return 0;
}


if (is_textfile(filein)!=0)    //CRASHES on this line
{
  printf("'%s' is not a text file.\n",filein);
  return -1;
}


Also, can you write the code to look for 'ASCII' in the psBuffer data? 
I tried strstr() and couldn't get it to work.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to determine if file is binary or text? DFS <nospam@dfs.com> - 2017-06-07 14:40 -0400
  Re: How to determine if file is binary or text? GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-06-07 20:46 +0200
  Re: How to determine if file is binary or text? bartc <bc@freeuk.com> - 2017-06-07 19:58 +0100
  Re: How to determine if file is binary or text? scott@slp53.sl.home (Scott Lurndal) - 2017-06-07 19:05 +0000
  Re: How to determine if file is binary or text? supercat@casperkitty.com - 2017-06-07 12:10 -0700
  Re: How to determine if file is binary or text? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-07 16:25 -0400
  Re: How to determine if file is binary or text? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2017-06-07 21:44 +0000
    Re: How to determine if file is binary or text? DFS <nospam@dfs.com> - 2017-06-07 18:14 -0400
      Re: How to determine if file is binary or text? GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-06-08 00:23 +0200
        Re: How to determine if file is binary or text? Robert Wessel <robertwessel2@yahoo.com> - 2017-06-08 00:11 -0500
          Re: How to determine if file is binary or text? GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-06-08 15:14 +0200
            Re: How to determine if file is binary or text? Robert Wessel <robertwessel2@yahoo.com> - 2017-06-08 11:06 -0500
          Re: How to determine if file is binary or text? raltbos@xs4all.nl (Richard Bos) - 2017-06-17 18:28 +0000
      Re: How to determine if file is binary or text? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-07 23:30 +0100
        Re: How to determine if file is binary or text? DFS <nospam@dfs.com> - 2017-06-07 20:26 -0400
          Re: How to determine if file is binary or text? bartc <bc@freeuk.com> - 2017-06-08 01:30 +0100
            Re: How to determine if file is binary or text? DFS <nospam@dfs.com> - 2017-06-07 23:50 -0400
              Re: How to determine if file is binary or text? mark.bluemel@gmail.com - 2017-06-08 02:13 -0700
                Re: How to determine if file is binary or text? mark.bluemel@gmail.com - 2017-06-08 04:05 -0700
                Re: How to determine if file is binary or text? DFS <nospam@dfs.com> - 2017-06-08 09:21 -0400
                Re: How to determine if file is binary or text? Ike Naar <ike@iceland.freeshell.org> - 2017-06-08 14:19 +0000
                Re: How to determine if file is binary or text? DFS <nospam@dfs.com> - 2017-06-08 12:07 -0400
                Re: How to determine if file is binary or text? scott@slp53.sl.home (Scott Lurndal) - 2017-06-08 16:14 +0000
                Re: How to determine if file is binary or text? DFS <nospam@dfs.com> - 2017-06-08 12:37 -0400
                Re: How to determine if file is binary or text? scott@slp53.sl.home (Scott Lurndal) - 2017-06-08 16:54 +0000
                Re: How to determine if file is binary or text? DFS <nospam@dfs.com> - 2017-06-08 13:14 -0400
                An old version of what??? (Was: How to determine if file is binary or text?) gazelle@shell.xmission.com (Kenny McCormack) - 2017-06-09 17:13 +0000
              Re: How to determine if file is binary or text? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-08 11:15 +0100
          Re: How to determine if file is binary or text? scott@slp53.sl.home (Scott Lurndal) - 2017-06-08 12:49 +0000
          Re: How to determine if file is binary or text? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2017-06-08 14:01 +0000
          Re: How to determine if file is binary or text? Michael Forsythe <forsythe@example.com> - 2017-06-08 15:33 +0000
            Re: How to determine if file is binary or text? DFS <nospam@dfs.com> - 2017-06-08 18:43 -0400
              Re: How to determine if file is binary or text? jak <please@nospam.tnx> - 2017-06-09 08:46 +0200
      Re: How to determine if file is binary or text? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-07 19:23 -0400
      Re: How to determine if file is binary or text? Barry Schwarz <schwarzb@dqel.com> - 2017-06-07 16:50 -0700
      Re: How to determine if file is binary or text? "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-06-08 15:51 +0200
  Re: How to determine if file is binary or text? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-06-07 17:17 -0600
    Re: How to determine if file is binary or text? Keith Thompson <kst-u@mib.org> - 2017-06-07 19:21 -0700
      Re: How to determine if file is binary or text? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-06-07 21:37 -0600
  Re: How to determine if file is binary or text? Noob <root@127.0.0.1> - 2017-06-08 08:40 +0200
  Re: How to determine if file is binary or text? gordonb.t9rom@burditt.org (Gordon Burditt) - 2017-06-08 12:03 -0500

csiph-web