Kaskus

Tech

taufikfahrudinAvatar border
TS
taufikfahrudin
[TANYA C] Menghapus Cur to End
Ane sudah buat kode sebagai berikut :
Code:

/*
* This program displays the names of all files in the current directory.
*/

#include <dirent.h>
#include <stdio.h>
#include <string.h>

int Search_in_File(char *fname);

int main(void)
{
DIR *d;
struct dirent *dir;
d = opendir(".");
if(d)
{
while ((dir = readdir(d)) != NULL)
{
if(strlen(dir->d_name)>4)
{
printf("Scanning:%s\n", dir->d_name);
Search_in_File(dir->d_name);
printf("------------------\n");
}
}
closedir(d);
}
return(0);
}

/*this function from [url]http://www.codingunit.com/c-tutorial-searching-for-strings-in-a-text-file[/url]
and edited by Taufik Fahrudin*/
int Search_in_File(char *fname) {
FILE *fp;
int line_num = 1;
int find_result = 0;
char temp[512];
char str[]="[removed]<!--";
int seekstart=0;
int seekend=0;

if((fp = fopen(fname, "r+")emoticon-Wink== NULL) {
return(-1);
}

while(fgets(temp, 512, fp) != NULL) {
if((strstr(temp, str)) != NULL) {
printf("A match found on line: %d\n", line_num);
seekstart=ftell(fp);
fseek(fp,0,SEEK_END);
seekend=ftell(fp);
printf("\n%s and from %d to %d\n", temp,seekstart,seekend);
find_result++;
fseek(fp,seekstart,SEEK_SET);
printf("Resetting seek to %d\n",ftell(fp));
//part edited from [url]http://www.thegeekstuff.com/2012/07/c-file-handling/[/url]
memset(temp, 0, sizeof(temp));
if(1*5 != fwrite(temp,1,seekend-seekstart,fp))
{
printf("\n fwrite() failed\n");
return 1;
}

printf("====");
break;
}
line_num++;
}

if(find_result == 0) {
printf("\nFile looks clean.\n");
}

//Close the file if still open.
if(fp) {
fclose(fp);
}
return(0);
}


tapi masih perlu banyak banyak perbaikan, baru mentok di sini (newbie mode). Inginnya sih bisa menghapus dari current seek ("<SCR...") sampai end filenya..

Contoh Filenya:
Spoiler for contoh potongan file:


Ane sekalian belajar C+HTML sekalian juga buat nie program kecil untuk mengheal virus VBS yang dah menjangkit banyak sekali file2 HTML..
Diubah oleh taufikfahrudin 09-11-2012 10:38
0
680
1
GuestAvatar border
Komentar yang asik ya
Urutan
Terbaru
Terlama
GuestAvatar border
Komentar yang asik ya
Komunitas Pilihan