Thread: reading strings and then converting them

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    5

    Unhappy reading strings and then converting them

    Hello

    Im newbie on C and have a little problem.
    So, I have ASCII file that with 1100 rows and 550 columns. I need to write a program what read tha file and then convert all those numbers to decimal number.
    like: 5.5000000e+01 should be 55.000000


    so how can i do it?



    Abies

  2. #2
    Register User andor's Avatar
    Join Date
    Aug 2006
    Location
    Novi Sad
    Posts
    42
    Code:
    int main()
    {
       /* your code going here */
       return 0;
    }
    Did you try to write something? Well you should read char by char and find out some logic to convert it to decimal
    Last edited by andor; 11-28-2006 at 09:28 AM.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Use fscanf to read each number into a double. Once you have the number, you can print it out using printf in any format you want (decimal, scientific notation, so many digits after the decimal, etc).

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'm quite sure we didn't need the WHOLE file just to get the idea.
    What have you tried so far?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    my code to read file, but how can i split strings and then convert them?


    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main( int argc, char *argv[] )
    {
       char buffer[10000]; // longest line
       int i, nr;
    
       if ( argc != 2)
       {
               printf("usage: %s filename", argv[0]);
       }
       else
       {
            FILE *input = fopen( argv[1], "r");
        
        if ( input == NULL);
        {
            printf("Error opening file \n");
            exit(-1);
        }
    
        nr = 0; // line_nr before while 0
        while(fgets(buffer, sizeof(buffer), input) != NULL)
        {
            nr++; // increase number
            printf("%d.%s", nr, buffer); // puting line_nr before line
        }
        fclose (input);    // closing input_file
    	return 0;
        }
    }
    Abies

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    right having this, replace the fgets function to fscanf function to read a double value from the file, and convert that into double. This is done internally by the fscanf

    Code:
    while(fscanf(fp,"%lf",&num) != EOF)  \\this converts from string to double internally, so don't need to do that convert separate
    printf("%f\n",num);
    ssharish2005

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    Code:
    while(fscanf(fp,"%lf",&num) != EOF)
    printf("%f\n",num);
    I need that the program would leave all rows and columns at the same place, but would convert numbers into decimal.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Something like:
    Code:
    while(fgets(buffer, sizeof(buffer), input) != NULL)
    {
    	double number;
    	while(sscanf(buffer,"%lf", &number) == 1)
    		fprintf(output, "%10.6f ",num);
    	fprintf(output, "\n");
    }
    ?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660

    Code:
    $ awk '{ for(i=1;i<=NF;i++) { printf("%f ",$i+0); } printf("\n"); }'
    55
    55.000000
    5.5E1 22.001 3E-1
    55.000000 22.001000 0.300000
    Black text is input, red text is output
    Just add a filename to the end of the command line and luxuriate
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    oh.. my mistake

    I need that program read number like 5.5009091e+01 from input file
    then just convert it to 55.009091 and leave all rows and columns at the same place.

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    btw. thanks Salem

Popular pages Recent additions subscribe to a feed