-
Problem with program..
Hi All,
I have written one small program where i m reading one file till the cursor position 200000 and writing into anther file.But its reading only first record and came out.i mean while loop is running once and if loop condition is SEEK_CUR reached 200000 in first time.below is my program.
Code:
#include<stdio.h>
#define MAX_DATA_LEN 999999
main()
{
FILE *fp,*fp1;
char lineOfText[400000];
if (( fp = fopen("test.dat", "r")) == NULL )
{
printf("\n Error in opening the Source file ");
exit(1);
}
if ((fp1 = fopen("ED_ALS_LOAN_split.dat","a")) == (FILE *) NULL)
{
printf("\n Error in opening the first splited file");
exit(1);
}
while(fgets(lineOfText, MAX_DATA_LEN, fp ) != NULL)
{
if ( (fseek(fp,200000,SEEK_CUR )) != NULL )
{
printf("file curosr reached at %d position\n",end_pos);
exit(1);
}
else
{
printf("\n lineOfText=%s",lineOfText);
fputs(lineOfText,fp1);
}
printf("\n Inside the while loop");
}
fclose(fp1);
fclose(fp);
}
above i have marked the red, i thnk the loop is having some problem,pls help me to sort out.
THanks
Anwar
-
Instead of using huge stupid numbers, why don't you create a small example? You know, something easy to test, like 10 characters at a time.
Quzah.
-
I'm wondering if the while expression being tested is evalutating to false, after you've moved the fp once?
Maybe it's reached the end of the file?
quzah has made a great suggestion for testing out your loop.
Note: please use code tags.
Adak
-
as i mention the output file is containing 1 record only.But my source file is having 5 records.So, there is no chance the cursor reached at EOF.I have tested with small number also(like 10 char at a time) but in that case also same result.Only reading one record.
I think problem with my loop.
I have one doubt Is it i need to initially set the cursor position SEEK_SET ?
Is it any way to print the cursor position ?
-
> while(fgets(lineOfText, MAX_DATA_LEN, fp ) != NULL)
Mmm
char lineOfText[400000];
#define MAX_DATA_LEN 999999
If you're going to lie about your buffer sizes, then there really isn't any hope.
char lineOfText[BUFSIZ];
fgets( lineOfText, sizeof lineOfText, file );
Is usually good enough, even if you have exceedingly long lines.
> if ((fp1 = fopen("ED_ALS_LOAN_split.dat","a")) == (FILE *) NULL)
Where did you get the dumb idea of casting a NULL pointer?
You didn't need it on the previous test.
> where i m reading one file till the cursor position 200000
fseek(fp,200000,SEEK_CUR ) moves FORWARD 200000.
If you want to find out whether you reached it, use ftell(), not fseek()