Thread: To open or not to open ?

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    93

    To open or not to open ?

    Hi,

    I am sorry to labour this point, but I cannot understand why I cannot get a count of lines in a text file

    Code:
    void check_activity(FILE ** p_fp)
    {
    int diff = 0;
    int result = 0;
    int countA = 0, countB = 0;
    int ch;
    
        printf("before this month, last month, difference percentage %d %d %d %d\n", countA, countB, diff, result);
    
    if(!(p_fp[OUT_FILE]))
       printf("OUT_FILE is not open !!");
    
    
    /*
       rewind(p_fp[OUT_FILE]);
    */
    
       fseek(p_fp[OUT_FILE], 0, SEEK_SET);
       while((ch = fgetc(p_fp[OUT_FILE])) != EOF)
          if(ch == '\n')
             countA++;
    
    printf("first count of OUT_FILE [%d]\n", countA);
    
       while((ch = fgetc(p_fp[LAST_FILE])) != EOF)
          if(ch == '\n')
             countB++;
    
    
       /* establish which is the largest file */
       if(countA > countB)
       {
          diff = countA - countB;
       }
       else
       {
          diff = countB - countA;
       }
    
       /* get the percentage to test */
       result = countA / 10;
    
       if(diff > result)
          cirp_process(p_fp);
    
        printf("after this month, last month, difference percentage %d %d %d %d\n", countA, countB, diff, result);
    }
    /* test run */
    $ cirp_process september.SND august.SND
    before this month, last month, difference percentage 0 0 0 0
    first count of OUT_FILE [0]
    YES
    after this month, last month, difference percentage 0 10 10 0
    $

    I have swapped the order of the files as arguments, I have swapped the order of calling the parameter definitions, I have swapped the cut-n-paste while routines round the other way, I have tried rewind and fseek and I have tested to see that the file really is open.

    I have also made a small function with one routine and sent the two files to it just to see, and still get the same result.

    Code:
    int get_count(FILE ** fp, int text_file)
    {
    int ch, count = 0;
    
       while((ch = fgetc(fp[text_file])) != EOF)
          if(ch == '\n')
             count++;
    
       return count;
    }
    /* test run */
    $ cirp_process september.SND august.SND
    before this month, last month, difference percentage 0 0 0 0
    count of OUT_FILE [0]
    count of LAST_FILE [10]
    after this month, last month, difference percentage 0 10 0 0
    $

    However, what I do not know is this...

    If I start a process and open some files, say two files, one already exists and the other doesn't, and I read the one that does, and write to the one that doesn't, do I have to close that new file and then re-open it before I can work on it ?

    If that is the case, it is going to make the program a bit messy, as at present all files are opened at the start and closed at the end, and I would have to do a separate process just for this file.


    tia,

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    That depends on the os your programming in.
    Windows uses "\r\n"
    Unix uses "\r"
    Mac uses "\n"

    Therefore if your testing for the wrong control sequence in the wrong invironment you will never know which file is bigger.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by velius
    That depends on the os your programming in.
    Windows uses "\r\n"
    Unix uses "\r"
    Mac uses "\n"

    Therefore if your testing for the wrong control sequence in the wrong invironment you will never know which file is bigger.
    If you open the file in text mode ("r"), the application will only ever see \n regardless of the OS (providing the file being read was created on the same OS as it's being read on).

    As this program is a line counter, this situation is fine, if it were a byte counter, there'd be a problem.

    >>do I have to close that new file and then re-open it before I can work on it ?<<
    No, just make sure you open it as read/write, do fflush()'ing after writing/before reading, and don't forget to rewind() where appropriate.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    my book says unix used \n for end-of-line. Not \r.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by chrismiceli
    my book says unix used \n for end-of-line. Not \r.
    Correct, Unix uses LF only (\n).
    MAC, I believe, uses CR only (\r)
    Windows uses CRLF
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Excuse me for being human. If I'm wrong, I'm wrong. It happens.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    93
    and I need to make an even bigger apology

    first, thanks to everyone who has posted and helped me.

    The reason I am not counting any records in this particular file, is because of the way I am opening it

    Code:
       /* Output file */
       strcpy(out_file, inp_file);
       strcpy(out_file + (strchr(out_file, '.') - out_file), OUT_SUFFIX);
       if(!(fp[OUT_FILE] = fopen(out_file, "w")))
          error_func("In file_open().", "Error : fopen(OUTPUT_FILE)", SYS | FATAL);
    as you can see, I am opening it in write mode - ooops

  8. #8
    root
    Join Date
    Sep 2003
    Posts
    232
    >If I'm wrong, I'm wrong.
    If you're wrong, you'll be corrected. Don't take it personally bud, the best way to learn new things is to make mistakes.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open Source Licenses
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 10-10-2006, 08:53 PM
  2. Big help in Astar search code...
    By alvifarooq in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2004, 11:38 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Problems with open and save boxes
    By pinkcheese in forum Windows Programming
    Replies: 3
    Last Post: 05-21-2002, 06:03 PM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM