Counting records in a file

This is a discussion on Counting records in a file within the C Programming forums, part of the General Programming Boards category; If I have a data file that is structured like this: word1 word2 word3 word4 word5 word6 I am using ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    Question Counting records in a file

    If I have a data file that is structured like this:

    word1 word2
    word3 word4
    word5 word6

    I am using fscanf to read the words into an array of strings. How can I count the number of lines in the file? What I want is the number of word pairs in the file, not the number of words.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Count the number of words and divide by two.
    2) Count he number of newline characters in the file.

    Take your pick.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    4
    I guess my problem then is getting fscanf to detect the end of the file.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I guess my problem then is getting fscanf to detect the end of the file.
    Something along these lines would work:
    Code:
    while (fscanf(file, "%10s %10s", array[a], array[b]) == 2) {
        /* Blah blah blah */
    }
    
    if (ferror(file)) {
        /* Whoops, error instead of EOF */
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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