Thread: Problem with program..

  1. #1
    Registered User
    Join Date
    Feb 2006

    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.

    #define     MAX_DATA_LEN            999999
     FILE *fp,*fp1;
     char lineOfText[400000];
     if (( fp = fopen("test.dat", "r")) == NULL )
       printf("\n Error in opening the Source file ");
     if ((fp1 = fopen("ED_ALS_LOAN_split.dat","a")) == (FILE *) NULL)
      printf("\n Error in opening the first splited file");
     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);
       printf("\n lineOfText=%s",lineOfText);
      printf("\n Inside the while loop");

    above i have marked the red, i thnk the loop is having some problem,pls help me to sort out.

    Last edited by Dave_Sinkula; 09-18-2006 at 08:39 PM. Reason: Added [code][/code] tags -- learn to use them yourself.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    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.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    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.

    Last edited by Adak; 09-18-2006 at 08:45 PM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    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 ?
    Last edited by anwar_pat; 09-18-2006 at 08:55 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > while(fgets(lineOfText, MAX_DATA_LEN, fp ) != NULL)
    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()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. Replies: 20
    Last Post: 06-12-2005, 11:53 PM