gpt4 book ai didi

C 程序读取/写入文件,同时递增文件中的数字

转载 作者:行者123 更新时间:2023-11-30 19:33:40 25 4
gpt4 key购买 nike

我正在从文件中读取时间,然后尝试将读取时间转换为秒。有没有更简单的方法来转换时间戳?我的方法似乎效率很低。您建议写入文件时最好的方法是什么?

示例文件

My test file
00:19.1 123456
00:35.4 testing whitespace end

Desired Output
1: My test file
2: 00:19.1
3: 00:35.4

代码:

#include <stdio.h>

#define MAXC 1024 // define constants, don't use magic number in code
#define MAXN 40
int main (int argc, char **argv) {

char buf[MAXC] = ""; // buffer to hold each line -- size as reqd
char filename[MAXN];
int line = 1;
int replace_line;
FILE *fp, *fp2;
printf("Please enter a file name: ");
scanf("%s",&filename);
fp = fopen(filename,"r+");
if (!fp) // validate file open for reading
{
fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}
//printf("Enter a increment of time: ");
//scanf("%d", &increment);
while (fgets (buf, sizeof buf, fp)) // read each line in file
{
char et[MAXC] = ""; // buffer for holding time
char etR[MAXC] = "";
char time[7] = "";
int filler = 0;

if (line == 1) // if 1st line, just print
{
printf ("%d : %s", line, buf); // note: \n included by fgets
//fprintf(fp2,"%s",buf);
} // end of if first line
else
{
if (sscanf (buf, "%s", et) != 1) // parse up to first whitespace
{
fprintf (stderr, "error: invalid conversion, line %d\n", line);
return 1;
}
printf ("%d : %s\n", line, et); //output elapsed time only
while(filler < 7)
{
time[filler] = et[filler];
filler++;
}
time[1] = time[1] - '0'; //Leading Minute (error if over 6)
time[2] = time[2] - '0'; //Minute
time[4] = time[4] - '0'; //Leading Second (Add to minute if over 6)
time[5] = time[5] - '0'; //Second (Add to LS if over 9)
time[7] = time[7] - '0'; //fraction of a second (Add to S if over 9)
float timeInSeconds;
char minuteHolder[2];
sprintf(minuteHolder,"%d%d",time[1],time[2]);
int minute;
minute = (minuteHolder[0]*10) + minuteHolder[1];
printf("\n%d\n",minute);
//getting 539 , minutes on every line???

} //end of else
line++; // increment line count
} // end of while parsing file
rewind(fp);
if (fp != stdin) // close file if not stdin
{
fclose (fp);
}
return 0;
}

最佳答案

转换源字符串:“00:19.1 123456”目标字符串: "1.00:19.1"

  • 第一个:我们应该找到黑色字符 pos:
    *char pblack = strchr(buf, ' ');
    现在pblack指向“123456”。
  • 第二:我们将旧字符串设置为新字符串:
    因为字符串以 '\0' 结尾,
    所以我们设置:pblack[0] = '\0';

现在,我们有了代码:

#include <stdio.h>

#define MAXC 1024 // define constants, don't use magic number in code
#define MAXN 40
int main(int argc, char **argv) {

char buf[MAXC] = ""; // buffer to hold each line -- size as reqd
char filename[MAXN];
int line = 1;
int replace_line;
//FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
//FILE *fp = fopen("test.txt","r");
FILE *fp, *fp2;
printf("Please enter a file name: ");
scanf("%s", &filename);
fp = fopen(filename, "r+");
//fp2 = fopen("datadump.txt","w");
if (!fp) // validate file open for reading
{
fprintf(stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}
//printf("Enter a increment of time: ");
//scanf("%d", &increment);
while (fgets(buf, sizeof buf, fp)) // read each line in file
{
char et[MAXC] = ""; // buffer for holding time
char etR[MAXC] = "";
char time[7] = "";
int filler = 0;

if (line == 1) // if 1st line, just print
{
printf("%d : %s", line, buf); // note: \n included by fgets
//fprintf(fp2,"%s",buf);
} // end of if first line
else
{
char *pblack = strchr(buf, ' '); // get chr ' ' pos
pblack[0] = 0; // set ' ' to '\0', get the new string
printf("%d : %s\n", line, buf);
} //end of else
line++; // increment line count
} // end of while parsing file
rewind(fp);
//fp2 = fopen("replica.c","w");
//replacement code
/*
flcose(fp);
fclose(fp2);
remove(filename(;
rename("replica.c",filename);
//if you to show them new file fflush stdin and repeat while
//fp = fopen(filename, "r");
//while loop
*/
if (fp != stdin) // close file if not stdin
{
fclose(fp);
//fclose (fp2);
}
return 0;

}

关于C 程序读取/写入文件,同时递增文件中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45124371/

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