gpt4 book ai didi

c - 更新txt文件内容

转载 作者:行者123 更新时间:2023-11-30 19:44:38 24 4
gpt4 key购买 nike

这样我就可以完成(不完全正确,但对我来说很好)一个名为 order.txt 的文件的内容列表

该文件具有以下内容

假设我们当前的时间是14:59

order.txt

file1.txt;5;15:00

file2.txt;6;15:01

file3.txt;7;15:02

file1.txt;2;15:03

file1.txt;5;15:05

file2.txt;2;15:06

file1.txt;3;15:07

所以基本上,“文件名”;“列出 X 秒”;“从 ##:## 开始列出”

该文件将打开,然后按照上面给出的顺序列出所有内容,直到完成为止。

但是,现在我需要在使用时保存每个文件,包括它的总列表数、列出的总时间,以及是否有列出时间(基本上,如果它在 order.txt 中)。 txt 文件)

例如,它应该是这样的:

used.txt

file1.txt;4;15;1 (1 - Is in the order.txt, 0 - Its not, I can do this one after this is done, it will change when I remove it's time)

file2.txt;2;8;1

file3.txt;1;7;1

基本上,“fileName”;“totalTimeItAppears”;“totalSecondsItShows”;“IfItIsInOrder.txt”

并且,如果用户决定再次显示 order.txt 并且它仍然保持不变,当然,它需要再次更新并且将具有新值(在这种情况下将是上述值的两倍)

used.txt

file1.txt;8;30;1

file2.txt;4;16;1

file3.txt;2;14;1

我已经尝试过执行我的代码,但它只在第一次起作用,并且可能只有在有两次相同的值时才起作用,在这种情况下,file1.txt 显示了 4 次,因此它可能不起作用,所以我删除了代码。我尝试过考虑不同的方法,但无法找到正确更新它的想法。

这是我显示 order.txt 的代码:

        orderFile = fopen("order.txt","r");

if(orderFile != NULL)
{
fseek(orderFile, 0, SEEK_END);
size = ftell(orderFile);
rewind(orderFile);

if(size == 0)
{
printf("\n< No files to list >\n\n");
fclose(orderFile);
}
else
{
while(fscanf(orderFile," %49[^;];%d; %49[^\n]\n",fileName,&seconds,timeValue) == 3)
{
contentFile = fopen(fileName,"r");

if(contentFile != NULL)
{
sscanf(timeValue,"%d:%d",&hour,&min);
fseek(contentFile, 0, SEEK_END);
size = ftell(contentFile);
rewind(contentFile);

if(size == 0)
{
printf("\n< Scanned File has nothing to list >\n\n");
fclose(contentFile);
}
else
{
time_t now;
time(&now);

struct tm file_time_tm;
file_time_tm = *localtime(&now);
file_time_tm.tm_hour = hour;
file_time_tm.tm_min = min;
file_time_tm.tm_sec = 0;
file_time_tm.tm_isdst = -1;
time_t fileTime = mktime(&file_time_tm);

diff_time = difftime(fileTime, now);

if(diff_time > 0)
{
defaultFile = fopen("default.txt","r");
while(fscanf(defaultFile,"%[^\t]",defaultContent) == 1)
{
printf("%s\n", defaultContent);
}
fclose(defaultFile);
sleep(diff_time);

while(fscanf(contentFile,"%[^\t]",textContent) == 1)
{
printf("%s\n", textContent);
}
fclose(contentFile);
sleep(seconds);
}
else
{
if(diff_time < 0)
{
diff_time2 = diff_time + 86400;

defaultFile = fopen("default.txt","r");
while(fscanf(defaultFile,"%[^\t]",defaultContent) == 1)
{
printf("%s\n", defaultContent);
}
fclose(defaultFile);
sleep(diff_time2);

while(fscanf(contentFile,"%[^\t]",textContent) == 1)
{
printf("%s\n", textContent);
}
fclose(contentFile);
sleep(seconds);
}
else
{
if(diff_time == 0)
{
while(fscanf(contentFile,"%[^\t]",textContent) == 1)
{
printf("%s\n", textContent);
}
fclose(contentFile);
sleep(seconds);
}
}
}
}
}
else
{
sscanf(timeValue,"%d:%d",&hour,&min);
printf("\n< File does not exist >\n\n");
fclose(contentFile);
}
}

fclose(orderFile);
}
}
else
{
printf("\n< File does not exists, please create it again >\n\n");
fclose(orderFile);
}

printf("\n");

我所知道的是,在 sleep (秒)之后,我应该将其添加到used.txt中,但我不知道如何仅添加一次,如果还有其他更新该行。我虽然想使用“r+”,所以它会写在该行的顶部,但在我的程序中,我可以重命名文件,所以如果 file1.txt 将重命名为 file.txt,一个字符仍然会在那里,并且在结尾将是 ;11(应该是 1 或 0)。

知道如何在每次文件运行时正确更新的同时添加它吗?

<小时/>

到目前为止我已经尝试过:

tempFile = fopen("temp.txt","w");
utilizadosFicheiro = fopen("utilizados.txt","r");

while(fscanf(utilizadosFicheiro," %49[^;];%d;%d;%d",ficheiroNome,&nApresentacao,&tempoAcumulado,&escalonamento) == 4)
{
fseek(tempFile, 0, SEEK_END);
size = ftell(tempFile);
rewind(tempFile);

if(size == 0)
{
fprintf(tempFile,"%s;%d;%d;%d\r\n",ficheiroNome,nApresentacao,tempoAcumulado,escalonamento);
}
else
{

while(fscanf(tempFile," %49[^;];%d;%d;%d",ficheiroNome2,&nApresentacao2,&tempoAcumulado2,&escalonamento2) == 4)
{
if(strcmp(ficheiroNome,ficheiroNome2) == 0)
{
// Edit the line with tempFile "r+"
}
else
{
// Keep line with tempFile "r"
fprintf(tempFile,"%s;%d;%d;%d\r\n",ficheiroNome,nApresentacao,tempoAcumulado,escalonamento);
}
}
}
}

ficheiroNome = fileName

nApresentacao = timesListedNumber

tempoAcumulado = totalSecondsListed

escalonamento = "time" (will either be 1 or 0)

最佳答案

用代码回答可能更容易:

#include <stdio.h>
#include <string.h>

struct used_entry_s {
char name[128];
int num_occurances;
int seconds_total;
int in_order_flag;
};

struct order_entry_s {
char name[128];
int seconds;
unsigned long long start;
};

/* first read from used.txt */
int read_entries(struct used_entry_s *entries) {
FILE *f = fopen("used.txt", "r");
struct used_entry_s entry;
int ne = 0;

if(!f) {
return 0;
}

while(fscanf(f, "%[^;];%d;%d;%d\n", entry.name, &entry.num_occurances,
&entry.seconds_total, &entry.in_order_flag) == 4) {
memcpy(&entries[ne], &entry, sizeof(entry));
ne++;
}

fclose(f);

return ne;
}

/* save in-memory used entries to file, overwriting its contents */
void save_entries(const struct used_entry_s *entries, int num_entries) {
int i;
FILE *f = fopen("used.txt", "w");
if(!f) {
return;
}

for(i = 0; i != num_entries; ++i) {
fprintf(f, "%s;%d;%d;%d\n", entries[i].name, entries[i].num_occurances,
entries[i].seconds_total, entries[i].in_order_flag);
}

fclose(f);
}

/* update in-memory entries with given file */
int update_entries(const char *filename, struct used_entry_s *entries,
int num_entries) {
int ne = num_entries;
FILE *f = fopen(filename, "r");
char time_str[128];
struct order_entry_s entry;

if(!f) {
return num_entries;
}

while(fscanf(f, "%[^;];%d;%[^\n]\n", entry.name, &entry.seconds, time_str) == 3) {
/* maybe convert time_str to time_t here, I'm too lazy for that */

int i;
for(i = 0; i != ne; ++i) {
if(strcmp(entries[i].name, entry.name) == 0) {
/* got match, update it */
entries[i].seconds_total += entry.seconds;
entries[i].num_occurances++;
break;
}
}

if(i == ne) {
/* no match found - add new one */

strcpy(entries[ne].name, entry.name);
entries[ne].num_occurances = 1;
entries[ne].seconds_total += entry.seconds;
entries[ne].in_order_flag = 1;

/* increase number of entries */
ne++;
}
}

fclose(f);

return ne;
}


int main(int argc, char **argv) {
/* maximum number of entries in used.txt */
#define MAX_USED_ENTRIES 128
struct used_entry_s used_entries[MAX_USED_ENTRIES];
int num_entries = read_entries(used_entries);

num_entries = update_entries("order.txt", used_entries, num_entries);
num_entries = update_entries("order.txt", used_entries, num_entries);

save_entries(used_entries, num_entries);

return 0;
}

它依赖 MAX_USED_ENTRIES 作为常量,并且从不检查溢出。如果需要的话可以改进,但这不是问题的主题。

关于c - 更新txt文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27766671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com