0

I am writing a library program and I want to organize data on books in a .txt file. I want to be able to change the title of the book as I want instead of rewriting the title after printing it on the screen.

    char bookName[30] = "A Clockwork Orange";
    printf("Edit Book Name --> %s", bookName);
    gets(bookName);

If I use the program I wrote above, it will be like this:
Edit book name -> _
the cursor will stop here (_) and I will have to rewrite the title.

I want to do this:
Edit book name -> A Clockwork Orange_
I want to change the bookName variable I printed on the screen, not rewrite it.

I would be glad if you help

Dagbfatih
  • 1
  • 2
  • 2
    Check [GNU Readline](https://tiswww.case.edu/php/chet/readline/rltop.html) – David Ranieri Nov 23 '20 at 14:48
  • 1
    @DavidRanieri ... or, maybe easier, `rlwrap executable` ([`rlwrap` is a `readline` wrapper](https://github.com/hanslub42/rlwrap)) – pmg Nov 23 '20 at 20:01
  • thanks for 'rlwrap is a readline wrapper' I did not understand how to install this library, can you help? @pmg – Dagbfatih Nov 25 '20 at 08:49
  • rlwrap is not a library; it's a standalone executable. You don't have to change your code in any way, simply have to call `rlwrap yourexe ...` rather than `yourexe ...` from the command line. There should be a package for your distribution... don't know about Windows. – pmg Nov 25 '20 at 08:52
  • I am very amateur in windows, also my English level is low so can you explain a little more? I could not understand what to do – Dagbfatih Nov 25 '20 at 09:30

2 Answers2

2

you can use the following code :

char bookName[30] = "A Clockwork Orange";
    printf("Edit Book Name --> %s \n", bookName);
    printf("editing book name ...");
    scanf("%s",bookName);
    printf("your new Book Name --> %s", bookName);

    return 0;
  • Thank you for your attention, but that's not exactly what I want. Edit Book Name -> A Clockwork Orange After giving this output to the screen, changing the place where it says "A Clockwork Orange" as we want, then press enter to save the new value. For example: Edit Book Name -> A Clockwork Orange 2 (enter) I've added '2' to the end of the book name and now the new value of the variable 'bookName' will be "A Clockwork Orange 2". – Dagbfatih Nov 23 '20 at 16:10
  • Okay now i understand what you want , you have to iterate bookName to know the number of the title , so you have to follow these steps: +iterate bookNmae to get the last character + check the last character is a number or not with function isdigit +if isdigit true you should stock the number in another variable then increment it – Aladin Cherki Nov 23 '20 at 16:46
  • I understood what you mean, but that was not what I wanted to say, I explained my problem in detail below, I hope we can get along :) – Dagbfatih Nov 23 '20 at 18:19
  • I just want to be able to change a value read with printf () – Dagbfatih Nov 23 '20 at 20:52
  • It's not a sample task , I suggest you to build a desktop application – Aladin Cherki Nov 23 '20 at 23:30
0

Thank you, but what I wanted is still not fully understood :) Maybe because my English is a bit poor. Now the thing is as follows; I have already saved the books saved in the library to a file (.txt). I get the name of a book I want from the file, edit it and save it again. Take the 3rd book for example: Inside the file:

  1. To Kill A Mockingbird
  2. Brave New World
  3. A Clockwork Ornage
  4. The Secret History

We read the 3rd book from the file and assign it to the variable named bookName:

fgets(bookName, 30, file); 

Now I want to edit this book because as you can see above, I misspelled the title of the book (A Clockwork Ornage).
I want the program to:
When I tell my program that I want to edit it, the program will print

Edit Book Name -> A Clockwork Ornage

And I will click on the 15th character ('a') with the mouse and I will delete 2 characters (with backspace) and type "an" and now the console will write:

Edit Book Name -> A Clockwork Orange 

   

And by pressing 'enter' I will change the value of the bookName variable to its new value, its new value will be: "A Clockwork Orange". Here I could not do the part up to here. This is where I stumble, how can I change something I have printed to the console with printf().

Dagbfatih
  • 1
  • 2