Home > Back-end >  How do I remove a file containing the same part of the whole line, a freshman for help for help!!!!!
How do I remove a file containing the same part of the whole line, a freshman for help for help!!!!!

Time:11-18

Already have a TXT file, specific format is as follows:
Number (11) the title author publishing date number
Then use the program for a number, (manually enter any number)
Use STRCMP than number and file all the number (find file whether there is a number of books), if the same will delete this whole line!!!!! This how to do ah, help!
Two kinds of thinking, a row to delete an existing file, or create a new file does not include the same number line, ask for help!!!!!!
Thank you very much!

CodePudding user response:

Read the file line by line, judgment

CodePudding user response:

All read
All write back does not skip deleting rows data

CodePudding user response:

Fyi:
//to sort the contents of the file 1 and heavy, as a result, save to file 2 
#include
#include
#include
1024//# define MAXCHARS can deal with the biggest line width, including the end-of-line \ n and string tail \ 0
Int MAXLINES=10000, MAXLINES2;
Buf, char * * buf2;
Int c, n, hh, I, L;
The FILE * f;
Char ln [MAXCHARS];
Int ignore_case=0;
Int icompare (const void * arg1, const void * arg2) {
Return stricmp ((char *) arg1, arg2 (char *));
}
Int the compare (const void * arg1, const void * arg2) {
Return STRCMP ((char *) arg1, arg2 (char *));
}
Int main (int arg c, char * * argv) {
If (argc<3) {
Printf (" Unique line. Designed by [email protected]. The 2012-08-20 \ n ");
Printf (" SRC Usage: % s. TXT uniqued. TXT [-] I \ n ", argv [0]).
return 1;
}
If (argc> 3) ignore_case=1;//if there is a command line parameter 3, ignore case
F=fopen (argv [1], the "r");
If (NULL==f) {
Printf (" Can not find the file % s! \ n ", argv [1]);
return 1;
}
Buf=(char *) malloc (MAXLINES * MAXCHARS);
If (NULL==buf) {
The fclose (f);
Printf (" Can not malloc LINES * (% d % d CHARS)! The \ n ", MAXLINES, MAXCHARS);
return 2;
}
N=0;
Hh=0;
i=0;
While (1) {
If (NULL==the fgets (ln, MAXCHARS, f)) break;//
Hh++;
L=strlen (ln) - 1;
If (' \ n '!=ln [L]) {//long row behind ignore content
Printf (" % s Line % d long (& gt; % d), spilth ignored. \ n ", argv [1], hh, MAXCHARS);
While (1) {
C=fgetc (f);
If (' \ n '==c | | EOF==c) break;//
}
}
While (1) {//removes white Spaces at the end of each line '\ n' and
If (=='\ n' ln [L] | | '==ln [L]) {
Ln [L]=0;
L -;
If (L<0) break;//
} the else break;//
}
If (L>=0) {
Strcpy (buf + I, ln); I +=MAXCHARS;
n++;
If (n>={MAXLINES)
MAXLINES2=MAXLINES * 2;
If (MAXLINES2==1280000) MAXLINES2=2500000;
Buf2=(char *) realloc (buf, MAXLINES2 * MAXCHARS);
If (NULL==buf2) {
Printf (" Can not malloc LINES * (% d % d CHARS)! The \ n ", MAXLINES2, MAXCHARS);
Printf (" WARNING: Lines & gt; % d ignored. \ n ", MAXLINES);
break;//
}
Buf=buf2;
MAXLINES=MAXLINES2;
}
}
}
The fclose (f);
If (n> 1) {
If (ignore_case) tree (buf, n, MAXCHARS, icompare);
The else tree (buf, n, MAXCHARS, compare);
}
F=fopen (argv [2], "w");
If (NULL==f) {
Free (buf);
Printf (" Can not create the file % s! \ n ", argv [2]);
return 2;
}
Fprintf (f "% s \ n", buf);
If (n> 1) {
If (ignore_case) {
Hh=0;
L=MAXCHARS;
For (I=1; iBuf if (stricmp (const char *) + hh, buf (const char *) + L)) {
Fprintf (f "% s \ n", buf + L);
}
Hh=L;
L +=MAXCHARS;
}
} else {
Hh=0;
L=MAXCHARS;
For (I=1; iBuf if (STRCMP (const char *) + hh, buf (const char *) + L)) {
Fprintf (f "% s \ n", buf + L);
}
Hh=L;
L +=MAXCHARS;
}
}
}
The fclose (f);
Free (buf);
return 0;
}
  • Related