- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我试图根据一个称为索引的变量写入文件行,该变量基本上是文件的行号。由于某种原因,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, oroffset
shall be a value returned by an earlier successful call to theftell
function …
由于您想要使用计算的偏移
,因此您不能使用文本流;为了使用二进制流,您必须在fopen
模式
中包含b
。请注意,您不一定可以稍后通过将文件作为二进制流打开来处理之前作为文本流编写的文件。
关于c - 我的代码使用 fseek() 写入文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58920210/
我正在运行 Windows 7 - 64 位,最新的 XAMPP 版本具有 32 位 PHP 版本。 关于测试http://php.net/manual/en/function.fseek.php#1
我在代码中遇到一种情况,其中有一个巨大的功能,可以逐行解析记录,验证并写入另一个文件。 如果文件中有错误,它将调用另一个函数来拒绝记录并写入拒绝原因。 由于程序中的内存泄漏,它会随着SIGSEGV崩溃
这不是一个纯粹的编程问题,但是它会影响使用fseek()的程序的性能,因此了解其工作原理非常重要。一点免责声明,以便它不会被关闭。 我想知道在文件中间插入数据的效率如何。假设我有一个具有1MB数据的文
在最后一个 while 循环中,应打印文件中存储的每 5 个字符,但循环无限期地进行并且不会终止。feof() 函数应该在到达 END OF FILE 时返回 1,并且应该退出循环,但循环会无限期地进
代码的作用:我读取一个二进制文件并对其进行排序。我使用频率阵列来做到这一点。更新:它确实执行了循环,但它没有正确地写入数字...... 这就是代码。我想在读取文件后写入文件。我会在已经写好的内容上加上
我想像这样计算文件字符: #include #include int main(){ int d=0; char filename[25]; FILE *file;
我想从 file.txt 中读入缓冲区几行。 目前,我fseek 和fread 循环中的每一行。我知道这些线是什么,因为我保留了它们的索引列表。因此,如果我想读取第 5 行到第 10 行,我就确切地知
我正在文件内容中搜索字符串,并通过排除空格“”将整个内容存储在 buff char[] 中,最后将此 buff char[] 与用户输入字符串进行比较以检查可用性。 但是我无法存储整个文件内容,因为
我写了这段代码,函数 fseek() 似乎有问题。因为它会影响文本文件的读取。它使函数 fscanf() 从文件中读取诸如垃圾和错误数据之类的内容。我的文件只包含 float 。 FILE *fptr
这个问题已经有答案了: fseek on a position beyond EOF does not trigger EOF using feof, how come? (4 个回答) 已关闭 8
底部解决方案 我正在开发一个 IP 转发程序。读取一个 header 字段后,我使用 fseek() 将文件指针指向下一个 IP header 字段的开头。只是,我的当前位置值为 20,偏移量为 40
我使用 fseek 和 fread 函数读取文件的指定 block ,然后将其写入另一个文件。由于某种原因,在目标文件中,我在其中写入的每个 block 之间有大约 20 个字节的重叠。 有人可以帮我
使用以下代码,我尝试从文件中读取 4 个字节: FILE *f uint32_t read_program(int A) { long i; uint32_t strofprog;
我有一个具有以下结构的 .dat 文件: Object name | Area | Length |Form Factor 我正在尝试查找哪个对象具有最小面积。我读取了整个文件并使用 ftell()
我正在制作一个 C++ 安装程序,我已将要提取的文件和要在程序中提取的文件的 8 字节文件大小附加到可执行文件中。我的程序因文件读取错误而退出,出了什么问题?请注意,除了我今天学到的知识外,我对 c
我在使用 fseek 时遇到了问题。我有一个包含获取的 HTTP 数据的文件指针。然后我让 libmagic 确定文件的 mime 类型,然后想倒带: char *mime_type (int fd)
这个问题在测验中弹出: 用正确的数字替换“Z”以将 FILE 指针“fp”设置为文件的最后一个字节: fseek( fp, Z, SEEK_END ); 我的回答是 -1,因为对我来说,位于某个字节意
我知道这个问题听起来很傻,但我不知道什么时候修改我的文件指针。我刚开始学习文件在 C 中是如何工作的。我正在做一个简单的练习,我必须编写一个函数来显示文件的大小并将文件指针作为参数。这是我的功能: i
我对 C 中的 fseek() 用法有疑问。我知道它可以设置由 fopen() 创建的文件描述符的偏移位置。但是它是否也可以设置新建文件的偏移位置呢?更具体地说,我可以: FILE * pFile
我正在编写一个比较文件大小的方法(就像任何比较方法一样)。这是我的方法(我使用 void * 作为参数,因为程序的其他部分需要它): int compareFileSize(void * p1, vo
我是一名优秀的程序员,十分优秀!