01-20-2016 11:30 PM
help me read text file
my text file contains following four lines
//////////////////////////////////////////////////////////////////file start ///////////////////////////////////////
com1=2
com2=8
IP Address= 192.167.0.10
file = "textfile.dat"
/////////////////////////////////////////////////////////////////file end ////////////////////////////////////////
i only want to read ,, 2,, 8 ,, 192.167.0.100 ,, textfile.dat ,,
which are my desired data values. help mw with some sample code
there is no trim function in labwindows which is present in .net so kindly guide me which cvi function will be useful in my case as com port can be 18 or 13 which means not a single character always, where as data in my file such as ip address, com ports and and file name can not always be of same size so i have to read correctly in all cases whether ip address = 192.168.0.8 or ip address = 192.168.244.207 so how do i read it correctly without changing the code in labwindows. guide me in detail, thanks in advance.
Solved! Go to Solution.
01-21-2016 02:15 AM - edited 01-21-2016 02:18 AM
If your file had a section heading (e.g. "[General]") you could read it unsing the .Ini file instrument, which you can find in <CVIInstallFolder>\toolslib\toolbox\inifile.fp. There is also an example program on how to use the instrument.
Since no section name is present, you can only read line by line and parse on the equal sign. A code framework (to be expanded and completed by you) could be the following. It uses CVI libraries, there is also the possibility to obtain the same using ANSI Cstandard functions.
fileHandle = OpenFile ("myfile.txt", VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII); while ((r = ReadLine (fileHandle, buffer, 511)) != -2) { idx = FindPattern (buffer, 0, -1, "=", 0, 0); if (idx > 0) { Scan (buffer, "%s[xt61]%s", parameter, value); } } CloseFile (fileHandle);
Be careful: this is only a conceptual framework that only handles the line checking and splitting. That is: it reads entire file content, it does not discriminates between parameter names and it does not integrate error checking, which is mandatory on file I/O operations.
By looking at the help on Scan function you can understand the meaning of the format code I used (detailed description is here, the same information is present in the help installed on your PC).
01-21-2016 03:39 AM - edited 01-21-2016 03:44 AM
Just rethinking to your question.
If you can modify the structure of your input file, the fastest solution to your problem is to add a section header and use the IniFile instrument to read info from the file.
That is: if your file can be modified this way:
[Configuration] com1=2 com2=8 IP Address= 192.167.0.10 file = "textfile.dat"
then your program would simply be like this:
int ReadConfiguration (void) { int com1, com2, error = 0; char path[MAX_PATHNAME_LEN], msg[512]; char ip[32], file[64]; IniText T = 0; // Pathname of config file GetProjectDir (path); MakePathname (path, "Myfile.ini", path); // Open and read the file nullChk (T = Ini_New (0)); errChk (Ini_ReadFromFile (T, path)); // Read parameters errChk (Ini_GetInt (T, "Configuration", "com1", &com1)); errChk (Ini_GetInt (T, "Configuration", "com2", &com2)); errChk (Ini_GetStringIntoBuffer (T, "Configuration", "File", file, 64)); errChk (Ini_GetStringIntoBuffer (T, "Configuration", "IP address", ip, 32)); Error: if (T) Ini_Dispose (T); if (error < 0) { sprintf (msg, "Error %d found in %s (%s):\n%s", error, __FUNCTION__, __FILE__, GetGeneralErrorString (error)); MessagePopup ("Error", msg); } return error < 0 ? error : 0; }