所以,我有这个:
将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”
输出/更改为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
关于它可能是什么的任何想法?
答案 0 :(得分:1)
通常可以更好地写入临时文件,然后删除原始文件并将临时文件重命名为原始文件的名称
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
答案 1 :(得分:0)
你必须存储这些行,然后将它们重写到文件中,可能会有更优雅/高效的解决方案,但对于一个简单的情况,这应该没问题,这里是代码
#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;
}
答案 2 :(得分:0)
删除文件内容是一项代价高昂的操作,处理此问题的更好方法是
fprintf()
IMO这是更容易和正确的方法。使用以下线程作为参考