gpt4 book ai didi

c - 我的代码使用 fseek() 写入文件时出错

转载 作者:行者123 更新时间:2023-11-30 16:10:15 26 4
gpt4 key购买 nike

所以我试图根据一个称为索引的变量写入文件行,该变量基本上是文件的行号。由于某种原因,fseek 函数不起作用。它适用于索引 = 0,但不适用于索引 = 1 等。变量 extra 没有被放入文件的正确位置。我已将 index*13 放入 fseek 中,因为未完成的文件中每个零后面有 11 个空格。我什至尝试过index*12。我的代码的贷款功能完成了这部分工作。请忽略我评论的内容。

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
struct account{
char name[20];
char accnum[9];
int balance;
long outstanding;
int flag;
char pin[5];
};
int difference(int d2,int m2,int y2){
time_t t = time(NULL);
struct tm tm = *localtime(&t);
struct tm d = { 0 }, dd = { 0 };
dd.tm_year = tm.tm_year ;
dd.tm_mon = tm.tm_mon+1;
dd.tm_mday = tm.tm_mday;
d.tm_year = y2 - 1900;
d.tm_mon = m2;
d.tm_mday = d2;
return(difftime(mktime(&dd), mktime(&d)))/(24*3600);
}
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
void takeloan(FILE *atm,FILE *outstanding,struct account holders[],int index,int extra){
FILE *loandates;
char daystr[2],monthstr[2],yearstr[4],ch;
long day,month,year;
int i,count=0,dashcount=0,entercount=0,count1=0,j,k;

loandates=fopen("loandates.txt","r+");
for(j=0;j<15;j++){
for(k=0;k<2;k++){
ch=fgetc(loandates);
daystr[k]=ch;
}
for(k=0;k<2;k++){
ch=fgetc(loandates);
monthstr[k]=ch;
}
for(k=0;k<4;k++){
ch=fgetc(loandates);
yearstr[k]=ch;
}
if(j==index){
break;
}
fseek(loandates,(j+1)*9,SEEK_SET);
}

// puts(daystr);
// puts(monthstr);
// puts(yearstr);
char *ptr;
char temp[13];
time_t t = time(NULL);
struct tm tm = *localtime(&t);
day = strtol(daystr, &ptr, 10);
month = strtol(monthstr, &ptr, 10);
year = strtol(yearstr, &ptr, 10);
// printf("%d", day);
// printf("%d", month);
// printf("%d", year);
if(holders[index].flag==1){
float y=difference(day,month,year);
if(y<=20){
printf("Please pay back the loan you have already taken.\n");
}
else{
printf("Your account is banned as you have crossed the deadline for paying back the loan. Please contact the bank for further details. ");
}
}
else{
atm=fopen("atm.txt","r+");
for(i=0;i<1000;i++){
ch=fgetc(atm);
count++;
if(ch=='-'){
dashcount++;
}
if(dashcount==(index+1)*2){
break;
}
}
fseek(atm,count+(dashcount/2)-1,SEEK_SET);
fprintf(atm,"1");

holders[index].outstanding=extra;
///////////////////////////////////////////////////////////
outstanding=fopen("outstanding.txt","r+");
fseek(outstanding,index*13,SEEK_SET);
fprintf(outstanding,"%d",extra);
fclose(outstanding);
///////////////////////////////////////////////////////////

fseek(loandates,index*9,SEEK_SET);
if(tm.tm_mday>=0&&tm.tm_mday<=9){
fprintf(loandates,"%d ",tm.tm_mday);
}
else{
fprintf(loandates,"%d",tm.tm_mday);
}
if(tm.tm_mon>=0&&tm.tm_mon<=9){
fprintf(loandates,"%d ",tm.tm_mon+1);
}
else{
fprintf(loandates,"%d",tm.tm_mon+1);
}
fprintf(loandates,"%d",tm.tm_year+1900);
}



fclose(loandates);
fclose(atm);



}
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
int main(){
char Cpin[4],Caccnum[8];
char ch,outstandingstr[9000];
int p,test;
char *ptr;
for(p=0;p<9000;p++){
outstandingstr[p]=' ';
}
int i,j;
FILE *atm,*outstanding,*pins,*balance;
atm=fopen("atm.txt","r");
outstanding=fopen("outstanding.txt","r");
pins=fopen("pins.txt","r");
balance=fopen("balance.txt","r");
struct account holders[15];
for(i=0;i<15;i++){
for(j=0;j<30;j++){
ch=fgetc(atm);
holders[i].name[j]=ch;
if(ch=='-'){
break;
}
}
for(j=0;j<20;j++){
if(!((holders[i].name[j]>='A'&&holders[i].name[j]<='Z')||(holders[i].name[j]>='a'&&holders[i].name[j]<='z')||(holders[i].name[j]==' '))){
holders[i].name[j]='\0';
}
}
for(j=0;j<30;j++){
ch=fgetc(atm);
holders[i].accnum[j]=ch;
if(ch=='-'){
break;
}
}
for(j=0;j<9;j++){
if(!(holders[i].accnum[j]>='0'&&holders[i].accnum[j]<='9')){
holders[i].accnum[j]='\0';
}
}
ch=fgetc(atm);
int x = ch - '0';
holders[i].flag=x;
fseek(atm,2,SEEK_CUR);
fscanf(balance,"%d",&holders[i].balance);
for(j=0;j<4;j++){
ch=fgetc(pins);
holders[i].pin[j]=ch;
}
for(j=0;j<5;j++){
if(!(holders[i].pin[j]>='0'&&holders[i].pin[j]<='9')){
holders[i].pin[j]='\0';
}
}

fseek(pins,(i+1)*6,SEEK_SET);

fgets(outstandingstr,13,outstanding);



holders[i].outstanding = strtol(outstandingstr, &ptr, 10);

for(p=0;p<9000;p++){
outstandingstr[p]=' ';
}
// fseek(outstanding,2)

}
fclose(pins);
fclose(outstanding);
fclose(balance);
fclose(atm);
printf("Enter account number: ");
gets(Caccnum);
int index=0;
while(1){
if(strcmp(Caccnum,holders[index].accnum)==0){
break;
}
else{
index++;
}
}
printf("%d",index);
puts(holders[index].pin);
printf("Enter pin: ");
gets(Cpin);
while(strcmp(Cpin,holders[index].pin)!=0){
printf("Wrong pin. Enter Again: ");
gets(Cpin);
}
//puts(holders[index].name);
// puts(holders[index].accnum);
// printf("%d",holders[13].outstanding);
// printf("\n\n");
takeloan(atm,outstanding,holders,index,785);


}

未完成的文件:
0<11个空格>
0<11个空格>
0<11个空格>
0<11个空格>
0<11个空格>
0<11个空格>
0<11个空格>
0<11个空格>
0<11个空格>
0<11个空格>
0<11个空格>
0<11个空格>
0<11个空格>
0<11个空格>
0<11 个空格>

最佳答案

您忽略了 C 标准的fseek 函数部分中的这一段:

For a text stream, either offset shall be zero, or offset shall be a value returned by an earlier successful call to the ftell function …

由于您想要使用计算的偏移,因此您不能使用文本流;为了使用二进制流,您必须在fopen模式中包含b。请注意,您不一定可以稍后通过将文件作为二进制流打开来处理之前作为文本流编写的文件。

关于c - 我的代码使用 fseek() 写入文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58920210/

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