LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

read from file

I was wondering if someone could help here. I have to read from a file line by line such that each time the word FAIL is found,

 

output each listed character follow by a tab and the corresponding number. For each missing character here give it the value found after FAIL.

 

for example from the attached file: in the first line I have FAIL 100 so I will output:

 

e          102

i            699

o          717

and I have to add the missing character of the alphabet that are not here and give them the value after FAIL

so, I will have

e          102

i            699

o          717

a          100

b         100

c          100

d         100

f         100

g          100

h         100

j          100

k          100

l          100

.

.

.

.

0 Kudos
Message 1 of 4
(2,759 Views)

Basically this is not a complicated task. I can give you a pseudo-code trace of how I will solve the problem.

 

 

Open the file (OpenFile / fopen)

wasSearching = 0

Until end-of-file
   Read a line (with ReadLine / fgets)
   Search 'FIND'
   if found
      if wasSearching
         print the list from the array
      scan line for the associated value
      wasSearching = 1
      prepare an array for all alphabetic characters with the value found
   if not found and wasSearching
      scan letter and associated value
      insert value in the array already prepared
   else
      do nothing

print the last list of values
Close the file

 

 



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 2 of 4
(2,740 Views)

I answered this in your other post here: http://forums.ni.com/t5/LabVIEW/reading-from-file/m-p/1531320

0 Kudos
Message 3 of 4
(2,730 Views)

gembo:

 

Are you using LabVIEW or LabWindows/CVI?

 

Your open ended question doesn't show any thought or effort on your part before you asked for help.  I'm guessing that Roberto posted some pseudo-code, rather than just write your program for you, to encourage you to start to think like a programmer.  Once you do that, a project like yours is straight-forward in any language.

 

One tip for CVI to make handling of the array Roberto mentions easier is to let the letter define the array subscript.  Your PC stores letters of the alphabet by their ASCII codes.  E.g. 'a' = 0x61, 'b' = 0x62, ... 'z' = 0x7a.

 

In ANSI C (on which CVI is based), you can use type casting to easily more between characters and integers.

For example

int i;

i = (int) 'a';  // take the letter a, and store its ASCII value as an integer

// i now equals 0x61, decimal 97

 

You can also do math on characters.

For example

int i;

char myChar = 'd';

i = (int) myChar - (int) 'a';

// i now equals 3

 

Now back to your array.

int letterValues[26];  // declare an array of integers, one for each letter in the alphabet

int failValue;  // to store the value on the FAIL line

char myChar;  // to store the letter on a non-FAIL line

int myValue;  // to store the value on a non-FAIL line

 

// now read your file, line by line

// when you find the word FAIL, read the value and store it to failValue

// now initialize the entire letterValues[] array to failValue

// now continue reading each line, storing the letter to myChar and the value to myValue

// until you find the next FAIL line.

// on each line, it it's not a FAIL line, use the ASCII value of myChar as the array index

letterValues[(int) myChar - (int) 'a'] = myValue;

 

When you find the next FAIL line or end of file, print the letterValues array, or whatever else you want to do.

If you want to print the letter for each value, you can again rely on the ASCII conversion.

int i;

for (i=0; i<26; i++)

  printf("%c = %d\n", (char) (i + (int) 'a'), letterValues[i]);

0 Kudos
Message 4 of 4
(2,724 Views)