Convert CHAR --> LONG --> FLOAT 
Author Message
 Convert CHAR --> LONG --> FLOAT

My C program has to read a file like :
________________________________________________________________________

Line 1:
AAAAAAAAAAAAAAAAAAAAAAAAABCCCCCCCCCCCCCCCCCCCCCCCCCDDDD1234567891234EEEE
 EEEEE11F
Line 2:
DEBU2OPHELIEOPHELIE---
FI20debu222222222222222222fi2UNIB1234567890123AZE22YUIO11W
________________________________________________________________________

So i use "fscanf" command.
In fact, the field "1234567891234" has to be 123456789,1234 with 4
positions after comma, in order to make comparisons, etc ...

How could i transform ALPHA in NUMERIC
________________________________________________________________________

#include <stdio.h>
#include <stdlib.h>
void main( void )
{
#define FichierCM "CM.txt"
FILE *fileCM;
char Ref  [26];
char Code  [2];
char Compo  [26];
char Unit  [5];
int  Qte  [14];
char fill1  [10];
char Nivo  [3];
char fill2  [2];

int i_ENT=0;
long i_LONG=0;
float i_FLOAT=0;
fileCM = fopen(FichierCM, "r" );

if( fileCM == NULL )
  {
  printf( "Erreur d'ouverture du fichier ...");
  return;
  }
while ( fscanf (fileCM,"%25s",Ref) != EOF )
  {
  fscanf(fileCM ,"%1s"  , Code );
  fscanf(fileCM ,"%25s" , Compo );
  fscanf(fileCM ,"%4s"  , Unit );
  fscanf(fileCM ,"%13s" , Qte );
  fscanf(fileCM  ,"%9s"  , fill1 );
  fscanf(fileCM  ,"%2s"  , Nivo );
  fscanf(fileCM  ,"%1s"  , fill2 );
        printf("%25s%1s%25s%4s%13s%9s%2s%1s"
                        ,Ref,Code,Compo,Unit,Qte,fill1,Nivo,fill2);
    i_LONG = (long) Qte;
  i_FLOAT = i_LONG;
  printf("\n");
  printf(i_LONG);
  printf("\n");
  printf("%f" ,i_FLOAT);
  printf("\n");
  }
fclose( fileCM );

Quote:
}

________________________________________________________________________

The result is :

Line 1 :
AAAAAAAAAAAAAAAAAAAAAAAAABCCCCCCCCCCCCCCCCCCCCCCCCCDDDD1234567891234EEEE
 EEEEE11F
1234567891234
1244964.000000

Line 2 :
DEBU2OPHELIEOPHELIE---
FI20debu222222222222222222fi2UNIB1234567890123AZE22YUIO11W
1234567890123
1244964.000000
________________________________________________________________________

1234567891234 becomes ... 1244964.000000 !!!... and i don't understand
why such a change in the value of field ...

                                  Thanks for answer. -))

_________________________________________
Bruno PONTEPRIMO - SEMA Group
Nanterre - France


--



Mon, 03 Feb 2003 03:00:00 GMT  
 Convert CHAR --> LONG --> FLOAT

Quote:

> How could i transform ALPHA in NUMERIC

Read the documentation of fscanf() again, and you'll see how.

Quote:
> 1234567891234 becomes ... 1244964.000000 !!!... and i don't understand
> why such a change in the value of field ...

Your result is effectively random garbage, caused by incorrect code
where you just cast a string to long to extract a value, which simply
cannot work.
--

Even if all the snow were burnt, ashes would remain.
--



Tue, 04 Feb 2003 03:00:00 GMT  
 Convert CHAR --> LONG --> FLOAT


Quote:
>My C program has to read a file like :
>________________________________________________________________________

>Line 1:
>AAAAAAAAAAAAAAAAAAAAAAAAABCCCCCCCCCCCCCCCCCCCCCCCCCDDDD1234567891234EEEE
> EEEEE11F
>Line 2:
>DEBU2OPHELIEOPHELIE---
>FI20debu222222222222222222fi2UNIB1234567890123AZE22YUIO11W
>________________________________________________________________________

>So i use "fscanf" command.
>In fact, the field "1234567891234" has to be 123456789,1234 with 4
>positions after comma, in order to make comparisons, etc ...

>How could i transform ALPHA in NUMERIC
>________________________________________________________________________

#include <stdio.h>
#include <stdlib.h>
/* defines usually go at the top of a file just after the includes */
#define FichierCM "CM.txt"

/* Don't use void main() main has always returned an int */
int main( void )
{
    FILE *fileCM;
    char Ref[26];
    char Code[2];
    char Compo[26];
    char Unit[5];
    int  Qte[14];
    char fill1[10];
    char Nivo[3];
    char fill2[2];

    int i_ENT = 0;
    long i_LONG = 0;
    float i_FLOAT = 0;
    fileCM = fopen(FichierCM, "r" );

    if( fileCM == NULL )
    {
        printf( "Erreur d'ouverture du fichier ...");
        return;
    }

    while ( fscanf (fileCM,"%25s",Ref) != EOF )
    {
        fscanf(fileCM ,"%1s"  , Code );
        fscanf(fileCM ,"%25s" , Compo );
        fscanf(fileCM ,"%4s"  , Unit );
        fscanf(fileCM ,"%13s" , Qte );
        fscanf(fileCM  ,"%9s"  , fill1 );
        fscanf(fileCM  ,"%2s"  , Nivo );
        fscanf(fileCM  ,"%1s"  , fill2 );
        printf("%25s%1s%25s%4s%13s%9s%2s%1s"
                        ,Ref,Code,Compo,Unit,Qte,fill1,Nivo,fill2);
        /* You can't do this to change a string to a long */
        i_LONG = (long) Qte;
        i_FLOAT = i_LONG;
        printf("\n");
        /* You can't do this, you have to use a format specifier within
printf */
        printf(i_LONG);
        printf("\n");*/

        printf("%lu\n", i_LONG);
        printf("%f\n" ,i_FLOAT);
    }
    fclose( fileCM );

    return 0;

Quote:
}

##############################################################
The following is one way of doing what you want, though having a buffer
containing several fields with no recognised separator is slightly
unusual and makes the problem slightly harder. If the source of your
data could be altered to separate the fields by some symbol (commas
perhaps)
then it becomes much easier to determine the boundaries.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_BUF_LEN 1000
#define NUM_VALS    8

typedef enum data_type{
    INVALID,
    NUMERIC,
        STRING

Quote:
} dataTypeT;

typedef struct infoType{
    size_t    size;
    dataTypeT type;

Quote:
}infoT;

typedef union bufferType{
        char string[MAX_BUF_LEN];
    unsigned long myLong;
    signed long   signLong;
    double        realVal;

Quote:
}bufferT;

int ReadIntoBuffer(bufferT * buf, infoT *facts, size_t num, char
*source);

int main( void )
{
    FILE *fileCM;
    char buffer[MAX_BUF_LEN];
    bufferT dest[NUM_VALS];
    infoT dataInfo[NUM_VALS] = {{25, STRING},
                                 {1, STRING},
                                 {25, STRING},
                                 {4, STRING},
                                 {13, NUMERIC},
                                 {9, STRING},
                                 {2, STRING},
                                 {1, STRING}};
    int readVals;

    char Ref[26];
    char Code[2];
    char Compo[26];
    char Unit[5];
    int  Qte[14];
    char fill1[10];
    char Nivo[3];
    char fill2[2];

    fileCM = fopen("test.txt", "r" );

    if( fileCM == NULL )
    {
        printf( "Erreur d'ouverture du fichier ...");
        return EXIT_FAILURE;
    }

    while (fgets (buffer, MAX_BUF_LEN, fileCM))
    {
        readVals = ReadIntoBuffer(dest, dataInfo, NUM_VALS, buffer);
        if(NUM_VALS != readVals)
        {
            fprintf(stderr, "Error in reading values from file.\n");
        }
        printf("%15.5f\n", dest[4].realVal/10000);
    }
    fclose(fileCM);

    return EXIT_SUCCESS;

Quote:
}

int ReadIntoBuffer(bufferT *buf, infoT *facts, size_t num, char *source)
{
    size_t i;
    char local[MAX_BUF_LEN];
    char *p;
    size_t offset = 0;

    for(i = 0; i < num; ++i)
    {
        memcpy(local, source + offset, facts[i].size);
        local[facts[i].size] = '\0';

        switch(facts[i].type)
        {
            case NUMERIC:
                buf[i].realVal = strtod(local, &p);
                /* Add error checking here and return i */
                break;
            case STRING:
                strcpy(buf[i].string, local);
                break;
        }
        offset += facts[i].size;
    }
    return i;

Quote:
}

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Bob Wightman
--



Tue, 04 Feb 2003 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Convert CHAR --> LONG --> FLOAT

2. Convert four chars to a long and a long to four chars

3. B>C>C++>VC++(MFC)>RAW API PROGRAMMING>ATL>COM>OLE>ADO>ASP.............

4. >>>>>>>>> What would you like in a debugger?

5. Q?: double|float <-> long

6. Q?: double|float <-> long|int portability

7. <<<<<<<Parsing help, please>>>>>>>>

8. Help >>on Float and Double Type

9. C->->->->C++

10. >>>> PUBLICATION QUALITY SOFTWARE MANUAL

11. File Format conversion, ascii freeform -->.csv <-->.wk1<-->dbf<-->?HELP

12. '>>' on char explanation needed

 

 
Powered by phpBB® Forum Software