Thread: Column count from data file

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    16

    Column count from data file

    Hi all,

    I have a dat file called customer.dat with this content:

    0 1.1 2 -0.3 4 ...
    5 6 7 8 9 ...
    10.1 11 12 13 99 ...
    ...
    ...

    I have no prior idea about number of rows and columns. Below a sample code I have written using fgets. the output gives the number of rows. but I also want to calculate number of columns.

    can anybody give me any idea...




    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main() {
      FILE *dataFile;
      char fileName[100];
      char customer[100];
      int nRow = 0;
    
    
      printf("Enter the name of the file: ");
      fgets(fileName, sizeof(fileName), stdin);
    
      if((dataFile = fopen(fileName, "r")) == NULL) {
    	 printf("Error Opening File.\n");
    	 exit(1);
    	 }
      
      while( fgets(customer, sizeof(customer), dataFile) != NULL ) {
    	 nRow++;
    	 printf("Row %d: %s\n",nRow, customer);
    	 }
    
      fclose(dataFile);
      return 0;
      }

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Looks good to me. Dev-cpp is your IDE? I see the weirdly aligned closing brace...
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Once you've got a line of data, you can then try to sscanf the line in a loop picking out one number each iteration of the loop and incrementing a counter.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    16
    Thanks hk_mp5kpdw. I got my answer. here is the final code. Please tell me if there is any scope of improvements.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main() {
      FILE *dataFile;
      char fileName[100];
      char customer[100];
      int nRow = 0, nCol = 0, n=0;
      char *line;
      char entry[11];
    
      printf("Enter the name of a ascii file: ");
      fgets(fileName, sizeof(fileName), stdin);
    
    
    
      if((dataFile = fopen(fileName, "r")) == NULL) {
    	 printf("Error Opening File.\n");
    	 exit(1);
    	 }
    
      while((line = fgets(customer, sizeof(customer), dataFile)) != NULL ) {
    	 while(sscanf(line, "%10s%n", entry, &n ) == 1 ) {
    		nCol++;
    	        line += n;
    		 }
    
    	 nRow++;
    	 printf("Column Entries: %d Row %d: %s\n",nCol, nRow, customer);
    	 nCol = 0;
    	 }
    
      fclose(dataFile);
      return 0;
     
    }
    BTW I am using Borland IDE.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it is enough to parse one line to determine number of columns

    dataFile = fopen(fileName, "r")
    you need to remove '\n' first from the filename
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    16
    Thanks for your comment. I need to check missed entry in the data file. so i keep counting the columns in each row. Why I need to remove '\n' first from the filename?

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by spiit231 View Post
    Why I need to remove '\n' first from the filename?
    Otherwise fopen will fail
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Editing a data file
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 02-05-2005, 04:21 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM