Thread: types and string conversion

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    9

    types and string conversion

    Hi I need to convert numbers to a string using sprintf() and convert a string to numbers using sscanf().

    This is the code I have so far and I am not sure how to do the conversion back to a string once they've been converted to floats.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void main(){
    
    float x,y,z;
    FILE *fp=fopen("temp", "a+");
    char *str[20], buf[20];
    
    
    printf("enter 3 numbers.");
    fgets(str, 20, fp);
    
    sscanf(str,"%f,%f,%f", &x,&y,&z);
    Any help would be appreciated.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Guess what?
    Code:
    sprintf(str2,"%f,%f,%f", x,y,z);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: types and string conversion

    I think you have solved your requirement I don't know where are you confusing?
    But only one mistake I found that is why are you selecting array of pointers for storage.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(){
    
    float x,y,z;
    FILE *fp=fopen("temp", "a+");
    char str[20], buf[20];
    
    
    printf("enter 3 numbers.");
    fgets(str, 20, fp);
    
    sscanf(str,"%f,%f,%f", &x,&y,&z); // string to float conversion
    
    printf("%f %f %f\n",x,y,z);
    sprintf(buf,"%f,%f,%f",x,y,z); // float to string conversion
    
    printf("buf:%s\n",buf);
    
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    Ganesh program works good.But,I want to add one exception handling in it.

    In his program,he opened the file in append mode.If the file not exits,then it will create and it will seek for string from the file.Empty string will be returned.sscanf function will seek for float values in the string.There is no string at all.Then,it will give some garbage value.To avoid this,you change ganesh code into the following.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(){
    
    float x,y,z;
    FILE *fp=fopen("temp", "r+");
    if(fp==NULL)
    {
            printf ( "No File Found.Create It and Run the Program.\n"  );
            perror("File");
            exit(1);
    }
    char str[20], buf[20];
    
    
    printf("Enter 3 numbers:");
    fgets(str, 20, fp);
    printf ( "STRING:%s\n",str  );
    
    sscanf(str,"%f,%f,%f", &x,&y,&z); // string to float conversion
    
    printf("%f %f %f\n",x,y,z);
    sprintf(buf,"%f,%f,%f",x,y,z); // float to string conversion
    
    printf("buf:%s\n",buf);
    
    return 0;
    }
    Last edited by vivekraj; 03-03-2010 at 11:18 PM.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    code is nice use perror to print the error message
    for ex :
    Code:
     if(fp==NULL)
            {
                            perror(err);
                            exit(1) ;
            }
    Last edited by Alexander jack; 03-03-2010 at 11:25 PM. Reason: add a sample code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes and redefinition; different basic types help
    By StealthRT in forum C++ Programming
    Replies: 3
    Last Post: 10-04-2009, 04:10 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM