LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with string value from a .txt File

Hello,
i will open a .dat file in my programms.
I have different informations in my file. So, i use a token to use it seperat.
so I can use every number in my file with the own string.
This is the structure of my file:

96400116
98 4
503 309
*TASTEN
5 22
145 284 25
170 284 39
195 284 53

But I have a Problem. The first number i a line is everytime correct. "98"
But the last number in every line is not correct. "4|"
You can see the sign "|"
I think the problem ist the end of the line. But I have no \n on the end of line.
And I get this files, so I can´t do anything with the structure!!!

This is the structure of my file:

96400116
98 4
503 309
*TASTEN
5 22
145 284 25
170 284 39
195 284 53

And this ist my program part. I want compare my token with the string "*TASTEN". But I get the token velue "*TASTEN|".
So my compare result ist not correct!!!

while (!feof(input_stream)) //Solange kein End-Of-File in der Eingabe...
{

if(fgets (line, LINE_LENGHT, input_stream)) {
temptok = line;
for(i = 0; (akt_token = strtok(temptok, DELIM)) != NULL && (i < ANZ_SPALTEN); ++i) {
temptok = NULL;

if(!strcmp(akt_token, "*TASTEN")) {
g_Flag1 = 1;
}
else
{
g_Flag1 = 0;
}



and so on....




Please help me!
Thanks!!!
0 Kudos
Message 1 of 4
(3,335 Views)
You are probably reading a line terminator character which is not discarded by fgets.

Looking at the structure of your file, I suggest you to use the functions in the Formatting and IO Library, which permits you to read a whole line at a time by using ReadLine function: this function discards the line terminator (if any) and should return you the true content of the file.

Otherwise, read the file in a single string variable and break the program to look at the variable contens: you should be able this way to look at the "|" character value (BTW, "|" is a usual way the OS represents non-printable characters, so you must look at the character value using Format >> Decimal menu option in the String Display panel), next modify your code to discard line terminators.

Hope this helps


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


@C.Tommy wrote:
Hello,
i will open a .dat file in my programms.
I have different informations in my file. So, i use a token to use it seperat.
so I can use every number in my file with the own string.
This is the structure of my file:

96400116
98 4
503 309
*TASTEN
5 22
145 284 25
170 284 39
195 284 53

But I have a Problem. The first number i a line is everytime correct. "98"
But the last number in every line is not correct. "4|"
You can see the sign "|"
I think the problem ist the end of the line. But I have no \n on the end of line.
And I get this files, so I can´t do anything with the structure!!!

This is the structure of my file:

96400116
98 4
503 309
*TASTEN
5 22
145 284 25
170 284 39
195 284 53

And this ist my program part. I want compare my token with the string "*TASTEN". But I get the token velue "*TASTEN|".
So my compare result ist not correct!!!

while (!feof(input_stream)) //Solange kein End-Of-File in der Eingabe...
{

if(fgets (line, LINE_LENGHT, input_stream)) {
temptok = line;
for(i = 0; (akt_token = strtok(temptok, DELIM)) != NULL && (i < ANZ_SPALTEN); ++i) {
temptok = NULL;

if(!strcmp(akt_token, "*TASTEN")) {
g_Flag1 = 1;
}
else
{
g_Flag1 = 0;
}



and so on....




Please help me!
Thanks!!!




@C.Tommy wrote:
Hello,
i will open a .dat file in my programms.
I have different informations in my file. So, i use a token to use it seperat.
so I can use every number in my file with the own string.
This is the structure of my file:

96400116
98 4
503 309
*TASTEN
5 22
145 284 25
170 284 39
195 284 53

But I have a Problem. The first number i a line is everytime correct. "98"
But the last number in every line is not correct. "4|"
You can see the sign "|"
I think the problem ist the end of the line. But I have no \n on the end of line.
And I get this files, so I can´t do anything with the structure!!!

This is the structure of my file:

96400116
98 4
503 309
*TASTEN
5 22
145 284 25
170 284 39
195 284 53

And this ist my program part. I want compare my token with the string "*TASTEN". But I get the token velue "*TASTEN|".
So my compare result ist not correct!!!

while (!feof(input_stream)) //Solange kein End-Of-File in der Eingabe...
{

if(fgets (line, LINE_LENGHT, input_stream)) {
temptok = line;
for(i = 0; (akt_token = strtok(temptok, DELIM)) != NULL && (i < ANZ_SPALTEN); ++i) {
temptok = NULL;

if(!strcmp(akt_token, "*TASTEN")) {
g_Flag1 = 1;
}
else
{
g_Flag1 = 0;
}



and so on....




Please help me!
Thanks!!!


First, Thank you for your help.
I think it is not so easy!!!
I did not work very long with CVI and so I do not know the functions in CVI very good.
Perhaps you can help me to change my program???
I do not understand all what you write very good, because I´m not from the USA.
I´m from Europe.
0 Kudos
Message 3 of 4
(3,323 Views)
My basic idea is this one (it relies on the hipotesis thet the special marker is at the very left of its row).

// Open the file in ASCII mode
fH = OpenFile ("pathname", VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
if (fH < 0) goto Error;

// Read the file one line at a time
while (error = ReadLine (fH, msg, -1) != -2) {
if (error == -1) goto Error; // Error in read

if (!strncom (msg, "*TASTEN", 7) break; // Keyword found
}
if (error == -2) {error = 0; goto Error; } // Keyword not found

// Add here your code to continue reading file and interpreting the string read
// ...


// Clear error indicator (If you're here all worked well!)
error = 0;

Error:
if (fH > 0) CloseFile (fH); // Close file on exit
if (error < 0) {
sprintf (msg, "Error %d received: \n%s.", error, GetFmtIOErrorString (error));
MessagePopup ("Severe error", msg);
}



Hope all is clear. If it's not tell me and I will try to clarify your doubts.


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 4 of 4
(3,316 Views)