所以我可以完成(不完全正确,但对我来说很好)来自名为 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
基本上,“fileName”;“X秒列表”;“##开始列出:##”
该文件将打开,然后按照上面给出的顺序列出所有内容,直到它,没关系。
然而,现在我需要在使用时保存每个文件,包含它的总列表数,列出的总时间,以及是否有列表时间(基本上,如果它在订单中)。 txt文件)
例如,它应该是这样的:
used.txt
file1.txt; 4; 15; 1 (1 - 在order.txt中,0 - 它不是,我可以在完成此操作后执行此操作,当我删除它的时候它会改变)< /强>
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");
我所知道的是,在睡眠(秒)之后我应该将它添加到 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”(将为1或0)
答案 0 :(得分: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作为常量,并且永远不会检查溢出。如果需要,这可以改进,但这不是问题的主题。