Is there a way to search a text file and delete every line that for example begins with the word Repo or the number 9?
Is there a way to search a text file and delete every line that for example begins with the word Repo or the number 9?
Yes. However, instead of doing this in C, you might want to do this using sed if it is available.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Hi @laserlight,
SED is great but in my program, my very first call is (system) and I don’t want to call system again unless it’s extremely urgent per application.
Other then removing a line by its number, is what I asking that difficult for C?
It's not that difficult to do in C.
It's just that it's a hell of a lot easier to do in sed.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Since my undertaking with C nearly every site I been about this subject a person would suggest using (system), but somewhere in that same threads somebody would provide a warning of why not to use shell-calls in C or why not to use too many.
I figure, why I should be the challenger. That is why, I prefer to see how it’s done in C. If it eats up more resources then needed, or end up being a large code-set to handle, I don’t care! I will still be learning something that I will eventually need to learn how to do, anyway. Thank you for you reply @Salem
Step 1. learn how to open the file and read it line by line
Step 2. use string.h functions to edit the line.
Step 3. Output the edited line to new output file
Step 4. Delete input file and rename output file to new name.
Tim S.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
I didn't suggest using system; I suggested using sed. There is no need to write a C program that calls system to invoke sed. Just invoke sed. If you do have other things to do that would be appropriate for a C program, either pipe output from sed as input into the C program, or vice versa. This is the basic Unix approach to having utilities that do one thing and do it well such that they can be used together to accomplish complex tasks.Originally Posted by jc2020
Last edited by laserlight; 07-05-2019 at 09:31 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks @stahta01. Your breakdown prove it IS a lot of stress, especially for tons of unneeded strings inside a bunch of large files. It’s going to be interesting to see what kind of impact its has on system memory. That’s all that counts other then speed. It makes since so it’s going to get in where it fit in. I’ll post my hardware result right here. I waste more time comparing everything I can keep up with since day1 with my OS, and now, C Thanks again @stahta01
Last edited by jc2020; 07-06-2019 at 12:06 AM.
@Laserlight, I apologize for my misinterpretation of your reply. Actually, I know nothing above using (system) in C. Can I say: May the sky cave in if one had no faith in your conclusions, codes and dedication to this community. I might be new but I was lucky to find and follow you for months though I understood nothing much.I didn't suggest using system; I suggested using sed. There is no need to write a C program that calls system to invoke sed. Just invoke sed.
I wish I knew how to pipe sed to a C program because my intent IS to complete this application in one C file. The best google turn up is one or the other but nothing about how to use both together. Makes me think of the future of AI misfires. Maybe someone will show a sample of how that is done. I'm going to try bing.com.If you do have other things to do that would be appropriate for a C program, either pipe output from sed as input into the C program, or vice versa.
Have a wonderful tomorrow++ Ms. @Laserlight
Last edited by jc2020; 07-06-2019 at 01:52 AM.
I figured out a way where I can delete by line-numbers. All it took was a little bit more planning. There are tons of examples for that on the internet and here. My most important lesson about C is if you cannot find what you thinking of on the internet in C it probably can’t be done without massive coding if you want to keep everything in one C file.