0

Apologies if this is unclear, but I'm really not sure how to describe this.

See, my issue is that I'm trying to read from the text for user input using fgets(), however, I also need to know if the user presses a special key like /b (backspace) since ideally I want it to start deleting characters from the line before if the current line is empty, like a text editor, which isn't possible with C.

Anyways, let me know if you need more information, thanks in advance.

EDIT: Thought I'd go ahead and post what I have as of now in case someone comes across this later with the same issue to get a better idea

for (; ;) 
 {  
  int i; 
  int key = getch();
  
  if (key == '/b')
  {  
   printf("Hello World");
  }  

  else
  {  
   buffer[i++] = key; // adding character to text user is writing
  }  
  i++;
 }

note that this code doesn't work at the moment because of a linker error and something with the /b, but in essence, this could work.

EDIT 2: Thank you chqrlie for bringing up the right way to refer to special characters. Forgot you had to use the backslash for them.

cs07
  • 15
  • 4
  • in order to know if the user presses a key, you will need to handle it. If you are using linux, maybe signal handler will be useful. – Vincent Apr 23 '22 at 21:43
  • 1
    No, it's not possible with `fgets` nor with anything from the C standard library. http://c-faq.com/osdep/cbreak.html – jamesdlin Apr 23 '22 at 21:45
  • What you are asking for is not possible in plain ISO C. However, most operating systems offer extensions which offer the features that you require. For example, on Linux, you can use [ncurses](https://en.wikipedia.org/wiki/Ncurses), and on Microsoft Windows, you can use the [Console API](https://learn.microsoft.com/en-us/windows/console/consoles). Therefore, if you want an answer to your question, you will have to specify to which operating system your question applies. – Andreas Wenzel Apr 23 '22 at 21:57
  • Maybe implement `getch()` in a loop? See [Alternative function in iostream.h for getch() of conio.h?](https://stackoverflow.com/questions/1377403/alternative-function-in-iostream-h-for-getch-of-conio-h) – kmoser Apr 23 '22 at 21:57
  • My bad for not responding, I was implementing the fix. I would say what I did but I just noticed kmoser wrote what I did while I wasn't looking at the post. – cs07 Apr 23 '22 at 21:59
  • 3
    This is an X-Y problem because you are asking how to use `fgets()` to solve a problem that it cannot solve. Better to ask how to solve your problem rather than ask how to use a specific function to solve your problem. – Clifford Apr 23 '22 at 22:05
  • Use `'\b'` instead of `'/b'` – chqrlie Apr 23 '22 at 22:06

1 Answers1

0

You can certainly write text editors in C :-) A lot of them are.

However, you can't write them in portable C because the facilities required for interactive character-by-character terminal I/O don't exist in standard C. They don't exist because standard C doesn't assume that there is such a thing as a terminal (lots of embedded CPUs don't have any such thing, for example), and it would go far beyond the mandate of ISO C to try to standardise the disparate communications protocols of the various operating systems which do have terminal-like I/O.

getch is an obsolete interface in the Windows conio.h header, and also one of the interfaces in the Curses library, which is available for many operating systems (including Windows). It has nothing to do with fgets, which is part of standard C, and it cannot be implemented using fgets.

It seems likely that what you are trying to do could best be accomplished using an implementation of Curses. (If you're using Linux, as suggested by your avatar, you would be looking for Ncurses.) You might want to look at the Forms library built on top of Ncurses, which provides higher-level input facilities, like editable text boxes.

Another common input-handling library, GNU readline, might also be useful, but it doesn't handle multiline input forms so it would probably be a lot more work.

rici
  • 234,347
  • 28
  • 237
  • 341
  • You *could* write interactive character-by-character handling in *portable* C, that isn't the problem -- the problem is that you somehow have to configure the *terminal* to send you raw input... – DevSolar Apr 24 '22 at 18:21
  • @devsolar: you could write the character-handling but you can't make it interactive. It's precisely that that I meant by "the facilities required". – rici Apr 24 '22 at 18:39
  • Yeah this is what I was implementing, marked this as the answer since you explained great. – cs07 Apr 24 '22 at 21:55