LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

reading character string from text file without spaces

Solved!
Go to solution

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. 

0 Kudos
Message 1 of 3
(3,976 Views)
Solution
Accepted by smartprogrammer

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



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 3
(3,960 Views)
Solution
Accepted by smartprogrammer

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;
}

 

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 3 of 3
(3,950 Views)