4 Replies - 312 Views - Last Post: 06 December 2012 - 02:58 AM Rate Topic: -----

#1 Pandamonium  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 26-October 10

Read numbers from file and then format them.

Posted 05 December 2012 - 09:51 PM

Hi, I am working on a C program to read phone numbers from file in various formats, such as:
404.817.6900
 (215)  686-1776
312-746-6000
877 275 5273
6173434200


and then convert them into the (XXX)XXX-XXXX format.
Here is my code so far:

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

int main() {

    FILE *fp;
    char str[31], ch;
    int i = 0, p1, p2, p3;
    fp = fopen("numbers.txt", "r");


        while((ch = getc(fp)) != '\n')
       {
 if(isdigit(ch))
                str[i++] = ch;
        str[i] = '\0';
        i = 0;

        sscanf(str, "%3d%3d%4d", &p1, &p2, &p3);
        printf("(%3d) %3d-%4d\n", p1, p2, p3);
      }
fclose(fp);
    return 0;
}



unfortunately it currently doesnt work. when i run it, it prints this:
(  4) 4196261-32767
(  0) 4196261-32767
(  4) 4196261-32767
(  4) 4196261-32767
(  8) 4196261-32767
(  1) 4196261-32767
(  7) 4196261-32767
(  7) 4196261-32767
(  6) 4196261-32767
(  9) 4196261-32767
(  0) 4196261-32767
(  0) 4196261-32767
(  0) 4196261-32767



Is This A Good Question/Topic? 0
  • +

Replies To: Read numbers from file and then format them.

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Read numbers from file and then format them.

Posted 05 December 2012 - 09:59 PM

The easiest way is to read ONLY digits, out of the str array, and put them into a phone array. Start putting them in, at the phone[1], index, instead of phone[0]. Skip phone[4], also, going from 3 to 5.

When you have all the numbers in, assign phone[0] '(', and phone[4] to ')', and you're done. Easiest way to do this is to test with isdigit() and including <ctype.h> in your include file list.

This post has been edited by Adak: 05 December 2012 - 10:00 PM

Was This Post Helpful? 1
  • +
  • -

#3 Pandamonium  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 26-October 10

Re: Read numbers from file and then format them.

Posted 05 December 2012 - 10:05 PM

OK, so ill set i = 1; at the start and change it so each time it runs it resets to i=1; instead of 0. but how would i code it to skip when i=4?
Was This Post Helpful? 0
  • +
  • -

#4 Pandamonium  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 26-October 10

Re: Read numbers from file and then format them.

Posted 05 December 2012 - 10:29 PM

Also i keep wondering if I should have ch as an int instead of a char.
Was This Post Helpful? 0
  • +
  • -

#5 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Read numbers from file and then format them.

Posted 06 December 2012 - 02:58 AM

View PostPandamonium, on 05 December 2012 - 10:29 PM, said:

Also i keep wondering if I should have ch as an int instead of a char.


I like using

FILE *fp;

//open the file for reading, then test fp to be sure the file opened. If not, return to stop the program.

//this is a great way to get all the lines of text out of a text file

i=0;
while((fgets(buffer, sizeof(buffer), fp)) != NULL) {
   get the length of the data in the buffer
   for(k=0,j=1;k<length;k++) {
      if(isdigit(buffer[k]) {
         phones[i][j]=buffer[k];
         ++j;
         if(j==4)
            j=5;
      }
      else if(buffer[k]=='\n') {
       
         //more code here
      }

}

//then print out the phone numbers with a for loop, using %s and phones[i]

//and don't forget to fclose(fp), the file.



Quite a bit. Then pick what you want out of your buffer, and put it into your char phone[?][15] array. Where ? is whatever the max number of phone numbers is, and the current row of a phone number.

It gets a little tricky, because your phone array will have rows, as well as columns, so you'll need three variables.
I used i,j,k.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1