我有一个名为" input.txt" 的文件,其中包含一些值。我正在编写将在该文件中找到最小值的程序,并将该最小值替换为命令行参数给出的数字 - 如果该命令行参数大于最小值。这些值代表室温,因此可以使用事实来找到最小值。此外,需要锁定文件的那部分(新号码替换最小值)。
示例:
$ ./prog 23
文件:21 25 19 22 24
文件:21 25 23 22 24
$ ./prog 29
文件: 21 25 23 22 24
文件: 29 25 23 22 24
代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdint.h>
/* Function that handles errors */
void fatal_error(const char *message){
perror(message);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv){
if(argc != 2)
fatal_error("Bad arguments.\n");
/* Fetching command line argument */
int temp = atoi(argv[1]);
/* Opening file and checking for errors */
FILE *file = fopen("input.txt", "r+");
if(!file)
fatal_error("Unable to open file.\n");
/* Finding minimum in the file */
int min = 200;
int value;
while(fscanf(file, "%d", &value) != EOF)
if(value < min)
min = value;
/* Exiting if nothing needs to change */
if(temp <= min)
return 0;
/* Creating file descriptor from stream and checking for errors */
int fdOpen = fileno(file);
if(fdOpen == -1)
fatal_error("Unable to open file descriptor.\n");
/* Moving offset to the beginning of the file */
off_t of = lseek(fdOpen,0,SEEK_SET);
printf("Ofset pre petlje: %jd\n", (intmax_t)of);
while(1){
/* I'm reading file all over again */
if(fscanf(file, "%d", &value) == EOF)
fatal_error("Reached end of file.\n");
/* If I reached minimum */
if(value == min){
/* I lock that part of the file - temperatures are two digit numbers*/
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_CUR;
lock.l_start = -2;
lock.l_len = 2;
/* I create lock */
if(fcntl(fdOpen,F_SETLK, &lock) == -1){
if(errno == EACCES || errno == EAGAIN)
fatal_error("File is locked.\n");
}
/* Moving two positions back from current position */
off_t offset;
if((offset = lseek(fdOpen, -2, SEEK_CUR)) == -1)
fatal_error("lseek error.\n");
/* Inserting read command line value into file */
fprintf(file, "%d", temp);
/* Unlocking */
lock.l_type = F_UNLCK;
if(fcntl(fdOpen, F_SETLK, &lock) == -1)
fatal_error("Unable to destroy lock.\n");
/* Closing file descriptor, and breaking loop */
close(fdOpen);
break;
}
}
return 0;
}
然而,这并不起作用。档案不会改变。问题是 - 我知道这是正确的方法。我写的基本上是相同的程序,例如每次出现单词&#34; aa&#34; 到&#34; bb&#34; 。这基本上是相同的概念。
我尝试过:
fscanf()
之前和之后获取偏移量
while()
循环。在第一个fscanf
偏移量设置为0之前 - 开始
的文件。在第一个fscanf
偏移设置为文件末尾后,
每次迭代后,偏移量仍保留在文件的末尾。ftell()
和printf()
中使用ftell()
给了我正确的偏移量。但是while循环中的ftell()
仍然存在
给出文件的结尾。我也尝试使用fseek()
而不是。{
lseek()
(即使我知道fseek()
使用lseek()
实现)。仍然,结果相同。char*
缓冲区而不是值。基本上要用
fscanf(file,"%s",buffer)
,转换该值以检查是否已读取
价值是最低的,之后我就用了
fprintf(file,"%s",buffer)
写下来。但结果相同。代码:
这是我提到的第二个使用相同概念的程序。这段代码有效,但我也尝试过打印偏移量,偏移量也在文件的末尾。
int main(int argc, char **argv){
if(argc != 4)
fatal_error("You must enter exactly 4 arguments.\n");
FILE *f = fopen(argv[1], "r+");
if(!f)
fatal_error("Unable to open file for reading and writing.\n");
int fd = fileno(f);
if(fd == -1)
fatal_error("Unable to fetch file descriptor for file.\n");
char word[MAX_LEN + 1];
int word_len = strlen(argv[2]);
while(fscanf(f,"%s",word) != EOF){
printf("%jd\n", lseek(fd,0,SEEK_CUR));
if(!strcmp(argv[2],word)){
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_CUR;
lock.l_start = -word_len;
lock.l_len = word_len;
if(fcntl(fd, F_SETLKW, &lock) == -1)
fatal_error("File locking failed.\n");
if(lseek(fd, -word_len, SEEK_CUR) == -1)
fatal_error("Lseek error.\n");
fprintf(f, "%s", argv[3]);
lock.l_type = F_UNLCK;
if(fcntl(fd, F_SETLK, &lock) == -1)
fatal_error("Failed to release lock.\n");
}
}
}
正如您所看到的,这是完全相同的概念。第二个程序有效,第一个没有。
我现在很困惑。如果我从文件流创建文件描述符,然后在该文件描述符上使用lseek()
来更改偏移量,流的偏移量是否也会改变?如果您使用fscanf()
从流中读取内容,那么offset_t
的更改与您从文件中读取的内容一样多吗?如果我使用fscanf()
格式说明符%d
和%s
,更改偏移量有什么不同吗?
答案 0 :(得分:1)
如果(源文本和替换文本)在长度(aa)== length(bb)的情况下具有相同的长度,那么您想要替换值的方式将仅。主要是您应该小心使用FILE*
和int fd
描述符,并在退出之前始终关闭该文件。
在close(fd)
之前调用fclose(f)
将导致不写入缓冲数据。
另一个问题 - 相对于SEEK_CUR锁定文件区域不会锁定要修改的文件部分
这里有一些有效修改的代码:
int main(int argc, char **argv){
if(argc != 4)
fatal_error("You must enter exactly 3 arguments.\n");
if(strlen(argv[2])!=strlen(argv[3]))
fatal_error("src&dst words must be the length.\n");
FILE *f = fopen(argv[1], "r+");
if(!f)
fatal_error("Unable to open file for reading and writing.\n");
int fd = fileno(f);
if(fd == -1)
fatal_error("Unable to fetch file descriptor for file.\n");
char word[MAX_LEN + 1];
int word_len = strlen(argv[2]);
while(fscanf(f,"%s",word) != EOF){
printf("%jd\n", ftell(f));
if(!strcmp(argv[2],word)){
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = ftell(f)-word_len;
lock.l_len = word_len;
if(fcntl(fd, F_SETLKW, &lock) == -1)
fatal_error("File locking failed.\n");
fseek(f,-word_len,SEEK_CUR); //FILE* based seek
fprintf(f, "%s", argv[3]);
fflush(f); //sync output
lock.l_type = F_UNLCK;
if(fcntl(fd, F_SETLK, &lock) == -1)
fatal_error("Failed to release lock.\n");
}
}
fclose(f); // close the file
}
Update1 :FILE
界面有自己的缓冲,与int fd
不同步。
所以主要的问题是使用lseek,而应该使用fseek
Update2 :代码循环查找最小值
int main(int argc, char **argv){
if(argc != 3)
fatal_error("You must enter exactly 2 arguments.\n");
if(strlen(argv[2]) != 2)
fatal_error("replace num must have 2 digits.\n");
FILE *f = fopen(argv[1], "r+");
if(!f)
fatal_error("Unable to open file for reading and writing.\n");
int fd = fileno(f);
if(fd == -1)
fatal_error("Unable to fetch file descriptor for file.\n");
// search for minimum
int word_len = strlen(argv[2]);
int value, minValue;
long minOffs=-1;
while(fscanf(f,"%d",&value) == 1){ //compare number of parsed items
printf("test value %d\n", value);
if (minValue > value) {
minValue = value;
minOffs = ftell(f) - word_len;
}
}
// replace if found
if (minOffs >= 0) {
printf("replacing v=%d at %ld\n", minValue, minOffs);
struct flock lock;
memset(&lock, 0, sizeof(lock));
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = minOffs;
lock.l_len = word_len;
fseek(f,minOffs,SEEK_SET);
if(fcntl(fd, F_SETLK, &lock) == -1)
fatal_error("File locking failed.\n");
fprintf(f, "%s", argv[2]);
fflush(f); //sync output
lock.l_type = F_UNLCK;
if(fcntl(fd, F_SETLK, &lock) == -1)
fatal_error("Failed to release lock.\n");
}
fclose(f);
}