0

I would like for my program to capture a set number of data from a file ( the last 10 records to be exact ) so I have a Text file storing the following.

`File Created: Sun Mar 30 20:44:05 2014

1000 Deposit:100.00 Sun Mar 30 20:45:02 2014`

This is the following code I currently have:

typedef struct transaction
{
    int acc_Id;
    char *time;
    char *type;
    float amount;
}my_Trans;

int Dtl(Acc *user)// the user parameter is not used in the code as yet
{
    my_Trans dtl_log[10];// to store the 10 information of my trans 
    FILE *dtl_fp;
    int dtl_Cnd=1,x=0;

    dtl_fp = Opnfile(acclg,r,dnt_ret_fp);// all this does is return my file for reading

    while(dtl_Cnd)
    {
        fseek(dtl_fp,(sizeof(my_Trans))*(-1),SEEK_END);// I set my file cur to the end and start seeking trans size record;
        fscanf(dtl_fp,"%d%s%.2f%s",&dtl_log[x].acc_Id,&dtl_log[x].type,&dtl_log[x].amount,&dtl_log[x].time);// read my trans records and store in my log ready to be displayed the x is just for test purposes to make sure i am getting the correct information out first 
        printf("%d",dtl_log[x].acc_Id);//check my information if it is the right thing
        break;
    }

}
user2861799
  • 115
  • 1
  • 2
  • 7
  • Err... `fscanf` have nothing to do with `sizeof`, and you scanning into uninitialised pointers (even if they were initialised - still there is no memory allocated for strings). – keltar Mar 31 '14 at 04:53
  • @keltar i understand the pointer problem but fscanf have nothing to do with size of ? – user2861799 Mar 31 '14 at 05:05
  • `sizeof` returns size of your structure in memory. It is known compile-time constant. `fscanf` works with text, which is not matches memory representation. If you intend to read `sizeof` bytes, - it would be binary file, not text. – keltar Mar 31 '14 at 05:13
  • @KELTAR i just thought i could use it to set the file at the end of my file so i can start searching backwards – user2861799 Mar 31 '14 at 05:22
  • No, it's not the case. If you need to read last line of file, better do something like http://stackoverflow.com/questions/13790662/c-read-only-last-line-of-a-file-no-loops – keltar Mar 31 '14 at 05:52

0 Answers0