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;
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.
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);
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).