Results 1 to 4 of 4

Thread: Determine number of columns in a file

  1. #1
    Join Date
    Apr 2009
    Posts
    79

    Determine number of columns in a file

    I want to determine the number of columns in the file with :

    0 0 0 0
    0 1 1 0
    0 1 1 0
    0 0 0 0

    it is a text file,I can determined the number of lines using the function gets () for the number of columns,but unable to determine column in file.

    /
    Code:
     * Read the file to find the number of lines * / 
            while (fgets (string, max_size, file)! = NULL)   
    	  / * Fgets () reads a string until the newline * / 
    	  line + +; / * on the number of newline * / 
        
            printf ( "row =% i", line); 
          
            nl = line; 
          
            / * Read the file to find the number of columns * / 
            rewind (file); 
            while ((c = getc (file))! = atoi ( "n")) 
    	  if (c! = atoi ( "")) + + column;  
            nc = column;

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Determine number of columns in a file

    If you know fgets () Function retrieves a character then Just put this function in a loop, your characters repeated one after the other until the '\ n' End of line and the number of columns also repeated in loop.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: Determine number of columns in a file

    with strlen () it does not work, and I suppose that a space is counted as a character, so it forced me to subtract the number of space ...
    Code:
    / * Read the file to find the number of lines * / 
            while (fgets (string, max_size, file)! = NULL)    
    	  / * Fgets () reads a string until the newline * / 
    	  nl + +; / * on the number of newline * / 
            printf ( "row =% i \ n", nl); 
          
            / * Read the file to find the number of columns * / 
            rewind (file); 
            while ((nc = getc (file))! = '\ n') 
    	  if (nc! = '') nc + +;  
            printf ( "column =% i \ n", nc);

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: Determine number of columns in a file

    You can create a file called column.dat that contains the text
    22 23 24
    25 26
    by entering the following commands
    Code:
    clear
         file = "temp.dat"
         rmfile(file);
         write(file, "22 23 24 ")
         write(file, " 25 26")
         close(file)
    If you then enter
    Code:
     ncols("temp.dat", "int")
    O-Matrix will respond
    { 24, 23 }
    because column.dat has three integers in its first row and two in its second row. If you continue by entering ncols("column.dat", "char")
    O-Matrix will respond
    { 26, 24 }
    because column.dat has five characters in its first row and three in its second row (there is one space between each of the numbers).

Similar Threads

  1. Find the number of text repeated in columns of Excel
    By Jigisha in forum MS Office Support
    Replies: 4
    Last Post: 24-02-2012, 04:35 PM
  2. How to determine the ppm file type?
    By hanni ball in forum Windows Software
    Replies: 2
    Last Post: 05-01-2012, 06:08 PM
  3. Replies: 4
    Last Post: 27-07-2011, 08:13 AM
  4. Determine a whole number in java
    By Remedy in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 04:39 AM
  5. Replies: 1
    Last Post: 28-07-2009, 05:52 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,038,414.83145 seconds with 16 queries