在文件中增加数字的同时读/写文件的C程序
我正在从文件中读取时间,然后尝试将读取时间转换为秒。有没有更简单的方法来转换时间戳?我的方法似乎效率不高。在写入文件时,您认为最好的方法是什么 样本文件在文件中增加数字的同时读/写文件的C程序,c,file-io,C,File Io,我正在从文件中读取时间,然后尝试将读取时间转换为秒。有没有更简单的方法来转换时间戳?我的方法似乎效率不高。在写入文件时,您认为最好的方法是什么 样本文件 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 代码: #包括 #定义MAXC 1024//定义常量,不要在代码中使用幻数 #定义最大值40 int main(int
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
代码:
#包括
#定义MAXC 1024//定义常量,不要在代码中使用幻数
#定义最大值40
int main(int argc,字符**argv){
char buf[MAXC]=“”;//保存每行的缓冲区--大小按要求
字符文件名[MAXN];
内线=1;
int替换_线;
文件*fp,*fp2;
printf(“请输入文件名:”);
scanf(“%s”和文件名);
fp=fopen(文件名为“r+”);
if(!fp)//验证文件是否打开以进行读取
{
fprintf(stderr,“错误:文件打开失败“%s”。\n”,argv[1]);
返回1;
}
//printf(“输入时间增量:”);
//扫描频率(“%d”,增量(&d);
while(fgets(buf,sizeof buf,fp))//读取文件中的每一行
{
char et[MAXC]=“”;//用于等待时间的缓冲区
字符etR[MAXC]=“”;
字符时间[7]=“”;
int filler=0;
if(line==1)//如果是第一行,只需打印
{
printf(“%d:%s”,第行,buf);//注意:\n包含在fgets中
//fprintf(fp2,“%s”,buf);
}//如果第一行结束
其他的
{
if(sscanf(buf,“%s”,et)!=1)//最多解析第一个空格
{
fprintf(stderr,“错误:无效转换,行%d\n”,行);
返回1;
}
printf(“%d:%s\n”,第行,et);//仅输出运行时间
while(填料<7)
{
时间[填料]=et[填料];
filler++;
}
时间[1]=时间[1]-“0”;//前导分钟(超过6时出错)
时间[2]=时间[2]-“0”;//分钟
时间[4]=时间[4]-“0”;//领先秒(如果超过6,则增加到分钟)
时间[5]=时间[5]-“0”;//秒(如果超过9,则添加到LS)
时间[7]=时间[7]-“0”;//秒的分数(如果超过9,则添加到S)
浮动时间间隔;
字符分钟持有者[2];
sprintf(分钟持有者,“%d%d”,时间[1],时间[2]);
整数分钟;
分钟=(分钟持有者[0]*10)+分钟持有者[1];
printf(“\n%d\n”,分钟);
//每行通话539分钟???
}//其他的结束
line++;//递增行数
}//解析文件时结束
倒带(fp);
if(fp!=stdin)//如果不是stdin,则关闭文件
{
fclose(fp);
}
返回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;
#包括
#定义MAXC 1024//定义常量,不要在代码中使用幻数
#定义最大值40
int main(int argc,字符**argv){
char buf[MAXC]=“”;//保存每行的缓冲区--大小按要求
字符文件名[MAXN];
内线=1;
int替换_线;
//文件*fp=argc>1?fopen(argv[1],“r”):stdin;
//文件*fp=fopen(“test.txt”、“r”);
文件*fp,*fp2;
printf(“请输入文件名:”);
scanf(“%s”和文件名);
fp=fopen(文件名为“r+”);
//fp2=fopen(“datadump.txt”,“w”);
if(!fp)//验证文件是否打开以进行读取
{
fprintf(stderr,“错误:文件打开失败“%s”。\n”,argv[1]);
返回1;
}
//printf(“输入时间增量:”);
//扫描频率(“%d”,增量(&d);
while(fgets(buf,sizeof buf,fp))//读取文件中的每一行
{
char et[MAXC]=“”;//用于等待时间的缓冲区
字符etR[MAXC]=“”;
字符时间[7]=“”;
int filler=0;
if(line==1)//如果是第一行,只需打印
{
printf(“%d:%s”,第行,buf);//注意:\n包含在fgets中
//fprintf(fp2,“%s”,buf);
}//如果第一行结束
其他的
{
char*pblack=strchr(buf',);//获取chr''位置
pblack[0]=0;//将“”设置为“\0”,获取新字符串
printf(“%d:%s\n”,行,buf);
}//其他的结束
line++;//递增行数
}//解析文件时结束
倒带(fp);
//fp2=fopen(“副本c”、“w”);
//替换代码
/*
flcose(fp);
fclose(fp2);
删除(文件名(;
重命名(“replica.c”,文件名);
//如果您希望向他们显示新文件,请使用stdin并在
//fp=fopen(文件名,“r”);
//while循环
*/
if(fp!=stdin)//如果不是stdin,则关闭文件
{
fclose(fp);
//fclose(fp2);
}
返回0;
}如果ti
#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;
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXC 1024 //input buffer size
#define MAXLINE 1024 //max number of line
int main (int argc, char **argv) {
char filename[FILENAME_MAX+1+1];//+1: newline, +1: NUL
char buf[MAXC] = "";
char time_field[7+1];//mm:ss.s, +1: NUL
unsigned char ch;
long pos[MAXLINE];//save file positions
int line = 0; //start with 0 in program
int replace_line; //input replace line number
int increment; //input incremant second.
FILE *fp;
printf("Please enter a file name: ");
fgets(filename, sizeof filename, stdin);
filename[strcspn(filename, "\n")] = 0;//chomp newline
fp = fopen(filename, "r+");
if (!fp){
fprintf (stderr, "error: file open failed '%s'.\n", filename);
return 1;
}
//display file & store file position each line
pos[line] = ftell(fp);
while (fgets (buf, sizeof buf, fp)){
if(++line == MAXLINE){
fprintf (stderr, "error: file too long.\n");//Need change the program. expand array or use malloc and realloc
return 2;
}
pos[line] = ftell(fp);
if (!isdigit(ch = *buf)){//If it does not start with a numerical value Output as it is
printf("%d : %s", line, buf); //note: \n included by fgets
} else {
if(sscanf(buf, "%7s%c", time_field, &ch)==2 && isspace(ch)){
printf ("%d : %s\n", line, time_field);
} else {
fprintf (stderr, "error: invalid time format '%s'", buf);
return 3;
}
}
}
printf("Enter a number of edit line: ");
scanf("%d", &replace_line);
printf("Enter a increment of time: ");
scanf("%d", &increment);//sec
if(fseek(fp, pos[replace_line-1], SEEK_SET)==0){//-1: 0 start in program, 1 start in input
int m, s, ds, seconds;
fscanf(fp, "%s", time_field);
if(sscanf(time_field, "%d:%d.%d", &m, &s, &ds)!=3){
fprintf (stderr, "error: invalid time format '%s' at %d line.\n", time_field, replace_line);
return 3;
}
seconds = m * 60 + s + increment;
m = seconds / 60;
s = seconds % 60;
if(m > 99 || seconds < 0){
fprintf (stderr, "error: Can't change time format.\n");//It is necessary to rewrite the whole file.
return 4;
}
fseek(fp, pos[replace_line-1], SEEK_SET);
fprintf(fp, "%02d:%02d", m, s);fflush(fp);
} else {
fprintf (stderr, "error: file seek failed to '%d' line.\n", replace_line);
return 5;
}
fclose(fp);
return 0;
}