NO

Author Topic: Reading a .txt file  (Read 7591 times)

xsilvergs

  • Guest
Reading a .txt file
« on: May 15, 2007, 12:06:06 PM »
Hi

Not sure if this is beginners or Pocket PC specific but I've written it for PPC.

I am using a datalogger program on my PPC from www.KRMicros.com. The logger creates a .txt file of values from which which my own code takes the value and displays them in an X Y graph, or at least it should.

If I write a text file myself in NotePad and transfer it to the pocket PC my code can read it.

If the KRMicros software creates the .txt file my code is unable to read the .txt file. BUT

If I drag this unreadable file to my desktop PC, open it in NotePad then just SAVE it and put it back to the same folder on ths Pocket PC thus overwritting the original my code reads the file ok.

So, I guess something happens during the file save on my desktop PC. Can anybody explain this and suggest a solution please?

THank you

Tony

 

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Reading a .txt file
« Reply #1 on: May 15, 2007, 03:35:20 PM »
Maybe the newline formatting from the datalogger output is not the standard CR-LF, or the format is unicode.
Check with an hex editor, or dump, your .txt file and check that each line is terminated with '0dh-0ah' characters, if not consider to open file in binary mode and look for the end of line char.
If the file is unicode you have to use unicode routines to extract data.
If you need more help post a short extract of your code, where you read the file, and a sample .txt incorrect file.
« Last Edit: May 15, 2007, 03:43:27 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

xsilvergs

  • Guest
Re: Reading a .txt file
« Reply #2 on: May 15, 2007, 03:45:49 PM »
I was just trying to post a .txt file and it failed.

xsilvergs

  • Guest
Re: Reading a .txt file
« Reply #3 on: May 15, 2007, 03:49:05 PM »
And here is my code which hasn't been cleaned as it's still development.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Reading a .txt file
« Reply #4 on: May 15, 2007, 04:18:49 PM »
What I can see is that the output of your program is not a standard text format of any known type (at least known to me! ;D).
I have also seen that what notepad does is just substitute the zeroes in the file with spaces (hex 20).
So I suggest to change your input code:
Code: [Select]
while (fgets(s, 1000,f))// !=NULL)// this creates an array s[ ]
fclose(f);
With:
Code: [Select]
  int i;
  for (i=0; i<1000-1; i++)
  {
    int in;
    in = fgetc(f);
    if (in == EOF)
        break;
    if ( !in )
      s[i] = ' ';
    else
      s[i] = in;
  }
  fclose(f);
  s[i] = 0;
You have also open the file for binary read:
Code: [Select]
f=fopen("Program Files/Serial Data Logger/LogData.txt","rb");
« Last Edit: May 15, 2007, 04:25:59 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

xsilvergs

  • Guest
Re: Reading a .txt file
« Reply #5 on: May 16, 2007, 12:27:48 PM »
frankie

Thanks for the code, I understand how it works (I think) but have trouble when it reads a data value of 0 (zero).

When a value of zero is read it translates it into 32, I think it is to do with this line here:

if ( !n )
s = ' '; If this creates a space,

a space is equal to 32 in ascii.

Am I right?

Can you suggest a way around this, I've tried a few things but am unable to get it working?

Thanks

Tony

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Reading a .txt file
« Reply #6 on: May 16, 2007, 02:24:21 PM »
Yes my code substitutes the bytes with value 0 (zero) with the ascii code for space (that is 32 decimal, 0x20 hex). I've done this because I made a trial opening the file with notepad then saving it back. What I have found is that notepad translated each zero to space.
If this still doesn't work I don't really understand:
1) which translation is made by notepad
2) what kind of data you have
I consider that the file you have is not a text file definitely! >:(
What I understand is that your file is a binary file containing a stream of short integer values (16 bits), where each record is delimited by a code which value is 65 ('A').
In this case try to treat it as a bynary file and read it sequentially:
Code: [Select]
  f=fopen("Program Files/Serial Data Logger/LogData.txt","rb");
f=fopen("Program Files/Serial Data Logger/LogData.txt","rb");
if (!f)
return 1;
iLength = fread(s,1,1000,f);
fclose(f);
while(p <= iLength-1)
{
if (s[p] == 65) //if s[p] is equal to 65 or "A" then
data[d] = s[p+1] + s[p+2]*128;
      ........ etc.
Do you have any spec of the expected output file from the datalogger?
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

xsilvergs

  • Guest
Re: Reading a .txt file
« Reply #7 on: May 16, 2007, 03:59:53 PM »
frankie

Thanks again.

What I've done is created a sort of error trap, the A is used to identify the next two bytes as data. The first 8 bit byte contains 7 bits, 0 to 127 the most significant bit is redundant. The next byte contains only 3 bits (7 + 3 = 10 the size of the output from the AtoD). I have to multiply this byte by 128 and add the two bytes together. As the second byte can be no larger than 7 I now test and if greater than 7 make it equal to zero this gets around the 32 problem. The first byte will never actually drop to zero as there is always enough activity to prohibit that.

I'll keep trying to improve my code as time goes on.

The project I'm working on grabs the output from a dental xray unit and transmits it via BlueTooth (all that was the easy bit) to my PocketPC, I'm now able to view the output profile on my PPC (that was the hard bit).

This is only my third C project but has been great fun, I'm most grateful for your help and I now fell I'm using my PPC for something usefull.

Tony


Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Reading a .txt file
« Reply #8 on: May 16, 2007, 04:44:45 PM »
So your file IS a binary file.
Last code I posted should work fine using fread instead of fgets. Your problem was due to fgets that stops reading on zero.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

xsilvergs

  • Guest
Re: Reading a .txt file
« Reply #9 on: May 17, 2007, 11:02:30 AM »
frankie

fread works like a dream, thank you.

I have a question regarding text colour. If

         swprintf(wszBuffer,L"%i",arraysize);
         SetDlgItemText(hwndDlg,TXT_ARRAYSIZE,wszBuffer);

writes the text to a textbox, how do you change the colour of the text in the box?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Reading a .txt file
« Reply #10 on: May 17, 2007, 06:29:51 PM »
A text box is a standard window, so to change colour you can use standard text colour procedure: get window device context, set object for pen, colour, etc...
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide