LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Read csv textfile and use its somedata

i m a student and i want to read csv text file and want to use some of its elements or data to put in transmit packet and then also to copy whole file and make another file and insert some data in this file. for example i am attaching text file and i want to take p from second row and and 1 from second row and put it into some array. i want to save same file withsome other name but want to put data in result and p/f

0 Kudos
Message 1 of 19
(3,477 Views)

What you want to do can be accomplished either with standard C functions (fopen, fclose, fgets and so on) or with functions from the Formatting and I/O Library (OpenFile, CloseFile, ReadFile, ReadLine and so on).

If you plan to reuse the code in some other compiler you will need to use standard functions, otherwise you can choose whichever set of functions you want. I personally find more handy the functions from the Formatting library but you may find easier to use the others.

 

I suggest you to find some CVI example regarding file I/O and try understanding how it works: your task is quite basic and there's lots of code to read. The easiest way to find examples related to some function is probably to search for the function in the help: available examples, if any, are listed at the bottom of the page.



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 19
(3,457 Views)

kindly give me some very basic code for understanding.

0 Kudos
Message 3 of 19
(3,454 Views)

hi sir. i have read file and and save it in to buffer. one task is completed. sir i want to extract some data in comma seperated value. for example

p,1,main value,lower limit,uper limit,result

i want to pick p and 1 and then put this data in some array to form my transmit packet. how can i extract it or is there any way that some function read file and put data on the basis of comma. means it consider every value between commas as a one value. kindly brief me in detail i shall be thankful to you sir

0 Kudos
Message 4 of 19
(3,442 Views)

Extracting data from a string is normally named "scanning" and can be accomplished in several ways. Since you have numbers and strings mixed together in a comma-separated string you must be careful on how to scan your data out. You may use functions like sscanf (ANSI_C) or Scan (formatting and /IO library) or even strtok (ANSI_C): read the documentation for each of them.

 

Depending on how your string is made up a function may be more handy than others, I would use this one that works on your sample data file:

#include <formatio.h>
static char	msg[64];
static char	title[16], dependency[16];
static int		sequence, pass;
static double	lowerlimit, upperlimit, result;

strcpy (msg, "p,1,p4,25.5,26.5,,");
Scan (msg, "%s[xt44]%d[x]%s[xt44]%f[x]%f[x]%f[x]%d", title, &sequence, dependency, &lowerlimit, &upperlimit, &result, &pass);

A problem of this command is that is tied up to the specific separator you use (in this case the comma, ASCII 44; should the string have semicolons you should place "t59" instead). I strongly recommend you to examine the documentation on Scan as the command is powerful and full of options that can help you in treating your data.

 

sscanf is a bit more cumbersome in my opinion since it appears that there is no space between data and this complicates things a bit.

 

An alternative command can be strtok which permits to treat strings with different separators (i.e. the same code can scan values from a string with commas as well as semicolons). Information on the use of strtok can be found in the help for the function.



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?
0 Kudos
Message 5 of 19
(3,438 Views)

Thank u so much sir for your kind help. sir kindly give me one more advise or suggestion that teacher has given me the option of a arranging or the format of a file. kindly you tell me that in which case is the easiest way to handle the  file and extract data from file. for example in attach file i separate data with commas. data could be separated with tabs or another format. you suggest me to whenever i need to read text file what format should i adopt. because it is relaxation from teacher. sir similarly if the same data in excel sheet how could i read and extract each data.

0 Kudos
Message 6 of 19
(3,425 Views)

Let's do it the opposite way Smiley Wink

I already gave you some hint in my previous responses and I suppose your teacher has given you some other infos to consider so, what's your opinion?

Based on my experience, data can be extracted from files regardless their format, but scanning them can be more or less difficult depending on how it's structured.

Let's limit the framework to one-line-per-record format which is quite handy and consider only the structure of a single line: it's quite easy to experiment with different separators (comma, semicolon, space, tab...) and different functions (sscanf, Scan, strtok...), and after doing that we can discuss about the difficulties you've found and the structure that you consider more handy.

 

With what refers to Excel, that is completely different as the file format is very complicated and requires a separate instrument to read and write data. If you really want to give it a try you can search the Excel examples that ship with CVI: run the Example Finder (Help >> Find Examples... menu option) and search for Excel to locate them. But since I seem to understand that your knowledge on CVI is quite limited I really discourage you from going that way.



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?
0 Kudos
Message 7 of 19
(3,422 Views)

sir i have read scan function and format specifiers. i have understand many of the theory. 

one thing i could not understand in your code. you have used 

%s[xt44]. in this case t44 means coma then what is the meaning of x. similarly u have not used xt44 with
%f[x]%f[x]%f[x]%d. how that part of code will seperate data from commas. what is the meaning of[x] used with f. because in my thinking now i have to use [t44] as many times as is coma in the string. sorry sir u may say its too simple but i need more explanation 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0 Kudos
Message 8 of 19
(3,415 Views)

It seems you have missed some important part of the documentation, please look at the explanation for f specifier:

f Real number As a source or target specifier, this specifier indicates that the corresponding parameter is a real number, or if rep is present, a real array. If you use f as a source specifier in conversions to string formats, the functions convert the floating-point value into ASCII form. If you use f as a target specifier in conversions from string format, the functions consume bytes of the source parameter as long as they match the pattern of floating-point ASCII numbers or until the functions encounter the end of the string. The functions convert the scanned characters to a floating-point value and place the values into the corresponding floating-point or floating-point array target parameter.
The pattern for floating-point ASCII numbers is an optional sign (+ or -), a series of one or more decimal digits that can contain a decimal point, and an optional exponent that consists of an E or e followed by an optionally signed decimal integer value.

 

As you can see in the highlight, when scanning a floating point value the function will stop when it finds a character not recognized as part of a number (see later for the list of allowed characters); that is, even without any specification, the function will stop scanning the source string when it finds a comma, semicolon, tab, space or whichever character you use as a separator.

BTW, this could give you some idea on the separator not to use if you happen to treat data coming from whichever source that uses commas as decimal separator, as it happens in several parts of the world.

 

The x is explained a bit later:

x Discard terminator The x floating-point modifier causes the functions to discard the character that terminates the numeric data. In this way, the functions can skip terminator characters when reading lists of numeric input. Thus, %3f[x] reads three floating-point numbers, disregarding the terminator character that appears after each one. You can use this specifier to scan the string 3.5, 7.6, -32.4.

 

The documentation page I linked previously is crucial in preparing a correct scanning strategy: study it carefully and keep it in mind when creating the scanning string. It happens to me to go and read it again sometimes even after 20+ years working with CVI in case I meet some unusual data format.



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?
0 Kudos
Message 9 of 19
(3,411 Views)
strcpy (msg, "p,1,p4,25.5,26.5,,");
Scan (msg, "%s[xt44]%d[x]%s[xt44]%f[x]%f[x]%f[x]%d", title, &sequence, dependency, &lowerlimit, &upperlimit, &result, &pass);

 sir in above code u gave an example when you use ist specifier %s[xt44] what is meaning of x in this. because i understand now %f[x] here x means to skip coma. i co uldnt understand %s[xt44]. is only [x] is not enough to skip coma

0 Kudos
Message 10 of 19
(3,405 Views)