检查行,然后更改其在 txt 文件中的数据
Checking line and then changing its data on a txt file
所以,我有这个:
order.txt
file.txt;10;21:21
file2.txt;3;20:00
file2.txt;30;22:20
file4.txt;3;10:00
我希望用户给出所需的行(1 到 3,取决于存在的行数)然后更改其内容。例如:
第 3 行
"文件名: file.txt"
"秒: 5"
"时间 HH:MM: 20:00"
Output/Changes变成order.txt:
file.txt;10;21:21
file2.txt;3;20:20
file.txt;5;20:00
file4.txt;3;10:00
如您所见,第三行发生了变化。为此,我尝试了以下方法:
变量
FILE* orderFile;
FILE* contentFile;
char fileName[50];
char timeValue[10];
int seconds;
char orderNameFile[50];
char orderTimeFile[50];
int orderSecondsFile;
int count = 0;
int i = 0;
int line;
int position;
代码
/* This will show the content on it with the number of the line behind */
orderFile = fopen("order.txt","r+");
printf("\n");
while(fscanf(orderFile," %49[^;];%d; %49[^\n]",fileName,&seconds,timeValue) == 3)
{
i++;
printf("[%d] %s;%d;%s\n",i,fileName,seconds,timeValue);
}
printf("\n");
rewind(orderFile);
/* ******************************************************************* */
/* This will ask which line he wants to change */
do{
printf("Choose the line you want to change\n");
printf("[Line] ");
scanf("%d",&line);
printf("\n");
if(line > i)
{
printf("That line does not exist\n\n");
}
}while(line > i || line < 1);
/* ******************************************* */
/* This will ask for the file name, seconds and time in HH:MM */
printf("Insert the name of the file\n");
printf("[Name] ");
getchar();
scanf("%s",&orderNameFile);
printf("Insert the presentation time in seconds\n");
printf("[Time in seconds] ");
scanf("%d",&orderSecondsFile);
printf("Insert the hour it starts\n");
printf("[Time in HH:MM (Hour:Minutes)] ");
getchar();
scanf("%s%",&orderTimeFile);
/* ******************************************* */
/* This is what is supposed to edit the line I want but does not work */
while(fscanf(orderFile," %49[^;];%d; %49[^\n]",fileName,&seconds,timeValue) == 3)
{
count++;
if(count == line)
{
position = ftell(orderFile);
fseek(orderFile, position, SEEK_SET);
fprintf(orderFile,"%s;%d;%s",orderNameFile,orderSecondsFile,orderTimeFile);
break;
}
}
*/ ****************************************************************** */
fclose(orderFile);
我想要使用第一个示例的输出:
file.txt;10;21:21
file2.txt;3;20:20
file.txt;5;20:00
file4.txt;3;10:00
使用第一个示例@EDITED 给我的输出:
file.txt;10;21:21
file2.txt;3;20:00
file2.txt;30;22:20file.txt;5;20:00
file4.txt;3;10:00
有什么想法吗?
你必须存储这些行然后将它们重写到文件中,可能会有更多elegant/efficient解决方案,但对于一个简单的情况,这应该没问题,这是代码
#include <stdio.h>
#include <string.h>
int main()
{
FILE* orderFile;
char fileName[50];
char timeValue[10];
int seconds;
char orderNameFile[50];
char orderTimeFile[50];
int orderSecondsFile;
int count;
int lineCount;
int line;
char lineContent[128];
char fileLines[128][128];
/* This will show the content on it with the number of the line behind */
orderFile = fopen("order.txt", "r+");
if (orderFile == NULL)
return -1;
lineCount = 0;
while ((fgets(lineContent, sizeof(lineContent), orderFile) != NULL) && (lineCount < 128))
{
size_t length;
length = strlen(lineContent);
if (lineContent[length - 1] == '\n')
lineContent[length - 1] = 0;
if (sscanf(lineContent, " %49[^;];%d; %49[^\n]", fileName, &seconds, timeValue) == 3)
{
strcpy(fileLines[lineCount], lineContent);
printf("[%d] %s;%d;%s\n", ++lineCount, fileName, seconds, timeValue);
}
}
printf("\n");
rewind(orderFile);
/* This will ask which line he wants to change */
do {
printf("Choose the line you want to change\n");
printf("[Line] > ");
scanf("%d", &line);
if ((line > lineCount) || (line < 1))
printf("That line does not exist\n\n");
} while ((line > lineCount) || (line < 1));
/* This will ask for the file name, seconds and time in HH:MM */
printf("Insert the name of the file\n");
printf("[Name] ");
scanf("%49s", orderNameFile);
printf("Insert the presentation time in seconds\n");
printf("[Time in seconds] ");
scanf("%d", &orderSecondsFile);
printf("Insert the hour it starts\n");
printf("[Time in HH:MM (Hour:Minutes)] ");
scanf("%49s", orderTimeFile);
/* This is what is supposed to edit the line I want but does not work */
count = 0;
while (++count < lineCount + 1)
{
if (count == line)
fprintf(orderFile, "%s;%d;%s\n", orderNameFile, orderSecondsFile, orderTimeFile);
else
fprintf(orderFile, "%s\n", fileLines[count - 1]);
}
fclose(orderFile);
return 0;
}
写入临时文件然后删除原始文件并将临时文件重命名为原始文件的名称通常效果更好
FILE *temp = fopen ( "temp", "w");
/* This is what is supposed to edit the line I want but does not work */
while(fscanf(orderFile," %49[^;];%d; %49[^\n]",fileName,&seconds,timeValue) == 3)
{
count++;
if(count == line)
{
fprintf(temp,"%s;%d;%s\n",orderNameFile,orderSecondsFile,orderTimeFile);
}
else
{
fprintf(temp,"%s;%d;%s\n",filename,seconds,timeValue);
}
}
fclose ( temp);
fclose ( orderFile);
remove ( "order.txt");// comment these two lines out until you are sure temp is correct
rename ( "temp", "order.txt");//that way you do not loose the original file
删除文件内容是一项代价高昂的操作,处理此问题的更好方法是
- 以写入模式打开一个新文件
- 开始将您之前文件的内容写入此新文件。
- 一旦到达应替换的行,请确保忽略读取的行并使用
fprintf()
将新行写入新文件
IMO 这是执行此操作的更简单和正确的方法。使用以下线程作为参考
Delete a Line from a file in C Language
所以,我有这个:
order.txt
file.txt;10;21:21
file2.txt;3;20:00
file2.txt;30;22:20
file4.txt;3;10:00
我希望用户给出所需的行(1 到 3,取决于存在的行数)然后更改其内容。例如:
第 3 行
"文件名: file.txt"
"秒: 5"
"时间 HH:MM: 20:00"
Output/Changes变成order.txt:
file.txt;10;21:21
file2.txt;3;20:20
file.txt;5;20:00
file4.txt;3;10:00
如您所见,第三行发生了变化。为此,我尝试了以下方法:
变量
FILE* orderFile;
FILE* contentFile;
char fileName[50];
char timeValue[10];
int seconds;
char orderNameFile[50];
char orderTimeFile[50];
int orderSecondsFile;
int count = 0;
int i = 0;
int line;
int position;
代码
/* This will show the content on it with the number of the line behind */
orderFile = fopen("order.txt","r+");
printf("\n");
while(fscanf(orderFile," %49[^;];%d; %49[^\n]",fileName,&seconds,timeValue) == 3)
{
i++;
printf("[%d] %s;%d;%s\n",i,fileName,seconds,timeValue);
}
printf("\n");
rewind(orderFile);
/* ******************************************************************* */
/* This will ask which line he wants to change */
do{
printf("Choose the line you want to change\n");
printf("[Line] ");
scanf("%d",&line);
printf("\n");
if(line > i)
{
printf("That line does not exist\n\n");
}
}while(line > i || line < 1);
/* ******************************************* */
/* This will ask for the file name, seconds and time in HH:MM */
printf("Insert the name of the file\n");
printf("[Name] ");
getchar();
scanf("%s",&orderNameFile);
printf("Insert the presentation time in seconds\n");
printf("[Time in seconds] ");
scanf("%d",&orderSecondsFile);
printf("Insert the hour it starts\n");
printf("[Time in HH:MM (Hour:Minutes)] ");
getchar();
scanf("%s%",&orderTimeFile);
/* ******************************************* */
/* This is what is supposed to edit the line I want but does not work */
while(fscanf(orderFile," %49[^;];%d; %49[^\n]",fileName,&seconds,timeValue) == 3)
{
count++;
if(count == line)
{
position = ftell(orderFile);
fseek(orderFile, position, SEEK_SET);
fprintf(orderFile,"%s;%d;%s",orderNameFile,orderSecondsFile,orderTimeFile);
break;
}
}
*/ ****************************************************************** */
fclose(orderFile);
我想要使用第一个示例的输出:
file.txt;10;21:21
file2.txt;3;20:20
file.txt;5;20:00
file4.txt;3;10:00
使用第一个示例@EDITED 给我的输出:
file.txt;10;21:21
file2.txt;3;20:00
file2.txt;30;22:20file.txt;5;20:00
file4.txt;3;10:00
有什么想法吗?
你必须存储这些行然后将它们重写到文件中,可能会有更多elegant/efficient解决方案,但对于一个简单的情况,这应该没问题,这是代码
#include <stdio.h>
#include <string.h>
int main()
{
FILE* orderFile;
char fileName[50];
char timeValue[10];
int seconds;
char orderNameFile[50];
char orderTimeFile[50];
int orderSecondsFile;
int count;
int lineCount;
int line;
char lineContent[128];
char fileLines[128][128];
/* This will show the content on it with the number of the line behind */
orderFile = fopen("order.txt", "r+");
if (orderFile == NULL)
return -1;
lineCount = 0;
while ((fgets(lineContent, sizeof(lineContent), orderFile) != NULL) && (lineCount < 128))
{
size_t length;
length = strlen(lineContent);
if (lineContent[length - 1] == '\n')
lineContent[length - 1] = 0;
if (sscanf(lineContent, " %49[^;];%d; %49[^\n]", fileName, &seconds, timeValue) == 3)
{
strcpy(fileLines[lineCount], lineContent);
printf("[%d] %s;%d;%s\n", ++lineCount, fileName, seconds, timeValue);
}
}
printf("\n");
rewind(orderFile);
/* This will ask which line he wants to change */
do {
printf("Choose the line you want to change\n");
printf("[Line] > ");
scanf("%d", &line);
if ((line > lineCount) || (line < 1))
printf("That line does not exist\n\n");
} while ((line > lineCount) || (line < 1));
/* This will ask for the file name, seconds and time in HH:MM */
printf("Insert the name of the file\n");
printf("[Name] ");
scanf("%49s", orderNameFile);
printf("Insert the presentation time in seconds\n");
printf("[Time in seconds] ");
scanf("%d", &orderSecondsFile);
printf("Insert the hour it starts\n");
printf("[Time in HH:MM (Hour:Minutes)] ");
scanf("%49s", orderTimeFile);
/* This is what is supposed to edit the line I want but does not work */
count = 0;
while (++count < lineCount + 1)
{
if (count == line)
fprintf(orderFile, "%s;%d;%s\n", orderNameFile, orderSecondsFile, orderTimeFile);
else
fprintf(orderFile, "%s\n", fileLines[count - 1]);
}
fclose(orderFile);
return 0;
}
写入临时文件然后删除原始文件并将临时文件重命名为原始文件的名称通常效果更好
FILE *temp = fopen ( "temp", "w");
/* This is what is supposed to edit the line I want but does not work */
while(fscanf(orderFile," %49[^;];%d; %49[^\n]",fileName,&seconds,timeValue) == 3)
{
count++;
if(count == line)
{
fprintf(temp,"%s;%d;%s\n",orderNameFile,orderSecondsFile,orderTimeFile);
}
else
{
fprintf(temp,"%s;%d;%s\n",filename,seconds,timeValue);
}
}
fclose ( temp);
fclose ( orderFile);
remove ( "order.txt");// comment these two lines out until you are sure temp is correct
rename ( "temp", "order.txt");//that way you do not loose the original file
删除文件内容是一项代价高昂的操作,处理此问题的更好方法是
- 以写入模式打开一个新文件
- 开始将您之前文件的内容写入此新文件。
- 一旦到达应替换的行,请确保忽略读取的行并使用
fprintf()
将新行写入新文件
IMO 这是执行此操作的更简单和正确的方法。使用以下线程作为参考
Delete a Line from a file in C Language