Thread: Determining file size

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41

    Determining file size

    I have a (hopefully) quick question. I have been searching around on the internet and through some books but can't seem to find an answer.

    I am using text files to store matrices where the elements are separated by tabs (on each line). What I want to do is figure out the number of columns and rows in these files. I figure I could run a routine to count the number of lines to the bottom of the file as well as have another counter that increases by 1 after each fscanf until fscanf reaches \n. This seems pretty slow and cumbersome - is there a faster way?

    I am a applied math grad student and am taking a HPC course next semester so I am learning C over break since I don't have much CSCI experience. Any critiques on this code? I know there aren't any things to catch exceptions and check input but other than that stuff what do you guys think? Thanks a lot in advance

    Code:
    #include <stdio.h>
    
    FILE *f1;
    
    int main (int argc, const char * argv[]) {
    	char inputname[50];
    	int matnum1, exit;
    	
    	exit = 0;
    	
    	printf("Input filename (must be less than 50 characters including the extension: ");
    	scanf("%s", inputname);
    	printf("Opening file %s\n",inputname);
    	
    	f1 = fopen(inputname, "r");
    	while (exit != 1) {
    		fscanf(f1, "%d\t",&matnum1);
    		printf("%d\n",matnum1);
    		if (feof(f1)) {
    			exit = 1;
    		}
    	}
    	
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41
    8 views and no comments? Is the question not clear or do you guys not know the answer?

  3. #3
    C/Linux Programmer
    Join Date
    Dec 2009
    Posts
    17

    Try Linux command (awk)

    In case you are working on Linux, and all you need is to "figure out the number of columns and rows in these files", here's a quick command-line solution using awk:
    To get the no. of rows:
    awk 'END { print NR }' YourFileName
    To get the no. of columns:
    awk 'END { print NF }' YourFileName

    Note 1: There will definitely be better solutions!
    Note 2: Assuming all rows have same no. of elements. Column count will depend on the no. of elements in last row.
    Last edited by technam; 12-15-2009 at 04:11 AM. Reason: Formatting commands for readability

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    SourceForge.net: Scanf woes - cpwiki
    Unfortunately, there is no function to get rows or columns directly from a file in C. You would have to read a line and read the number of tabs there is in the line to figure out the number of columns. And read all lines to figure out number of rows.

    A better solution would instead be to keep track of number of rows and columns and write them to the file, say at the first 8 bytes or so.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Use the stat() function to get file info such as it's size...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41
    Quote Originally Posted by Elysia View Post
    SourceForge.net: Scanf woes - cpwiki
    Unfortunately, there is no function to get rows or columns directly from a file in C. You would have to read a line and read the number of tabs there is in the line to figure out the number of columns. And read all lines to figure out number of rows.

    A better solution would instead be to keep track of number of rows and columns and write them to the file, say at the first 8 bytes or so.
    Good idea - that's what I will do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Error with a vector
    By Tropicalia in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2006, 07:45 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM