08-31-2015 09:26 AM
Hi to all of u i have a text file like that
1.25,3.2,4,12.2
2.36,12,6,8.8.5
1.25,3.2,4,12.2
is there any function which can find number of rows from file and how many characters in file and how many values in file excluding commas.
i used scanfile but didnt succedded in my task.
08-31-2015 10:52 AM
File lenght is returned from several functions: GetFileInfo, FileExists...
No predefined functions for other tasks I'm afraid: simply... count!
1. Number of rows in a file: OpenFile + iterate on ReadLine until end of file
2. Number of fields in a row: having read a file line you can use strtok to loop through it with the appropriate separator counting the number of fields. Look at the examples listed in the help ti understand how to use this function
09-01-2015 05:36 AM
sir Roberto if i read use read line function how can i give the loop limit to exit for example if i have a khadim.txt file in which i have a data
1,2,3
4,5,6
7,8,9
now i will use read line function but how to use for loop so that i can iterate some variable for rows
for example
means if i know that how many times i should run for loop than i will also khow how much rows
sir kindly explain me with example consider the data in khadim.txt in above format for example
i m also trying to use strtok function but i mange to use just when i have a data in buffer with different delimiters and then i able to use it. kindly explain me the task in both ways.
i know that if i use for loop 10 times and also use read line that it will read 10 lines. but the main problem which i want to solve is that i dnt know the number of lines that what will be the end of loop value?
09-01-2015 06:40 AM - edited 09-01-2015 06:49 AM
int x, cnt = 0; OpenFile ();
// Count lines
While (TRUE) { x = ReadLine (); if (x == -1) { // Handle I/O error } if (x == -2) break; // End of file cnt++; // Count a line }
// Roll back to start of file
SetFilePtr ();
// Read lines and handle them for (x = 0; x < cnt; x++) { ReadLine (); // Scan lines
// .... }
This is how I would address the first part of your question.
For what refers to strtok I direct you to the help for the function and the examples linked there (timeaxis.prj seems to me the most simple to study).
09-01-2015 06:58 AM
thanku sir Roberto sir another way u told me about using strtok function how would we use that function to count lines?
09-01-2015 07:43 AM
Using strtok to count lines in file is done the same way as scanning elements in a line provided you use '\n' separator; the problem here is you need to read entire file content in one single big buffer and this can have negative effects depending on file size.
Additionally, I seem to remember you cannot nest two independent strtok (one to scan lines in file and the second to scan field in every line) in a single flow.
09-01-2015 08:22 AM
sir Roberto as in case of read line when we give in argument -1 it read the line till the complete line.
ir redafile we have to give maximum bytes in argument to read number of bytes. is there any argument or way whic reads the whole file in one buffer because we dnt know the number of bytes to read
09-01-2015 10:38 AM
khadimhussain ha scritto:
(...) because we dnt know the number of bytes to read
09-01-2015 11:42 AM
sir roberto which argument we will use in read file to read the whole file. in read line we give -1 to read whole line
09-01-2015 03:01 PM
Have you noted the link in my previous answer? It points you to where I listed some functions that return the file size, which is the value you must pass to ReadFile to fully read it.