Hi everyone,
i met some difficulties handling the following problem. Say I have a input file in following format:
File Name File Number Size(blocks) Size(bytes)
file1 1 39 19968
file3 3 28 14336
file4 4 18 9216
file5 5 16 8192
file6 6 7 3584
file7 7 5 2560
file8 8 35 17920
file9 9 33 16896
file10 10 24 12288
My program suppose to simulate the rename operation on file.
For example, when u run "ren file1 file0", the input file will become as following:
File Name File Number Size(blocks) Size(bytes)
file0 1 39 19968
file3 3 28 14336
file4 4 18 9216
file5 5 16 8192
file6 6 7 3584
file7 7 5 2560
file8 8 35 17920
file9 9 33 16896
file10 10 24 12288
What I am able to do now is just insert the new file name "file0" in front of the old name in the input file,such as this:
File Name File Number Size(blocks) Size(bytes)
file0 file1 1 39 19968
file3 3 28 14336
file4 4 18 9216
file5 5 16 8192
file6 6 7 3584
file7 7 5 2560
file8 8 35 17920
file9 9 33 16896
file10 10 24 12288
so how can I delete the old file name? Any help would be greatly appreciated.
My current code is as following:
[CODE=c]main(int argc, char **argv){
FILE *list,*tmp;
char line[80];
char *pch;
if(argc!=3){
perror("You must have 2 argument.\n");
exit(1);
}
//generate the tmp list file
if((tmp = fopen("tmp.txt" , "w+"))==NUL L){
printf("Cannot create temp list file");
}
//do the actual rename
if(rename(argv[1], argv[2]))
printf("Rename failed.\n");
//rename the entry in the table
if((list = fopen("filelist .txt", "r+"))==NUL L){
printf("Cannot open the list file");
}
while(fgets(lin e, sizeof(line), list))
{
printf("Line read: %s\n", line);
if(strstr(line, argv[1]) !=NULL)
{
printf("Replaci ng.\n");
strcpy(line, "");
strcat(line, argv[2]);
}
fputs(line, tmp);
}
fclose(list);
fclose(tmp);
//remove filelist.txt
if (remove("fileli st.txt")){
printf("Cannot delete file.\n");
exit(1);
}
//rename tmp.txt to filelist.txt
if(rename("tmp. txt", "filelist.txt") )
printf("Rename failed.\n");
}[/CODE]
i met some difficulties handling the following problem. Say I have a input file in following format:
File Name File Number Size(blocks) Size(bytes)
file1 1 39 19968
file3 3 28 14336
file4 4 18 9216
file5 5 16 8192
file6 6 7 3584
file7 7 5 2560
file8 8 35 17920
file9 9 33 16896
file10 10 24 12288
My program suppose to simulate the rename operation on file.
For example, when u run "ren file1 file0", the input file will become as following:
File Name File Number Size(blocks) Size(bytes)
file0 1 39 19968
file3 3 28 14336
file4 4 18 9216
file5 5 16 8192
file6 6 7 3584
file7 7 5 2560
file8 8 35 17920
file9 9 33 16896
file10 10 24 12288
What I am able to do now is just insert the new file name "file0" in front of the old name in the input file,such as this:
File Name File Number Size(blocks) Size(bytes)
file0 file1 1 39 19968
file3 3 28 14336
file4 4 18 9216
file5 5 16 8192
file6 6 7 3584
file7 7 5 2560
file8 8 35 17920
file9 9 33 16896
file10 10 24 12288
so how can I delete the old file name? Any help would be greatly appreciated.
My current code is as following:
[CODE=c]main(int argc, char **argv){
FILE *list,*tmp;
char line[80];
char *pch;
if(argc!=3){
perror("You must have 2 argument.\n");
exit(1);
}
//generate the tmp list file
if((tmp = fopen("tmp.txt" , "w+"))==NUL L){
printf("Cannot create temp list file");
}
//do the actual rename
if(rename(argv[1], argv[2]))
printf("Rename failed.\n");
//rename the entry in the table
if((list = fopen("filelist .txt", "r+"))==NUL L){
printf("Cannot open the list file");
}
while(fgets(lin e, sizeof(line), list))
{
printf("Line read: %s\n", line);
if(strstr(line, argv[1]) !=NULL)
{
printf("Replaci ng.\n");
strcpy(line, "");
strcat(line, argv[2]);
}
fputs(line, tmp);
}
fclose(list);
fclose(tmp);
//remove filelist.txt
if (remove("fileli st.txt")){
printf("Cannot delete file.\n");
exit(1);
}
//rename tmp.txt to filelist.txt
if(rename("tmp. txt", "filelist.txt") )
printf("Rename failed.\n");
}[/CODE]
Comment