Query reg file handeling..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shariquehabib
    New Member
    • Oct 2006
    • 22

    Query reg file handeling..

    Hi,

    I am using C language to read a file: row.txt
    Can any one plz help me to get number of rows present in a file BEFORE reading a file.
    So i can check number of rows and if the rows greater than the array size, i can throw an error.

    I just wanted to check row numbers before fscanf function so we can come to know that how many rows are in row.txt file.

    Please find my code:

    #include <stdio.h>

    typedef struct
    {
    int a;
    int b;
    }shane;

    int main()
    {

    FILE *fp;
    int row=0;
    int i=0;
    shane array[10];

    fp=fopen("row.t xt", "r");

    if (fp ==NULL)
    {

    printf(" File: row.txt not found...\n");

    }

    printf("Rows are = [%d]\n",row);

    while (fscanf(fp, "%d %d\n",&array[i].a,&array[i].b) != EOF)
    {
    i=i+1;
    printf(" Row No:[%d] and Numbers are: [%d] [%d]\n",row,array[i].a,array[i].b);
    }


    printf("Rows are: [%d]\n", row);

    }


    PLease help me..:)

    Thanks,
    Sharique...
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Originally posted by shariquehabib
    I am using C language to read a file: row.txt
    Can any one plz help me to get number of rows present in a file BEFORE reading a file.
    So i can check number of rows and if the rows greater than the array size, i can throw an error.
    I just wanted to check row numbers before fscanf function so we can come to know that how many rows are in row.txt file.
    It all depends on the format of row.txt. The data format must include some sort of row delimiter. A simple approach would be to scan the file and count those delimiters. A more complex approach would involve verifying that there is no malformed or otherwise invalid data in the file.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Why bother. Use a linked list and just keep adding nodes to the linked list as you read records from the disc file.

      Comment

      Working...