04-20-2011 09:54 AM
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
.
.
.
.
04-20-2011 10:51 AM
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
04-20-2011 11:32 AM
I answered this in your other post here: http://forums.ni.com/t5/LabVIEW/reading-from-file/m-p/1531320
04-20-2011 12:27 PM
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]);