Problem with program..

This is a discussion on Problem with program.. within the C Programming forums, part of the General Programming Boards category; Hi All, I have written one small program where i m reading one file till the cursor position 200000 and ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    22

    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
    Last edited by Dave_Sinkula; 09-18-2006 at 09:39 PM. Reason: Added [code][/code] tags -- learn to use them yourself.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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
    Last edited by Adak; 09-18-2006 at 09:45 PM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    22
    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 09:55 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > 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()
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 03:38 PM
  4. Replies: 20
    Last Post: 06-13-2005, 12:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21