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


Groups > comp.lang.c > #140188

Re: Function to trim trailing spaces

From DFS <nospam@dfs.com>
Subject Re: Function to trim trailing spaces
Newsgroups comp.lang.c
References <p_82E.54640$G14.18982@fx29.iad> <lnef9222nu.fsf@kst-u.example.com> <Pib2E.185816$JW5.82323@fx46.iad> <ln1s521h8v.fsf@kst-u.example.com>
Message-ID <pTo2E.115832$Fj3.56469@fx06.iad> (permalink)
Organization blocknews - www.blocknews.net
Date 2019-01-24 15:06 -0500

Show all headers | View raw


On 1/24/2019 5:29 AM, Keith Thompson wrote:
> DFS <nospam@dfs.com> writes:
>> On 1/23/2019 9:46 PM, Keith Thompson wrote:
>>> DFS <nospam@dfs.com> writes:
>>>> Needed to right-trim a string (remove trailing spaces), and came up with
>>>> this:
>>>>
>>>> void rtrim(char * s) {s[strlen(s)-1]='\0';}	
>>>>
>>>> test_rtrim.c
>>>> ------------------------------------------------
>>>> #include <stdio.h>
>>>>
>>>> void rtrim(char * s) {s[strlen(s)-1]='\0';}	
>>>>
>>>> int main(int argc, char *argv[])
>>>> {
>>>> 	char str[100];	
>>>> 	
>>>> 	printf("\nEnter some text: ");
>>>> 	fgets(str, sizeof(str), stdin);
>>>> 	fseek(stdin,0,SEEK_END);  //flush stdin
>>>> 	
>>>> 	printf("Before: '%s'\n",str);
>>>> 	rtrim(str);
>>>> 	printf("After : '%s'\n",str);
>>>> }
>>>> ------------------------------------------------
>>>>
>>>> Seems to work well.  Any unobvious pitfalls?
> [...]
>>> Your fseek call also doesn't do what the comment says it does.  If stdin
>>> is an interactive device (which seems to be the intent), it will simply
>>> fail.
>>
>> It doesn't fail at all, but yes, I know it's not a true flush of stdin.
> 
> In the program you posted, you can't tell whether it fails because you
> don't test the result.


How could   fseek(stdin,0,SEEK_END);   ever fail?




>> This snippet is from a larger program, where I keyboard prompt 3x
>> consecutively.
>>
>> Without the "flush" after the 1st prompt, the code falls through to
>> the 3rd prompt without letting you enter anything for the 2nd prompt.
>>
>> Without the "flush" after the 2nd prompt, the 3rd prompts fails (falls
>> through without letting you enter anything).
>>
>> So I do those flushes after all 3 prompts and it works fine.
> 
> That's odd.  Since you talk about keyboard prompts, I presume
> that stdin is associated with a keyboard.  I would have expected
> fseek(stdin, ...)  to fail in that case.
> 
> Can you show a (possibly reduced version of) your program in which it
> works as you say it does?


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void rtrim(char * s) {s[strlen(s)-1]='\0';}	

/*
above is the original rtrim(), subject of this thread.  I've since 
replaced it with:

void rtrim(char * s)
{
     int i=0, index=0;
     while(s[i] != '\0')
     {
         if(s[i]!=' ' && s[i]!='\t' && s[i]!='\n') {index= i;}
         i++;
     }
     s[index+1]='\0';
}	
*/


int main(int argc, char *argv[])
{	

	//vars
	char fileop[8];
	char separator[2];
	char filein[100];
	int i;
	FILE *fin;
	
	//get values from keyboard
	fseek(stdin,0,SEEK_END);

	printf("Choose file operation:\n1. Summary\n2. Data Operations\n");
	i = getchar();
	memset(fileop, '\0', sizeof(fileop));
	if (i=='1') {strcpy(fileop,"summary");}
	if (i=='2') {strcpy(fileop,"data");}
	fseek(stdin,0,SEEK_END);
		
	printf("\nChoose field separator:\n");
	printf("1. pipe  |\n2. pound #\n3. tilde ~\n4. comma ,\n");
	i = getchar();
	memset(separator, '\0', 2);
	if (i=='1') {strcpy(separator,"|");}
	if (i=='2') {strcpy(separator,"#");}
	if (i=='3') {strcpy(separator,"~");}
	if (i=='4') {strcpy(separator,",");}
	fseek(stdin,0,SEEK_END);

	printf("\nEnter name of .csv file:\n");
	fgets(filein, sizeof(filein), stdin);
	rtrim(filein);
	fseek(stdin,0,SEEK_END);


//that's all 3 keyboard prompts.  After this I open the .csv "filein", 
count headers and data rows, print the results, then close the file and exit

}




> I just did a test.  On Ubuntu Linux, using the above program, fseek()
> fails and sets errno to 29 (ESPIPE, Illegal seek).  On Windows
> (compiling with x86_64-w64-mingw32-gcc) the fseek() call succeeds,
> but I'm not sure what it does.
> 
> If you want to discard input (for example, all characters up to and
> including the next newline), it's probably better to read and discard
> them
> 
>      int c; do c = getchar(); while (c != EOF && c != '\n');
> 
> rather than playing tricks with fseek().  (It's not necessary in your
> small example because fgets() has already read the newline -- assuming
> the input line isn't too long.)
> 
> [...]


FYI: tcc 0.9.26 on Windows

$tcc -Wall csvmgr.c -o csvmgr.exe

compiles with no warnings

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


Thread

Function to trim trailing spaces DFS <nospam@dfs.com> - 2019-01-23 21:00 -0500
  Re: Function to trim trailing spaces Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-01-23 19:14 -0700
    Re: Function to trim trailing spaces DFS <nospam@dfs.com> - 2019-01-23 23:41 -0500
      Re: Function to trim trailing spaces Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-01-23 22:40 -0700
        Re: Function to trim trailing spaces Bart <bc@freeuk.com> - 2019-01-24 10:44 +0000
        Re: Function to trim trailing spaces DFS <nospam@dfs.com> - 2019-01-24 11:20 -0500
          Re: Function to trim trailing spaces Bart <bc@freeuk.com> - 2019-01-24 16:47 +0000
            Re: Function to trim trailing spaces DFS <nospam@dfs.com> - 2019-01-24 13:51 -0500
          Re: Function to trim trailing spaces Manfred <noname@invalid.add> - 2019-01-24 17:49 +0100
        Re: Function to trim trailing spaces James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-01-24 08:52 -0800
          Re: Function to trim trailing spaces Bart <bc@freeuk.com> - 2019-01-24 17:40 +0000
          Re: Function to trim trailing spaces James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-01-24 12:08 -0800
            Re: Function to trim trailing spaces Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-01-24 14:47 -0700
              Re: Function to trim trailing spaces scott@slp53.sl.home (Scott Lurndal) - 2019-01-25 14:40 +0000
          Re: Function to trim trailing spaces Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-01-24 14:44 -0700
      Re: Function to trim trailing spaces Barry Schwarz <schwarzb@dqel.com> - 2019-01-23 23:39 -0800
        Re: Function to trim trailing spaces Rosario19 <Ros@invalid.invalid> - 2019-01-24 10:49 +0100
          Re: Function to trim trailing spaces mark.bluemel@gmail.com - 2019-01-24 01:55 -0800
    Re: Function to trim trailing spaces "Pascal J. Bourguignon" <pjb@informatimago.com> - 2019-01-24 12:58 +0100
    Re: Function to trim trailing spaces Nathan Wagner <nw@hydaspes.if.org> - 2019-01-24 03:01 +0000
  Re: Function to trim trailing spaces Keith Thompson <kst-u@mib.org> - 2019-01-23 18:46 -0800
    Re: Function to trim trailing spaces DFS <nospam@dfs.com> - 2019-01-23 23:38 -0500
      Re: Function to trim trailing spaces Barry Schwarz <schwarzb@dqel.com> - 2019-01-23 23:39 -0800
        Re: Function to trim trailing spaces DFS <nospam@dfs.com> - 2019-01-24 11:25 -0500
          Re: Function to trim trailing spaces mark.bluemel@gmail.com - 2019-01-24 08:34 -0800
          Re: Function to trim trailing spaces Barry Schwarz <schwarzb@dqel.com> - 2019-01-24 15:05 -0800
            Re: Function to trim trailing spaces DFS <nospam@dfs.com> - 2019-01-24 18:40 -0500
      Re: Function to trim trailing spaces Keith Thompson <kst-u@mib.org> - 2019-01-24 02:29 -0800
        Re: Function to trim trailing spaces DFS <nospam@dfs.com> - 2019-01-24 15:06 -0500
          Re: Function to trim trailing spaces Mateusz Viste <mateusz@wont.tell> - 2019-01-24 20:07 +0000
            Re: Function to trim trailing spaces scott@slp53.sl.home (Scott Lurndal) - 2019-01-24 20:24 +0000
              Re: Function to trim trailing spaces Mateusz Viste <mateusz@wont.tell> - 2019-01-24 20:34 +0000
                Re: Function to trim trailing spaces scott@slp53.sl.home (Scott Lurndal) - 2019-01-24 21:37 +0000
          Re: Function to trim trailing spaces Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2019-01-24 15:34 -0500
          Re: Function to trim trailing spaces James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-01-24 13:19 -0800
          Re: Function to trim trailing spaces Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-01-24 14:52 -0700
            Re: Function to trim trailing spaces James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-01-24 14:30 -0800
              Re: Function to trim trailing spaces Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2019-01-24 16:59 -0700
          Re: Function to trim trailing spaces Keith Thompson <kst-u@mib.org> - 2019-01-24 14:51 -0800
  Re: Function to trim trailing spaces mark.bluemel@gmail.com - 2019-01-24 01:06 -0800
    Re: Function to trim trailing spaces Keith Thompson <kst-u@mib.org> - 2019-01-24 02:13 -0800
      Re: Function to trim trailing spaces mark.bluemel@gmail.com - 2019-01-24 03:46 -0800
    Re: Function to trim trailing spaces Anton Shepelev <anton.txt@g{oogle}mail.com> - 2019-01-24 14:56 +0300
      Re: Function to trim trailing spaces mark.bluemel@gmail.com - 2019-01-24 05:14 -0800
      Re: Function to trim trailing spaces Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-01-24 18:51 +0000
        Re: Function to trim trailing spaces "Pascal J. Bourguignon" <pjb@informatimago.com> - 2019-01-24 20:04 +0100
          Re: Function to trim trailing spaces Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-01-24 20:25 +0000
          Re: Function to trim trailing spaces Anton Shepelev <anton.txt@gmail.com> - 2019-01-24 23:50 +0300
        Re: Function to trim trailing spaces Anton Shepelev <anton.txt@gmail.com> - 2019-01-24 23:45 +0300
          Re: Function to trim trailing spaces Anton Shepelev <anton.txt@gmail.com> - 2019-01-24 23:54 +0300
      Re: Function to trim trailing spaces Ralf Damaschke <rwspam@gmx.de> - 2019-01-24 22:43 +0000
  Re: Function to trim trailing spaces Bart <bc@freeuk.com> - 2019-01-24 10:57 +0000
    Re: Function to trim trailing spaces Bart <bc@freeuk.com> - 2019-01-24 23:05 +0000
  Function to trim trailing spaces "I Set My Girlfriend's Pantyhose on Fire" <cdalten@gmail.com> - 2019-01-24 03:53 -0800

csiph-web