Thread: Converting character array into an integer array

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    1

    Converting character array into an integer array

    I have a string array in a file that contains the following:

    1, 2, 3, 4, 5

    I am trying to pick out the characters that are not commas and convert them to integers in a new array so I can do some calculations on those integers.

    I've been looking into the 'fgetc' function. Here is my code and output:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main()
    {
      /* open the file 'access.log' for reading */
      FILE * file = fopen ("numbersLog.dat", "r");
      
      char c;
      
      do
      {
        c = fgetc(file);
        printf("%c\n", c);
      }
      while (c != EOF);  
    
    
      fclose(filePointer);
    }
    //////OUTPUT////
    1
    ,
    2
    ,
    3
    ,
    4
    ,
    5




    My program works in print each character, line by line. But, I am at a complete loss in how to use the fgetc function to convert strings to integers and to omit the commas

    Many thanks


    Digisweep

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There are such things as isdigit(), ispunct(), and so on that can tell you whether a character is a digit or a punctuation or whatever. Also: do you plan to get as large as 10? If so, you'll have to process multiple characters.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    You also might have a look at (obsolete but still working) atoi() or atol() or use the newer strtol() function

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by ddutch View Post
    You also might have a look at (obsolete but still working) atoi() or atol() or use the newer strtol() function
    Huh? Since when are atoi() and atol() obsolete?

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Hodor View Post
    Huh? Since when are atoi() and atol() obsolete?
    I have no idea where this guy gets his info from. He thinks things are standard that aren't, things are obsolete when they're not even deprecated in C11, and things are new when strtol has been around since at least C89.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting an Array of Character to a String
    By IPthereforIam in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2012, 08:57 AM
  2. Replies: 8
    Last Post: 11-12-2008, 11:25 AM
  3. Converting character array to integer array
    By quiet_forever in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2007, 05:48 AM
  4. Converting a text box value into a character array?
    By n00bguy in forum Windows Programming
    Replies: 3
    Last Post: 07-29-2006, 08:08 PM
  5. Converting a string to a character array
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2003, 10:25 PM