By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,929 Members | 1,830 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,929 IT Pros & Developers. It's quick & easy.

Query reg file handeling..

P: 22
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.txt", "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...
Apr 29 '09 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 2,402
@shariquehabib
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.
Apr 29 '09 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Why bother. Use a linked list and just keep adding nodes to the linked list as you read records from the disc file.
Apr 29 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.