-2

Which function, we use in C language to read a separate value from multiple lines like name phone address .. example of what i need

name:.....
phone:.....
address:....

Now after printing all this informations then the user can start fill in each one

halfer
  • 19,824
  • 17
  • 99
  • 186
Anas Kasmi
  • 43
  • 7
  • This question leaves me with *no idea at all* what your actual problem might be. An [mcve](https://stackoverflow.com/help/mcve) would help and make sure to read the other help-pages as well, e.g. [how to ask](https://stackoverflow.com/help/how-to-ask). As you write you want to read whole lines and `scanf()` doesn't work, *maybe* my [document on scanf](http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html) could help you as well. –  Jun 24 '17 at 13:26
  • Are you looking to read from lines **of a file**? Or lines **in console input**? – halfer Jun 24 '17 at 13:28
  • Since you need to show all the titles which user have to fill in advance, so you have to use graphics. It is not supported by `C` standard. You should use some graphics library based upon your platform. Mention the platform on which you are working i.e. UNIX or Windows? – cse Jun 24 '17 at 13:28
  • actually its in console input – Anas Kasmi Jun 24 '17 at 13:31
  • After reading your update: My document won't help you. `stdio` in C works in input and output *streams*, there's no notion of cursor position and things like that. There are platform-specific APIs, e.g. the [Windows Console API](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682087(v=vs.85).aspx). If you want to write portable code, look into `curses`. [...] –  Jun 24 '17 at 13:32
  • For Linux and many other Unix-like systems, there's [`ncurses`](https://www.gnu.org/software/ncurses/), for windows, you can use [`pdcurses`](https://pdcurses.sourceforge.io/), both provide the same API. For an introduction to `curses`, [look for example here](http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/) or ask google for many other resources. –  Jun 24 '17 at 13:33
  • But as you're trying to do something like that using `scanf()`, I assume you need more basic training before attempting to write a curses program (no offense intended!). In that case, have a look at my document linked above and first try to do some basic I/O using `stdio` streams. –  Jun 24 '17 at 13:35
  • @AnasKasmi Even if it is console application, you requirement is to move your cursor location in `X and Y` coordinates on the screen. So you will need graphics library to move cursor position. – cse Jun 24 '17 at 13:36
  • @cse *graphics library* isn't correct. Windows provides the console API (see my link above) that could be used directly. For portable code, `curses` is the standard way to go, and this isn't a *graphic library* either, it just abstracts console control behind a common API. –  Jun 24 '17 at 13:37

1 Answers1

0

To get you started, here's a simple example program using an "input mask" like you outlined:

#include <curses.h>
#include <stdio.h>

int main(void)
{
    char name[60];
    char phone[60];
    char addr[60];

    initscr();
    mvaddstr(0, 0, "name:");
    mvaddstr(1, 0, "phone:");
    mvaddstr(2, 0, "address:");

    mvgetnstr(0, 9, name, 59);
    mvgetnstr(1, 9, phone, 59);
    mvgetnstr(2, 9, addr, 59);

    endwin();
    // using curses ends here, so we can use `stdio` functions like
    // `printf()` again.

    printf("name: %s\n", name);
    printf("phone: %s\n", phone);
    printf("address: %s\n", addr);

    return 0;
}

Link this with a curses library suitable for your platform (on Linux, use -lncurses when compiling, on windows, get and link pdcurses).

I suggest you read the NCURSES Programming HOWTO for learning more about curses.

  • thank you all i just asked my teatcher and he advise me to use gotoxy function and im looking now of how it works if any one could help me i'll apreciate it – Anas Kasmi Jun 24 '17 at 13:56
  • `gotoxy()` is part of `conio.h`, a long deprecated console API found for example with compilers for `MS-DOS`. I highly doubt you would find it anywhere nowadays. –  Jun 24 '17 at 13:57
  • @AnasKasmi you might use an ancient platform with an ancient compiler like *Turbo C*, in this case, [here's some documentation](http://www.programmingsimplified.com/c/conio.h). Be aware this is *not* portable to *any* modern system and don't mix `conio` calls with `stdio`. If you **can**, prefer `curses`. –  Jun 24 '17 at 14:07
  • thank you im reading you documentaion right now and i actualy using codebloks with GNU GCC compiler – Anas Kasmi Jun 24 '17 at 14:12
  • @AnasKasmi Then your teacher's advice isn't the best. People wrote `conio` replacements for MinGW, like [this project](http://conio.sourceforge.net/docs/html/), but you'd really be better of using `curses` as I already outlined. –  Jun 24 '17 at 14:17
  • thank you you right im going the use curses because non of the functions of conio.h worked i think that GNU GCC compiler doesnt support it – Anas Kasmi Jun 24 '17 at 14:30
  • @AnasKasmi technically, it's not the compiler. If you add the code from the project I linked above, it *would* probably work. But the difference is: `curses` is an API that's still used and implemented on nearly any system providing a console. If your code uses `curses`, you WILL find libraries implementing this, be it for Windows, Linux, FreeBSD, Solaris, MacOS X, even MS-DOS. So it's the better choice :) –  Jun 24 '17 at 14:32
  • @AnasKasmi unfortunately, I have no experience with Code::Blocks, but I could tell you what you have to do at the command line. What is your operating system? –  Jun 24 '17 at 14:37
  • my OS is WIndows 7 – Anas Kasmi Jun 24 '17 at 14:39
  • @AnasKasmi then, for using `curses`, you will need the [`pdcurses`](https://pdcurses.sourceforge.io/) library. Just download [pdc34dll.zip](https://sourceforge.net/projects/pdcurses/files/pdcurses/3.4/pdc34dll.zip/download) and place the `.dll` somewhere, then add `-l/path/to/pdcurses.dll` to your gcc options when compiling. You will also need the provided `curses.h`, place it where your other `.h` files are. –  Jun 24 '17 at 14:44
  • @AnasKasmi if you want to link `pdcurses` statically (so your compiled `.exe` doesn't need `pdcurses.dll`), unfortunately you have to build `pdcurses` yourself from source. –  Jun 24 '17 at 14:45