File Handling in C - Removing specific words from a list in text file

后端 未结 2 1371
猫巷女王i
猫巷女王i 2021-01-25 14:12

I am populating a short dictionary from my basic C program using the following code :

void main () {

FILE *fp;

fp = fopen(\"c:\\\\CTEMP\\\\Dictionary2.txt\", \         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-25 14:58

    • You open two files: the one you've got (for reading) and a new one (for writing).
    • You loop through the first file reading each line in turn.
    • You compare the contents of each line with the words you need to delete.
    • If the line does not match any of the deletion words, then you write it to the new file.

    If the manipulation that you need to do is much more complex then you can literally "read it into memory" using mmap(), but that is a more advanced technique; you need to treat the file as a byte array with no zero terminator and there are lots of ways to mess that up.

提交回复
热议问题