File Access with Strings and Numbers

This is a discussion on File Access with Strings and Numbers within the C Programming forums, part of the General Programming Boards category; I'm just trying to learn C and I've come accross something that stumps me. How do you read in a ...

  1. #1
    Registered User Mace's Avatar
    Join Date
    Nov 2001
    Posts
    7

    Question File Access with Strings and Numbers

    I'm just trying to learn C and I've come accross something that stumps me. How do you read in a file that has both words and numbers as both strings and doubles? Say a .txt file has a name, name, number, number all on separate lines and you want to input them as stings and as doubles. I've tried combining fscanf and fgets in for loops and while loops and even stand alones, I keep getting messed up on the numbers part. Any help would be greatly appreciated!!!

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    If you read '1' or '2' from a text file it is still text.....just its ascii representation...........to turn '2' to say an interger you could use "atoi()" or one of the similar fuctions depending on what you wish to convert it to.

  3. #3
    Registered User Mace's Avatar
    Join Date
    Nov 2001
    Posts
    7
    I'm sorry, I'm not familiar with "atoi()" How would you use that to convert the string "zz" to integer "yy"?
    Code:
    int yy;
    char zz[10];
    
    strcpy(zz,"5280");
    
    printf("%.2i",yy);

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    atoi( ) is pretty simple:

    int x;

    x = atoi( "1234" );

    x will equal 1234.

    In your case:

    yy = atoi( zz );

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2007, 12:38 PM
  2. Replies: 9
    Last Post: 03-17-2006, 11:44 AM
  3. String parser
    By sand_man in forum C Programming
    Replies: 13
    Last Post: 08-13-2005, 10:33 AM
  4. strings and numbers
    By Granger9 in forum C Programming
    Replies: 3
    Last Post: 09-10-2002, 04:11 PM
  5. character strings and reading in a file...
    By Unregistered in forum C Programming
    Replies: 14
    Last Post: 07-30-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21