-3

If I comment fprintf(pf,"1111"); the exe will crash and if I keep it,I get 2/3/2011 (only the first record).If I try to close the file the exe will crash too.

Is fscanf detecting the end of row as end of file and reads null?

I tried also close(*pf),it still crash.

fprintf should not be used in final code,I didn't mean to use it ,but when I used it I observed that the exe reads first line succesfully

 Data* d;
fscanf(pf,"%d",&n);
d=calloc(n,sizeof(Data* ));
for(i=0;i<n;i++){
    if(fscanf(pf,"%d/%d/%d",&(d[i].zi),&(d[i].luna),&(d[i].an))!=3) break;
    printf("%d/%d/%d ",d[i].zi,d[i].luna,d[i].an);
       // fprintf(pf,"1111"); with this I observed that first data can be read

}
  fclose(pf);

input

3
2/3/2011
2/2/2012
2/2/2016
King Tsunamy
  • 47
  • 1
  • 7

3 Answers3

1

Welcome to programming in C.

In C, loops are written like this:

for( i = 0;  i < n;  i++ )

not like this:

for( i = 1;  i <= n;  i++ )
Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
1

Apart from the other errors pointed out in your program, the real reason for crash is: once you use fprintf(), the file pointer points to the end of the file. So essentially you're trying to use fscanf at the end of the file in your second call and hence the crash. To fix this, you can use rewind() after the fprintf call which rewinds the file pointer to previous location.

fscanf(pf,"%d",&n);
for(i=1;i<=n;i++){
    if(fscanf(pf,"%d/%d/%d",&(d[i].zi),&(d[i].luna),&(d[i].an))!=3) break;
    printf("%d/%d/%d ",d[i].zi,d[i].luna,d[i].an);
        fprintf(pf,"1111");
    }
    rewind(fp);
}
  fclose(pf);

EDIT: As pointed out in the comments, this is probably not what you want. Better way to go would be to keep track of bytes read so far and use fseek to get to your desired location after using fprintf.

16tons
  • 675
  • 5
  • 14
  • 2
    You are right in detecting the mistake, but I do not think that your suggestion for a fix will work. The OP will probably need to rethink what he/she is trying to do. Reading from and at the same time writing to the same text file is flawed as a concept. – Mike Nakis Jan 17 '17 at 21:47
  • 1
    Adding rewind() will not work, since the fscanf call will fail due to the fact that "1111" does not match the specifier "%d/%d/&d". As @Mike says, the OP needs to rethink what he is trying to do. – FredK Jan 17 '17 at 21:49
  • rewind need the argument pf and it will refresh the file so the second and third line will never be read – King Tsunamy Jan 17 '17 at 21:49
  • Yes it is flawed idea indeed. But if he *really* wants to do it for some reason, his choices are either `rewind` or `fseek` and keeping track of bytes. – 16tons Jan 17 '17 at 21:50
  • Regardless, reading from and writing to the same file is fraught with potential problems. – FredK Jan 17 '17 at 21:51
  • @FredK OP checks the return value of `fscanf` and breaks out of the loop on failure. – 001 Jan 17 '17 at 21:53
  • I updated the post a bit,with fseek(pf,0,SEEK_CUR); ,it returns -1073741819 (0xC0000005) – King Tsunamy Jan 17 '17 at 22:07
0

fix like this:

#include <stdio.h>
#include <stdlib.h>

typedef struct data {
    int zi, luna, an;
} Data;

int main(void){
    FILE *pf = fopen("data.txt", "r");
    if(pf == NULL){
        perror("fopen");
        return -1;
    }
    Data *d;
    int n;
    fscanf(pf, "%d", &n);
    d = calloc(n, sizeof(Data));//sizeof(Data* ) should be sizeof(Data)
    if(d == NULL){
        perror("malloc");
        return -2;
    }
    for(int i = 0; i < n; i++){//i <= n occurs out ouf bounds, In C 0 origin
        if(fscanf(pf, "%d/%d/%d", &d[i].zi, &d[i].luna,&d[i].an)!=3) break;
        printf("%d/%d/%d ", d[i].zi, d[i].luna, d[i].an);
    }
    fclose(pf);
    free(d);
}

DEMO

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70